-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
89 lines (89 loc) · 3.85 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# vim app.py
# -*- coding: utf-8 -*-
import os
import json
import requests
import arrow
from flask import Flask
from flask import request
app = Flask(__name__)
def bytes2json(data_bytes):
data = data_bytes.decode('utf8').replace("'", '"')
return json.loads(data)
def makealertdata(data):
for output in data['alerts'][:]:
try:
pod_name = output['labels']['pod']
except KeyError:
try:
pod_name = output['labels']['pod_name']
except KeyError:
pod_name = 'null'
try:
namespace = output['labels']['namespace']
except KeyError:
namespace = 'null'
try:
message = output['annotations']['message']
except KeyError:
try:
message = output['annotations']['description']
except KeyError:
message = 'null'
if output['status'] == 'firing':
status_zh = '报警'
title = '【%s】宝安楼栋生产环境 %s 有新的报警' % (status_zh, output['labels']['alertname'])
send_data = {
"msgtype": "markdown",
"markdown": {
"content": "## %s \n\n" %title +
">**告警级别**: %s \n\n" % output['labels']['severity'] +
">**告警类型**: %s \n\n" % output['labels']['alertname'] +
">**告警主机**: %s \n\n" % output['labels']['instance'] +
">**告警详情**: %s \n\n" % output['annotations']['summary'] +
">**告警状态**: %s \n\n" % output['status'] +
">**触发时间**: %s \n\n" % arrow.get(output['startsAt']).to('Asia/Shanghai').format(
'YYYY-MM-DD HH:mm:ss ZZ')
}
}
elif output['status'] == 'resolved':
status_zh = '恢复'
title = '【%s】宝安楼栋生产环境 %s 有报警恢复' % (status_zh, output['labels']['alertname'])
send_data = {
"msgtype": "markdown",
"markdown": {
"content": "## %s \n\n" %title +
">**告警级别**: %s \n\n" % output['labels']['severity'] +
">**告警类型**: %s \n\n" % output['labels']['alertname'] +
">**告警主机**: %s \n\n" % output['labels']['instance'] +
">**告警详情**: %s \n\n" % output['annotations']['summary'] +
">**告警状态**: %s \n\n" % output['status'] +
">**触发时间**: %s \n\n" % arrow.get(output['startsAt']).to('Asia/Shanghai').format(
'YYYY-MM-DD HH:mm:ss ZZ') +
">**触发结束时间**: %s \n" % arrow.get(output['endsAt']).to('Asia/Shanghai').format(
'YYYY-MM-DD HH:mm:ss ZZ')
}
}
return send_data
def send_alert(data):
#此处获取环境变量“TOKEN”,会在docker-compose的配置文件中配置,docker-compose启动docker时向docker容器注入环境变量
url = os.getenv('URL')
if not url:
print('you must set ROBOT_TOKEN env')
return
send_data = makealertdata(data)
req = requests.post(url, json=send_data)
result = req.json()
if result['errcode'] != 0:
print('notify dingtalk error: %s' % result['errcode'])
@app.route('/', methods=['POST', 'GET'])
def send():
if request.method == 'POST':
post_data = request.get_data()
send_alert(bytes2json(post_data))
return 'success'
else:
return 'weclome to use prometheus alertmanager dingtalk webhook server!'
if __name__ == '__main__':
app_port = os.getenv('PORT')
app.run(host='0.0.0.0', port=app_port)