Compare commits

..

1 commit

Author SHA1 Message Date
2a296c09d1 Dump json 2022-03-23 17:00:44 +01:00
4 changed files with 5 additions and 30 deletions

View file

@ -8,10 +8,6 @@ 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

View file

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

View file

@ -1,8 +1,7 @@
import json
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__)
@ -16,6 +15,9 @@ def root() -> t.Tuple[str, int]:
def alertmanager(): def alertmanager():
data = request.get_json(cache=False) 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: 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:
@ -26,22 +28,5 @@ 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

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