This repository has been archived by the owner on May 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpubuim.py
147 lines (127 loc) · 5.2 KB
/
pubuim.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Powered by myluoluo
import os,sys,smtplib,requests,MySQLdb,codecs
to = sys.argv[1]
subject = sys.argv[2]
body = sys.argv[3]
servicesUrl = 'https://hooks.pubu.im/services/' + to
# Zabbix 数据库连接信息
mysql_server= "127.0.0.1"
mysql_user = "zabbix"
mysql_pass = "xxxxx"
db_name = "zabbix"
# Zabbix 后台地址以及登录信息
zabbix_url = "http://zabbix.moe/"
zabbix_user = "admin"
zabbix_pass = "xxxxx"
# 放在 Web 目录,使其可被零信访问
# 若非 Root 运行,可能需要设定文件夹所属:
# chown zabbix:zabbix /home/wwwroot/default/zabbix-graph/
image_path = "/home/wwwroot/default/zabbix-graph/" + os.popen('date +%Y%m').read().rstrip() + "/"
image_url = "http://zabbix.moe/zabbix-graph/" + os.popen('date +%Y%m').read().rstrip() + "/"
stime = os.popen('date +%Y%m%d%H%M%S').read().rstrip()
period = 3600 # 秒
cookie = "/tmp/cookie"
width = 1222
# 获取报警级别(相对于零信)
def getLevel(body):
tmp = body.split('Trigger severity: ')
if any(tmp) != True:
return "info"
level = tmp[1].split('\n')[0].strip()
if level == 'Warning':
return 'warning'
if level == 'Information':
return 'info'
if level == 'Average':
return 'error'
return 'info';
# 事件详细信息地址
def getEvent(body):
tmp = body.split('Original event ID: ')
if any(tmp) != True:
return ""
eventId = tmp[1].split('\n')[0].strip()
sql = "select objectid from events where eventid=%s" % (eventId)
db = MySQLdb.connect(mysql_server, mysql_user, mysql_pass, db_name)
cursor = db.cursor()
cursor.execute(sql)
results = cursor.fetchall()
if any(results):
db.close()
return str(results[0][0]), str(eventId)
return "", ""
# 通过ITEM ID查询图表
def getGraphId(body):
# 获得ITEM ID
itemId = body.split("ITEM ID: ")[1]
db = MySQLdb.connect(mysql_server, mysql_user, mysql_pass, db_name)
cursor = db.cursor()
sql = "select graphs_items.graphid, graphs.graphtype from graphs_items,graphs where graphs_items.itemid=%s and graphs_items.graphid = graphs.graphid limit 1;" % (itemId)
cursor.execute(sql)
results = cursor.fetchall()
# 存在关联的图表
if any(results):
db.close()
return int(results[0][0]), results[0][1], 1
# 不存在关联图表
return int(itemId), 0, 0
def main(to, subject, body):
itemId = int(body.split("ITEM ID: ")[1])
level = getLevel(body)
print("level: " + level)
triggerid, eventId = getEvent(body)
if itemId > 0:
graphid, type, flag = getGraphId(body)
os.popen("""mkdir -p %s""" % (image_path))
os.popen("""curl -c '%s' -b '%s' -d "request=&name=%s&password=%s&autologin=1&enter=Sign+in" %s/index.php""" % (cookie, cookie, zabbix_user, zabbix_pass, zabbix_url))
# 存在关联的图表
if flag > 0:
print("存在关联的图表: " + str(graphid))
zabbixUrl = zabbix_url + "/charts.php?graphid=" + str(graphid)
os.popen("""curl -b '%s' -F "graphid=%d" -F "period=%d" -F "stime=%s" -F "width=%d" %s > %s%s.png""" % (cookie, graphid, period, stime, width, zabbix_url + "/chart2.php", image_path, stime))
# 不存在关联图表,仅有独立item
else:
print("不存在关联图表")
zabbixUrl = zabbix_url + "/history.php?action=showgraph&itemids[0]=" + body.split("ITEM ID: ")[1]
os.popen("""curl -b '%s' -F "itemids[0]=%s" -F "period=%d" -F "stime=%s" -F "width=%d" %s > %s%s.png""" % (cookie, graphid, period, stime, width, zabbix_url + "/chart.php", image_path, stime))
values = {
"text": subject + "\r\n" + body,
"displayUser": {
"name": "Zabbix",
"avatarUrl": "https://up.521.moe/zabbix-logo.png"
},
"attachments": [
{
"title": subject,
"url": zabbixUrl,
"color": level
},
{
"title": "查看事件详细信息",
"url": zabbix_url + """/tr_events.php?triggerid=%s&eventid=%s""" % (triggerid, eventId) ,
"color": level
},
{
"photoUrl": image_url + str(stime) + ".png",
"color": level
}
],
"buttons": [
{
"text": "滚去确认警报 _(:з」∠)_",
"url": zabbix_url + "/zabbix.php?action=acknowledge.edit&eventids[]=%s" % (eventId)
}
]
}
# 发送到零信
return requests.post(servicesUrl, json = values).text
# 没有ITEM ID,仅发送信息
values = {"text": subject + "\r\n" + body, "displayUser": {"name": "Zabbix", "avatarUrl": "https://up.521.moe/zabbix-logo.png"}}
return requests.post(servicesUrl, json = values).text
if __name__ == "__main__":
# xenbr0 等同于 eth0 避免重复 (XenServer)
if "xenbr" in subject:
exit
res = main(to, subject, body)