|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +""" |
| 5 | +@version: 1.0.0 |
| 6 | +@author: zheng guang |
| 7 | +@contact: zg.zhu@daocloud.io |
| 8 | +@time: 2019/7/4 4:56 PM |
| 9 | +""" |
| 10 | + |
| 11 | + |
| 12 | +from flask import Flask, request, Response |
| 13 | +import requests |
| 14 | +import logging |
| 15 | +import json |
| 16 | +import os |
| 17 | + |
| 18 | +LOG = logging.getLogger(__name__) |
| 19 | +app = Flask(__name__) |
| 20 | + |
| 21 | +URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}" |
| 22 | +KEY = os.getenv('ROBOT_KEY', '') |
| 23 | +ALEAT_MANAGER_URL = os.getenv('ALEAT_MANAGER_URL', '') |
| 24 | + |
| 25 | + |
| 26 | +def send_wechat_msg(key, message): |
| 27 | + data = { |
| 28 | + "msgtype": "markdown", |
| 29 | + "markdown": { |
| 30 | + "content": message |
| 31 | + } |
| 32 | + } |
| 33 | + if not key and KEY: |
| 34 | + key = KEY |
| 35 | + if not isinstance(message, str): |
| 36 | + data = message |
| 37 | + respose = requests.post(URL.format(key), json=data) |
| 38 | + LOG.info("send message result: %s:%s", respose.status_code, respose.text) |
| 39 | + if respose.status_code == 200: |
| 40 | + return True |
| 41 | + else: |
| 42 | + return False |
| 43 | + |
| 44 | + |
| 45 | +@app.route('/prometheus_webhook', methods=['POST']) |
| 46 | +def prometheus_webhook(): |
| 47 | + bearer_token = request.headers.get('Authorization', 'bearer_token ').split(" ") |
| 48 | + if len(bearer_token) != 2: |
| 49 | + get_result(error="bearer_token can not null") |
| 50 | + receiver = bearer_token[1] |
| 51 | + |
| 52 | + data = request.get_json() |
| 53 | + msg = "" |
| 54 | + for alert in data.get('alerts'): |
| 55 | + status = alert.get('status') |
| 56 | + status_color = 'warning' if status == 'firing' else 'info' |
| 57 | + status = '告警' if status == 'firing' else '已恢复' |
| 58 | + labels = alert.get('labels', {}) |
| 59 | + annotations = alert.get('annotations', {}) |
| 60 | + |
| 61 | + title = "{namespace}{resource_name}: {alertname}".format( |
| 62 | + namespace="{}/".format(labels.get("namespace")) if labels.get("namespace") else '', |
| 63 | + resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster'))), |
| 64 | + alertname=labels.get("alertname", ' ')) |
| 65 | + |
| 66 | + message = annotations.get("message", annotations.get("description", '')) |
| 67 | + |
| 68 | + msg += ''' |
| 69 | +<font color="{_status_color}">{_status}</font>: [{_title}]({alert_namager_url}) \n |
| 70 | +>描述: <font color="comment">{_message}</font> |
| 71 | +>开始时间: <font color="comment">{startsAt}</font> |
| 72 | +>结束时间: <font color="comment">{endsAt}</font> |
| 73 | +\n |
| 74 | + '''.format(_title=title, _status_color=status_color, _status=status, _message=message, |
| 75 | + startsAt=alert.get('startsAt', ' '), endsAt=alert.get('endsAt', ' '), |
| 76 | + alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' ')) |
| 77 | + |
| 78 | + result = send_wechat_msg(receiver, msg) |
| 79 | + |
| 80 | + return get_result(error=result) |
| 81 | + |
| 82 | + |
| 83 | +def get_result(text='', receiver='', error=""): |
| 84 | + if isinstance(error, bool): |
| 85 | + if error: |
| 86 | + error = "" |
| 87 | + else: |
| 88 | + error = "send alert failed" |
| 89 | + result = { |
| 90 | + "receiver": receiver, |
| 91 | + "text": text, |
| 92 | + } |
| 93 | + if error: |
| 94 | + return Response(json.dumps({"error": error}), mimetype='application/json', status=400) |
| 95 | + return Response(json.dumps(result), mimetype='application/json') |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == '__main__': |
| 99 | + logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)-8s %(message)s') |
| 100 | + LOG.info("app started") |
| 101 | + app.run('0.0.0.0', '8080') |
0 commit comments