From ec6b1dc0b0cb0cdc64fa171838ffd51a980d54bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20M=C3=A4der?= <maeder@phys.ethz.ch> Date: Mon, 11 Apr 2022 14:58:41 +0200 Subject: [PATCH] Add missing deb, config and /webhook/grafana --- .gitignore | 4 ++++ app.py | 52 +++++++++++++++++++++++++++++++++++++++--------- config.py | 1 + requirements.txt | 1 + 4 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 config.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..21f22a7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vim* +.bash* +.profile* +env/ diff --git a/app.py b/app.py index 223f939..dfc5d3d 100644 --- a/app.py +++ b/app.py @@ -1,7 +1,6 @@ import requests from flask import Flask, request, make_response - -url = 'https://hookshot.mbot.ethz.ch/webhook/' +from config import url app = Flask(__name__) @@ -9,8 +8,8 @@ app = Flask(__name__) def slack(hook): plain = '' html = '' - markdown = '' incoming = request.json + print('Got incoming /slack hook: ' + str(incoming)) if 'attachments' in incoming: for attachment in incoming['attachments']: @@ -40,19 +39,54 @@ def slack(hook): html += '<b>' + title + '</b>: ' + value + '<br/>\n' html += '</font>' if color else '' - if text and html: - r = requests.post(url + hook, json={'text':plain,'html':html}) + if plain and html: + json = {'text':plain,'html':html} + print('Sending hookshot: ' + str(json)) + r = requests.post(url + hook, json=json) else: - print('Invalid format: ' + incoming) + print('Invalid format, sending unmodified.') r = requests.post(url + hook, json=incoming) return {"ok":True} @app.route("/webhook/grafana/<hook>", methods=['POST']) def grafana(hook): - incoming = request.json - r = requests.post(url + hook, json=incoming) - return incoming + plain = '' + html = '' + incoming = dict(request.json) + print('Got incoming /grafana hook: ' + str(incoming)) + + title = str(incoming.get('title', '')) + rule_url = str(incoming.get('ruleUrl', '')) + rule_name = str(incoming.get('ruleName', '')) + message = str(incoming.get('message', '')) + state = str(incoming.get('state', '')) + eval_matches = incoming.get('evalMatches', []) + + if title and rule_url and rule_name: + plain += title + ' ' + rule_url + ': ' + rule_name + ' (' + state + ')\n' + html += '<b><a href="' + rule_url + '">' + title + '</a></b>: ' + rule_name + ' (' + state + ')<br/>\n' + + if message: + plain += message + '\n' + html += message + '<br/>\n' + + for eval_match in eval_matches: + metric = str(eval_match.get('metric', '')) + value = str(eval_match.get('value', '')) + if metric and value: + plain += metric + ': ' + value + '\n' + html += '<b>' + metric + '</b>: ' + value + '<br/>\n' + + if plain and html: + json = {'text':plain,'html':html} + print('Sending hookshot: ' + str(json)) + r = requests.post(url + hook, json=json) + else: + print('Invalid format, sending incoming as str.') + r = requests.post(url + hook, json={'text':'Invalid format: ' + str(incoming)}) + + return {"ok":True} if __name__ == "__main__": app.run(port=9080, debug=True) diff --git a/config.py b/config.py new file mode 100644 index 0000000..3b638ab --- /dev/null +++ b/config.py @@ -0,0 +1 @@ +url = 'https://hookshot.mbot.ethz.ch/webhook/' diff --git a/requirements.txt b/requirements.txt index c85c873..e9f7f37 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ flask +requests uwsgi -- GitLab