Skip to content

Commit 1aed853

Browse files
committed
feat: added alertmanager, send alerts to telegram chat
1 parent b2f5d92 commit 1aed853

18 files changed

Lines changed: 185 additions & 13 deletions

.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ TELEGRAM_CHANNEL=
2727

2828
CLICKUP_API_TOKEN=
2929
CLICKUP_SPACE_ID=
30+
31+
ALERTMANAGER_TELEGRAM_TOKEN=
32+
ALERTMANAGER_TELEGRAM_CHAT_ID=

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ FROM python:3.11
22

33
RUN apt update --no-install-recommends -y
44

5+
RUN apt-get update && \
6+
apt-get install -y cmake && \
7+
rm -rf /var/lib/apt/lists/*
8+
59
ENV PYTHONFAULTHANDLER=1 \
610
PYTHONUNBUFFERED=1 \
711
PYTHONHASHSEED=random \

Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
up:
22
docker compose -f docker-compose.yml up -d
33
down:
4-
docker compose -f docker-compose.yml down
4+
docker compose -f docker-compose.yml down
5+
6+
build:
7+
docker compose -f docker-compose.yml build
8+
9+
superuser:
10+
docker exec -it web poetry run python manage.py createsuperuser
11+
12+
migrate:
13+
docker exec -it web poetry run python manage.py migrate
14+
15+
migrations:
16+
docker exec -it web poetry run python manage.py makemigrations
17+
18+
logs:
19+
docker container logs web

alerts/__init__.py

Whitespace-only changes.

alerts/admin.py

Whitespace-only changes.

alerts/apps.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class AlertsConfig(AppConfig):
5+
default_auto_field = "django.db.models.BigAutoField"
6+
name = "alerts"

alerts/migrations/__init__.py

Whitespace-only changes.

alerts/urls.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from django.urls import path
2+
from .views import alert_webhook
3+
4+
app_name = "alerts"
5+
6+
urlpatterns = [
7+
path("webhook/", alert_webhook, name="alert_webhook"),
8+
]

alerts/views.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
import logging
3+
import requests
4+
import socket
5+
from django.conf import settings
6+
from django.http import HttpResponseForbidden
7+
from django.http import JsonResponse
8+
from django.views.decorators.csrf import csrf_exempt
9+
10+
logger = logging.getLogger(__name__)
11+
12+
13+
# todo: refactor this
14+
15+
16+
def allow_alertmanager_only(view_func):
17+
def _wrapped_view(request, *args, **kwargs):
18+
alertmanager_ip = socket.gethostbyname("alertmanager")
19+
20+
client_ip = request.META["REMOTE_ADDR"]
21+
print("abcd", client_ip, alertmanager_ip)
22+
if client_ip == alertmanager_ip:
23+
return view_func(request, *args, **kwargs)
24+
25+
return HttpResponseForbidden("Forbidden")
26+
27+
return _wrapped_view
28+
29+
30+
@csrf_exempt
31+
@allow_alertmanager_only
32+
def alert_webhook(request):
33+
if request.method == "POST":
34+
try:
35+
payload = json.loads(request.body)
36+
for alert in payload["alerts"]:
37+
message = f"Alert: {alert['annotations']['summary']} - {alert['status']}"
38+
send_telegram_message(message)
39+
40+
return JsonResponse({"status": "success"})
41+
except Exception as exc:
42+
logger.error(f"Failed to process alert {exc}", exc_info=exc)
43+
return JsonResponse({"status": "error"}, status=400)
44+
45+
return JsonResponse({"status": "method not allowed"}, status=400)
46+
47+
48+
def send_telegram_message(message):
49+
url = (
50+
f"https://api.telegram.org/bot{settings.ALERTMANAGER_TELEGRAM_TOKEN}/sendMessage"
51+
)
52+
data = {"chat_id": settings.ALERTMANAGER_TELEGRAM_CHAT_ID, "text": message}
53+
response = requests.post(url, data=data)
54+
return response.json()

docker-compose.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ services:
1010
command: bash ./scripts/startup.sh
1111
volumes:
1212
- ./log:/procollab/log
13+
- ./db.sqlite3:/procollab/db.sqlite3
14+
- ./:/procollab
1315
env_file:
1416
- .env
1517
environment:
1618
HOST: 0.0.0.0
1719
expose:
1820
- 8000
19-
2021
grafana:
2122
image: grafana/grafana-enterprise
2223
container_name: grafana
@@ -36,6 +37,15 @@ services:
3637
- prom-data:/prometheus
3738
- ./prometheus:/etc/prometheus
3839

40+
alertmanager:
41+
image: prom/alertmanager:latest
42+
container_name: alertmanager
43+
volumes:
44+
- ./prometheus/alertmanager.yml:/etc/alertmanager/alertmanager.yml
45+
command:
46+
- '--config.file=/etc/alertmanager/alertmanager.yml'
47+
ports:
48+
- '9093:9093'
3949

4050
nginx:
4151
container_name: nginx

0 commit comments

Comments
 (0)