Compare commits

..

1 commit

Author SHA1 Message Date
8f1495cf0c Send Signal alert on alertmanager hook 2022-03-23 17:45:56 +01:00
4 changed files with 30 additions and 5 deletions

View file

@ -8,6 +8,10 @@ Provide ways to send notifications to signal-cli through webhooks
virtualenv -p python3 --system-site-packages venv
source venv/bin/activate
pip install -r requirements.txt
# Configure
cp signal_webhook/configuration.sample.py signal_webhook/configuration.py
$EDITOR signal_webhook/configuration.py
```
**Beware!** Sending messages to Signal requires `gi`, which cannot be installed

1
signal_webhook/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
configuration.py

View file

@ -1,7 +1,8 @@
import json
import typing as t
from collections import defaultdict
from flask import Flask, request
from . import signal
from . import configuration
app = Flask(__name__)
@ -15,9 +16,6 @@ def root() -> t.Tuple[str, int]:
def alertmanager():
data = request.get_json(cache=False)
with open("/tmp/dump.json", "w") as handle:
handle.write(json.dumps(data))
if "version" not in data or int(data["version"]) != 4:
return "Bad API version", 400
if "alerts" not in data:
@ -28,5 +26,22 @@ def alertmanager():
# Ignore
return "OK", 200
severities = defaultdict(int)
alert_lines = []
for alert in data["alerts"]:
...
try:
severities[alert["labels"]["severity"]] += 1
alert_lines.append(
f"<{alert['labels']['alertname']}> {alert['annotations']['description']}"
)
except KeyError as exn:
alert_lines.append(f"(malformed alert — {exn})")
msg = f"[PROMETHEUS] {len(data['alerts'])} firing: "
msg += ", ".join([f"{count} {typ}" for typ, count in severities.items()])
msg += "\n\n"
msg += "\n".join(["* " + line for line in alert_lines])
signal.signal_send(configuration.RECIPIENTS[configuration.DEFAULT_RECIPIENT], msg)
return "OK", 200

View file

@ -0,0 +1,5 @@
# Signal 'address book'
RECIPIENTS = {"foo": "+42..."} # FIXME
# Recipient from the dict above to send to by default
DEFAULT_RECIPIENT = "foo" # FIXME