diff --git a/ircbot/plugin/help.py b/ircbot/plugin/help.py index d77c0c6..6b6944e 100644 --- a/ircbot/plugin/help.py +++ b/ircbot/plugin/help.py @@ -1,6 +1,7 @@ """Provide help information.""" import collections import http.server +import pkgutil import threading import jinja2 @@ -28,6 +29,11 @@ def build_request_handler(bot): autoescape=True, ) + webhook_receivers = {} + for importer, mod_name, _ in pkgutil.iter_modules(['ircbot/plugin/webhook']): + mod = importer.find_module(mod_name).load_module(mod_name) + webhook_receivers[mod.PATH] = mod + class RequestHandler(http.server.BaseHTTPRequestHandler): def render_response(self, template, **context): @@ -57,6 +63,20 @@ def do_GET(self): self.end_headers() self.wfile.write(b'404 File not found') + def do_POST(self): + before, _, hookname = self.path.partition('/hook/') + print(webhook_receivers, before, hookname) + if not before and hookname in webhook_receivers: + content_length = int(self.headers['Content-Length']) + body = self.rfile.read(content_length) + + webhook_receivers[hookname].handle_hook(bot, body) + + self.send_response(200) + else: + self.send_response(404) + self.end_headers() + return RequestHandler diff --git a/ircbot/plugin/webhook/prometheus.py b/ircbot/plugin/webhook/prometheus.py new file mode 100644 index 0000000..393c7bd --- /dev/null +++ b/ircbot/plugin/webhook/prometheus.py @@ -0,0 +1,22 @@ +"""Handle webhooks from Prometheus AlertManager""" +# See https://prometheus.io/docs/alerting/configuration/#webhook_config for a +# description of the webhook format. + +import json + +PATH = 'prometheus' + +def handle_hook(bot, body): + body_json = json.loads(body.decode()) + for alert in body_json['alerts']: + if alert['status'] == 'resolved': + status = '\x02\x0303OK\x0F' + else: + status = '\x02\x0304FIRING\x0F' + + alert = '{status} \x02{alertname}\x0F: {summary}'.format( + status=status, + alertname=alert['labels']['alertname'], + summary=alert['annotations']['summary'], + ) + bot.say('#rebuild-spam', alert)