Skip to content
This repository was archived by the owner on Oct 2, 2024. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions ircbot/plugin/help.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Provide help information."""
import collections
import http.server
import pkgutil
import threading

import jinja2
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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


Expand Down
22 changes: 22 additions & 0 deletions ircbot/plugin/webhook/prometheus.py
Original file line number Diff line number Diff line change
@@ -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)