forked from sunsharing-note/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zhoubao.py
167 lines (162 loc) · 5.04 KB
/
zhoubao.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#coding=utf-8
import requests,json,codecs,datetime,time,pandas
from email import encoders
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr
from smtplib import SMTP
import smtplib
ApiUrl = 'http://192.168.1.2/api_jsonrpc.php'
header = {"Content-Type":"application/json"}
user="Admin"
password="zabbix"
x=(datetime.datetime.now()-datetime.timedelta(minutes=30)).strftime("%Y-%m-%d %H:%M:%S")
y=(datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
def gettoken():
data = {"jsonrpc": "2.0",
"method": "user.login",
"params": {
"user": user,
"password": password
},
"id": 1,
"auth": None
}
auth=requests.post(url=ApiUrl,headers=header,json=data)
return json.loads(auth.content)['result']
def timestamp(x,y):
p=time.strptime(x,"%Y-%m-%d %H:%M:%S")
starttime = str(int(time.mktime(p)))
q=time.strptime(y,"%Y-%m-%d %H:%M:%S")
endtime= str(int(time.mktime(q)))
return starttime,endtime
def logout(auth):
data={
"jsonrpc": "2.0",
"method": "user.logout",
"params": [],
"id": 1,
"auth": auth
}
auth=requests.post(url=ApiUrl,headers=header,json=data)
return json.loads(auth.content)
def getevent(auth,timestamp):
data={
"jsonrpc": "2.0",
"method": "event.get",
"params": {
"output": [
"name",
"severity"
],
"value":1,
"time_from":timestamp[0],
"time_till":timestamp[1],
"selectHosts":[
#"hostid",
"name"
]
},
"auth": auth,
"id": 1
}
getevent=requests.post(url=ApiUrl,headers=header,json=data)
triname=json.loads(getevent.content)['result']
triggers=[]
a={}
for i in triname:
triggers.append(i['name'])
for i in triggers:
a[i]=triggers.count(i)
#for i in triname:
# i['count']=a[i['name']]
list2=[]
print(triname)
#print(a)
for key in a:
b={}
b['name']=key
b['host']=[i['hosts'][0]['name'] for i in triname if i['name']==key][0]
b['severity']=[i['severity'] for i in triname if i['name']==key][0]
b['count']=a[key]
list2.append(b)
# host=[i['host'] for i in list2]
# name=[i['name'] for i in list2]
# severity=[i['severity'] for i in list2]
# count=[i['count'] for i in list2]
# result=[]
# result.append(host)
# result.append(name)
# result.append(severity)
# result.append(count)
#print(result)
#return result
return list2
def convertohtml(result):
d = {}
title=['主机','触发器','告警级别','告警次数']
index = 0
for t in title:
d[t]=result[index]
index = index+1
df = pandas.DataFrame(d)
df = df[title]
h = df.to_html(index=False)
return h
def datatohtml(list2):
tables = ''
for i in range(len(list2)):
host,name,severity,count = list2[i]['host'], list2[i]['name'], list2[i]['severity'], list2[i]['count']
td = "<td>%s</td> <td>%s</td> <td>%s</td> <td>%s</td>"%( host,name, severity, count)
tables = tables + "<tr>%s</tr>"%td
base_html="""
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>zabbix监控告警</title>
</head>
<body>
<table width="900" border="0">
<tr>
<td colspan="2" style="background-color:#FFA500;">
<h4>告警级别: 1 表示:信息 2 表示:告警 3 表示:一般严重 4 表示:严重 5 表示:灾难</h4>
</td>
</tr>
<tr>
<td style="background-color:#FFD700;width:100px;">
<TABLE BORDER=1><TR><TH>主机</TH><TH>触发器</TH><TH>告警级别</TH><TH>告警次数</TH></TR>%s</TABLE>
</td>
</tr>
<tr>
<td colspan="2" style="background-color:#FFA500;text-align:center;">
zabbix告警统计</td>
</tr>
</table>
</body>
</html>
""" %tables
return base_html
def sendmail(base_html):
from_addr = '[email protected]'
password = '没有故事的陈师傅'
to_addr = '[email protected]'
smtp_server = 'smtp.qq.com'
msg = MIMEText(base_html, 'html', 'utf-8')
msg['From'] = from_addr
msg['To'] = to_addr
msg['Subject'] = Header('Zabbix本周监控报表', 'utf-8').encode()
try:
server=SMTP(smtp_server,"25") #创建一个smtp对象
#server.starttls() #启用安全传输模式
server.login(from_addr,password) #邮箱账号登录
server.sendmail(from_addr,to_addr,msg.as_string()) #发送邮件
server.quit() #断开smtp连接
except smtplib.SMTPException as a:
print (a)
auth=gettoken()
timestamp=timestamp(x,y)
getevent=getevent(auth,timestamp)
base_html=datatohtml(getevent)
sendmail(base_html)
logout(auth)