diff --git a/app.py b/app.py index f82b839..3c58e37 100644 --- a/app.py +++ b/app.py @@ -34,6 +34,7 @@ def send_wechat_msg(key, message): key = KEY if not isinstance(message, str): data = message + LOG.debug("send wechat msg: %s", data) respose = requests.post(WECAHT_API.format(key), json=data) LOG.info("send message result: %s:%s", respose.status_code, respose.text) if respose.status_code == 200: @@ -42,6 +43,13 @@ def send_wechat_msg(key, message): return False +def try_get_value(data_dict, keys, default_value=""): + for key in keys: + if data_dict.get(key): + return data_dict.get(key) + return default_value + + @app.route('/prometheus_webhook', methods=['POST']) def prometheus_webhook(): bearer_token = request.headers.get('Authorization', 'bearer_token ').split(" ") @@ -50,6 +58,7 @@ def prometheus_webhook(): receiver = bearer_token[1] data = request.get_json() + LOG.debug("receive msg: %s", data) msg = "" for alert in data.get('alerts'): status = alert.get('status') @@ -57,12 +66,14 @@ def prometheus_webhook(): status = '告警' if status == 'firing' else '已恢复' labels = alert.get('labels', {}) annotations = alert.get('annotations', {}) - + resource_name = try_get_value(labels, + ["resource_name", "deployment", "daemonset", "statefulset", "pod", "pod_name", + "instance"], 'cluster') resource = "{namespace}{resource_name}".format( namespace="{}/".format(labels.get("namespace")) if labels.get("namespace") else '', - resource_name=labels.get("pod", labels.get("pod_name", labels.get("instance", 'cluster')))) + resource_name=resource_name) - message = annotations.get("message", annotations.get("description", '')) + message = try_get_value(annotations, ["message", "description"], "") action = annotations.get("Action", '') runbook_url = annotations.get("runbook_url", '') action_msg = "" @@ -79,7 +90,8 @@ def prometheus_webhook(): {_action_msg} \n '''.format(_title=labels.get("alertname", ' '), _resource=resource, _status_color=status_color, _status=status, - _message=message, _action_msg=action_msg, _severity=annotations.get("Severity", ' '), + _message=message, _action_msg=action_msg, + _severity=try_get_value(labels, ["Severity", "severity"], ""), _alert_namager_url=ALEAT_MANAGER_URL if ALEAT_MANAGER_URL else alert.get('generatorURL', ' ')) result = send_wechat_msg(receiver, msg) @@ -103,6 +115,7 @@ def get_result(text='', receiver='', error=""): if __name__ == '__main__': - logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)s %(levelname)-8s %(message)s') + logging.basicConfig(level=logging.getLevelName(os.getenv('LOG_LEVEL', 'DEBUG')), + format='%(asctime)s %(name)s %(levelname)-8s %(message)s') LOG.info("app started") app.run('0.0.0.0', '8080')