commit 73660a9b5bdbcd8b31b3c8a371dfbfc677d188d3 Author: Julian-Samuel Gebühr Date: Mon Jul 11 08:44:05 2022 +0200 Initial Commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..74d35ea --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# Setup + +Use a domain e.g. webbhook.hyteck.de and configure nginx as +reverse proxy for port 4242 for this domain. + +# Connect + +Run the local server and connect via (29316 is the local maubot port) +`ssh -N -R 4242:localhost:29316 s` + + + +# Send some data with + + +Put the following in `data.json` +```json +{"receiver":"matrix","status":"firing","alerts":[{"status":"firing","labels":{"alertname":"InstanceDown","environment":"h2916641.stratoserver.net","instance":"localhost:9100","job":"node_exporter","severity":"critical"},"annotations":{"description":"localhost:9100 of job node_exporter has been down for more than 5 minutes.","summary":"Instance localhost:9100 down"},"startsAt":"2022-06-23T11:53:14.318Z","endsAt":"0001-01-01T00:00:00Z","generatorURL":"http://h2916641.stratoserver.net:9090/graph?g0.expr=up+%3D%3D+0\u0026g0.tab=1","fingerprint":"9cd7837114d58797"}],"groupLabels":{"alertname":"InstanceDown"},"commonLabels":{"alertname":"InstanceDown","environment":"h2916641.stratoserver.net","instance":"localhost:9100","job":"node_exporter","severity":"critical"},"commonAnnotations":{"description":"localhost:9100 of job node_exporter has been down for more than 5 minutes.","summary":"Instance localhost:9100 down"},"externalURL":"https://alert.hyteck.de","version":"4","groupKey":"{}:{alertname=\"InstanceDown\"}","truncatedAlerts":0} +``` +and then +```shell +curl --header "Content-Type: application/json" \ + --request POST \ + --data "@data.json" \ + https://webhook.hyteck.de/_matrix/maubot/plugin/maubot/webhook +``` diff --git a/alertbot.py b/alertbot.py new file mode 100644 index 0000000..1a13e6f --- /dev/null +++ b/alertbot.py @@ -0,0 +1,61 @@ +from maubot import Plugin, MessageEvent +from maubot.handlers import web, command +from aiohttp.web import Request, Response, json_response +import json + + +def get_alert_messages(alertmanager_data: dict) -> list: + """ + Returns a list of messages in markdown format + + :param alertmanager_data: + :return: List of alert messages in markdown format + """ + messages = [] + for alert in alertmanager_data["alerts"]: + messages.append(alert_to_markdown(alert)) + return messages + + +def alert_to_markdown(alert: dict) -> str: + """ + Converst a alert json to markdown + + :param alert: + :return: Alert as fomatted markdown + """ + message = ( + f"""**{alert['status']}**: {alert['annotations']['description']} + +* **Alertname:** {alert["labels"]['alertname']} +* **Instance:** {alert["labels"]['instance']} +* **Job:** {alert["labels"]['job']} +""" + ) + return message + + +class AlertBot(Plugin): + @web.post("/webhook") + async def webhook(self, req: Request) -> Response: + text = await req.text() + self.log.info(text) + content = json.loads(f"{text}") + for message in get_alert_messages(content): + await self.client.send_markdown("!zOcbWjsWzdREnihgeC:hyteck.de", message) + return json_response({"status": "Ok"}) + + @command.new() + async def ping(self, evt: MessageEvent) -> None: + """Answers pong to check if the bot is running""" + await evt.reply("pong") + + @command.new() + async def roomid(self, evt: MessageEvent) -> None: + """Answers with the current room id""" + await evt.reply(f"`{evt.room_id}`") + + @command.new() + async def url(self, evt: MessageEvent) -> None: + """Answers with the url of the webhook""" + await evt.reply(f"`{self.webapp_url}/webhook`") diff --git a/maubot.yaml b/maubot.yaml new file mode 100644 index 0000000..d05a196 --- /dev/null +++ b/maubot.yaml @@ -0,0 +1,8 @@ +maubot: 0.1.0 +id: de.hyteck.alertbot +version: 1.0.0 +license: AGPL-3.0-or-later +modules: + - alertbot +main_class: AlertBot +webapp: true \ No newline at end of file