Skip to content

Commit

Permalink
Merge pull request #2 from zgfh/master
Browse files Browse the repository at this point in the history
fix Severity empty
  • Loading branch information
zgfh authored Aug 8, 2019
2 parents a69e801 + c625d80 commit 8c333e0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(" ")
Expand All @@ -50,19 +58,22 @@ 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')
status_color = 'warning' if status == 'firing' else 'info'
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 = ""
Expand All @@ -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)
Expand All @@ -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')

0 comments on commit 8c333e0

Please sign in to comment.