alertbot/bot.py

119 lines
3.2 KiB
Python
Raw Normal View History

2021-04-01 16:17:54 +02:00
#!/usr/bin/env python3
import argparse
import asyncio
import dataclasses
import logging
import aiohttp.web
import jinja2
import markdown
import nio
import yaml
@dataclasses.dataclass
class Config:
matrix_homeserver: str
matrix_user: str
matrix_password: str
matrix_room: str
webhook_token: str
TEMPLATE = (
"{% if status == 'resolved' %}"
"<b><font color='green'>RÉSOLU</font></b> "
"{% elif labels.severity == 'critical' %}"
"@room <b><font color='red'>CRITIQUE</font></b> "
"{% elif labels.severity == 'warning' %}"
"<b><font color='orange'>ATTENTION</font></b> "
"{% endif %}"
"<b>{{ labels.alertname }}</b> "
"{{ labels.instance }}<br/>"
"<blockquote>{{ annotations.summary }}</blockquote>"
)
async def format_messages(config, src, dest):
env = jinja2.Environment()
template = env.from_string(TEMPLATE)
while True:
message = await src.get()
for alert in message["alerts"]:
rendered = template.render(**alert)
await dest.put((rendered))
async def send_notices(config, messages):
client = nio.AsyncClient(config.matrix_homeserver, config.matrix_user)
await client.login(config.matrix_password)
while True:
formatted = await messages.get()
await client.room_send(
config.matrix_room,
message_type="m.room.message",
content={
"msgtype": "m.text",
"format": "org.matrix.custom.html",
"body": formatted, # à corriger
"formatted_body": formatted,
},
)
async def run_app(config, out, host, port):
async def webhook(request):
tokens = request.query.getall("token", [])
if config.webhook_token not in tokens:
logging.warning("Invalid tokens: %s", tokens)
raise aiohttp.web.HTTPForbidden("Invalid tokens")
message = await request.json()
logging.debug("Incoming message: %s", message)
await out.put(message)
return aiohttp.web.Response()
app = aiohttp.web.Application()
app.add_routes([aiohttp.web.post("/webhook", webhook)])
runner = aiohttp.web.AppRunner(app)
await runner.setup()
await aiohttp.web.TCPSite(runner, host, port).start()
async def main():
logging.basicConfig(level=logging.INFO)
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--config", default="config.yaml")
parser.add_argument("--host", default="*")
parser.add_argument("--port", type=int, default=8000)
args = parser.parse_args()
with open(args.config) as f:
config = yaml.safe_load(f)
config = Config(**config)
messages = asyncio.Queue()
formatted = asyncio.Queue()
logging.info("Started Alertbot on %s:%d", args.host, args.port)
tasks = (
asyncio.create_task(run_app(config, messages, args.host, args.port)),
asyncio.create_task(format_messages(config, messages, formatted)),
asyncio.create_task(send_notices(config, formatted)),
)
# on propage les exceptions
await asyncio.gather(*tasks, return_exceptions=False)
if __name__ == "__main__":
asyncio.get_event_loop().run_until_complete(main())