mirror of
https://github.com/moan0s/alertbot.git
synced 2024-10-01 06:25:35 -04:00
Initial Commit
This commit is contained in:
commit
73660a9b5b
26
README.md
Normal file
26
README.md
Normal file
@ -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
|
||||
```
|
61
alertbot.py
Normal file
61
alertbot.py
Normal file
@ -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`")
|
8
maubot.yaml
Normal file
8
maubot.yaml
Normal file
@ -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
|
Loading…
Reference in New Issue
Block a user