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 31 additions and 1 deletions

View file

@ -8,6 +8,10 @@ Provide ways to send notifications to signal-cli through webhooks
virtualenv -p python3 --system-site-packages venv virtualenv -p python3 --system-site-packages venv
source venv/bin/activate source venv/bin/activate
pip install -r requirements.txt 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 **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,6 +1,8 @@
import typing as t import typing as t
from collections import defaultdict
from flask import Flask, request from flask import Flask, request
from . import signal from . import signal
from . import configuration
app = Flask(__name__) app = Flask(__name__)
@ -13,6 +15,7 @@ def root() -> t.Tuple[str, int]:
@app.route("/alertmanager", methods=["POST"]) @app.route("/alertmanager", methods=["POST"])
def alertmanager(): def alertmanager():
data = request.get_json(cache=False) data = request.get_json(cache=False)
if "version" not in data or int(data["version"]) != 4: if "version" not in data or int(data["version"]) != 4:
return "Bad API version", 400 return "Bad API version", 400
if "alerts" not in data: if "alerts" not in data:
@ -23,5 +26,22 @@ def alertmanager():
# Ignore # Ignore
return "OK", 200 return "OK", 200
severities = defaultdict(int)
alert_lines = []
for alert in data["alerts"]: 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