-
Notifications
You must be signed in to change notification settings - Fork 13
/
operation.py
executable file
·359 lines (288 loc) · 13.9 KB
/
operation.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/python
# coding:utf-8
import logging
from dbread import *
from modconf import *
from send_wechat import *
from send_email import *
from send_sms import *
a = getConfig('log')
logging.basicConfig(filename=a['logfile'], encoding="utf-8", filemode="a",
format="%(asctime)s %(name)s:%(levelname)s:%(message)s", datefmt="%Y-%m-%d %H:%M:%S",
level=logging.DEBUG)
def mergeproblem(originallist):
""" 告警信息合并
将不同triggerkey的报警放到List不同的下标里
举例子: [[{A主机的SSH报警},{B主机的SSH报警}],[{A主机的ICMP报警},{B主机的ICMP报警}]]
"""
problemlist = []
# normalist = []
Unknown = []
triggerkeylist = []
sorts = []
alarminfo = []
# 判断告警or恢复,这里只处理告警状态的消息
for origina in originallist:
if origina['triggervalue'] == '1':
problemlist.append(origina)
if origina['triggerkey'] not in triggerkeylist:
triggerkeylist.append(origina['triggerkey'])
else:
Unknown.append(origina)
# 将不同triggerkey的报警放到List不同的下标里
# 举例子: [[{A主机的SSH报警},{B主机的SSH报警}],[{A主机的ICMP报警},{B主机的ICMP报警}]]
for triggerkey in triggerkeylist:
for problem in problemlist:
if problem['triggerkey'] == triggerkey:
sorts.append(problem)
alarminfo.append(sorts)
sorts = []
return alarminfo
def mergenormal(originallist):
""" 恢复信息合并 """
normallist = []
Unknown = []
triggerkeylist = []
sorts = []
alarminfo = []
for origina in originallist:
if origina['triggervalue'] == '0':
normallist.append(origina)
if origina['triggerkey'] not in triggerkeylist:
triggerkeylist.append(origina['triggerkey'])
else:
Unknown.append(origina)
for triggerkey in triggerkeylist:
for normal in normallist:
if normal['triggerkey'] == triggerkey:
sorts.append(normal)
alarminfo.append(sorts)
sorts = []
return alarminfo
def compressproblem(alarminfo):
""" 告警信息压缩
将相同triggerkey的报警消息合并为一条并增加发送类型和收件人
举例子: [['告警脚本名字','收件人','主题:A+B主机的SSH报警','内容:A+B主机的SSH报警'],['告警脚本名字','收件人','主题:A+B主机的ICMP报警','内容:A+B主机的ICMP报警']]
"""
currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
messagelist = []
for info in alarminfo:
hostlist = []
hostgroup = []
eventidlist = []
actionlist = []
subjectlist = []
eventtime = info[0]['eventtime']
triggerstatus = info[0]['triggerstatus']
triggerseverity = info[0]['triggerseverity']
triggername = info[0]['triggername']
eventage = info[0]['eventage']
itemvalue = info[0]['itemvalue']
# 将主机分组
for host in info:
triggerkey = host['triggerkey']
hostinfo = host['hostname']
# 单主机有存在多组的情况,所以先切分后判断
hostgrousplit = host['hostgroup'].split(', ')
for group in hostgrousplit:
if group not in hostgroup:
hostgroup.append(group)
hostlist.append(hostinfo)
# if host['hostgroup'] not in hostgroup:
# hostgroup.append(host['hostgroup'])
# hostlist.append(hostinfo)
# 获取eventid列表
eventid = host['eventid']
eventidlist.append(eventid)
# 获取actions列表
action = host['action']
actionlist.append(action)
# 获取subject列表
subject = host['subject']
subjectlist.append(subject)
# 根据subjectlist查询告警接收人和mediatypeid
alert_receivera = alert_receiver(subjectlist, triggerkey)
# 查询media_type然后定义告警信息及收件人/收件类型
# EMAIL [email protected]
messageinfo = []
# 查询所有的脚本名字和对应的mediatypeid
media_type_list = mediatype()
receiverlist = []
receiverlisttwo = []
# 将host去重
hostlist = list(set(hostlist))
# 计算告警信息条数和告警主机台数
infonum = len(info)
hostnum = len(hostlist)
# 将list处理为字符串-每个值之间增加逗号方便发送显示
hoststr = ",".join(str(i) for i in hostlist)
hostgroupstr = ",".join(str(i) for i in list(set(hostgroup)))
actionstr = ",".join(str(i) for i in list(set(actionlist)))
eventidstr = ",".join(str(i) for i in eventidlist)
# 将两者融合得到结果:"通过什么脚本发送给谁"
# 最终得到的结果: messagelist: [['告警脚本名字','收件人','告警信息主题','告警消息内容'],['告警脚本名字','收件人','告警信息主题','告警消息内容']]
for a in media_type_list:
for b in alert_receivera:
if a[0] in b:
actions = a[1]
receiverlist.append(b[1])
continue
# 如果收件人为空则不执行下面的代码
if (len(receiverlist)) == 0:
continue
messageinfo.append(actions)
messageinfo.append(receiverlist)
receiverlisttwo.append(receiverlist)
receiverlist = []
# # 如果收件人为空则不执行下面的代码
# if (len(messageinfo[1])) == 0:
# continue
if infonum == 1:
if messageinfo[0] == 'SMS':
message = "故障%s:%s,%s在%s发生%s故障!\n详情见邮件" % (
triggerstatus, triggerseverity, hoststr, eventtime.split(' ')[1], triggername)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
else:
subject = "故障%s,告警等级:%s,服务器:%s发生:%s故障!" % (triggerstatus, triggerseverity, hoststr, triggername)
message = "告警主机 : " + hoststr + \
"\n告警等级 : " + triggerseverity + \
"\n告警项目 : " + triggername + \
"\n监控配置 : " + triggerkey + \
"\n当前状态 : " + triggerstatus + ", " + itemvalue + \
"\n告警时间 : " + eventtime + \
"\n分析时间 : " + currenttime + \
"\n事件ID : " + eventidstr + \
"\nActions : " + actionstr
messageinfo.append(subject)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
elif infonum > 1:
if messageinfo[0] == 'SMS':
message = "故障%s:%s服务器组:%s共%s台服务器在%s发生:%s故障!详情见邮件" % (
triggerstatus, triggerseverity, hostgroupstr, str(infonum), eventtime.split(' ')[1],
triggername)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
else:
subject = "故障%s,告警等级:%s服务器组:%s共%s台服务器发生:%s故障! %s条相同告警被压缩!" % (
triggerstatus, triggerseverity, hostgroupstr, str(hostnum), triggername, str(infonum))
message = "共" + str(infonum) + "条相同告警被压缩!" + "共" + str(hostnum) + "台服务器故障!" \
"\n告警等级 : " + triggerseverity + \
"\n告警项目 : " + triggername + \
"\n监控配置 : " + triggerkey + \
"\n当前状态 : " + triggerstatus + ", " + itemvalue + \
"\n涉及主机组 : " + hostgroupstr + \
"\n涉及主机器 : " + hoststr + \
"\n告警时间 : " + eventtime + \
"\n分析时间 : " + currenttime + \
"\n事件ID : " + eventidstr + \
"\nActions : " + actionstr
messageinfo.append(subject)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
# 将合并细节打印到日志中
logging.info("compresslog, Eventid:'%s', triggervalue:1, hostgroup:%s, host:%s, triggerkey:'%s', actions:%s, receiverlist:%s" % (
eventidstr, hostgroupstr, hoststr, triggerkey, actionstr, receiverlisttwo))
return messagelist
def compressnormal(alarminfo):
""" 恢复信息压缩 """
currenttime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
messagelist = []
for info in alarminfo:
hostlist = []
hostgroup = []
eventidlist = []
actionlist = []
subjectlist = []
triggerseverity = info[0]['triggerseverity']
triggerstatus = info[0]['triggerstatus']
triggername = info[0]['triggername']
eventage = info[0]['eventage']
for host in info:
triggerkey = host['triggerkey']
hostinfo = host['hostname']
if host['hostgroup'] not in hostgroup:
hostgroup.append(host['hostgroup'])
hostlist.append(hostinfo)
eventid = host['eventid']
eventidlist.append(eventid)
action = host['action']
actionlist.append(action)
subject = host['subject']
subjectlist.append(subject)
alert_receivera = alert_receiver(subjectlist, triggerkey)
messageinfo = []
media_type_list = mediatype()
receiverlist = []
receiverlisttwo = []
hostlist = list(set(hostlist))
infonum = len(info)
hostnum = len(hostlist)
hoststr = ",".join(str(i) for i in hostlist)
hostgroupstr = ",".join(str(i) for i in list(set(hostgroup)))
actionstr = ",".join(str(i) for i in list(set(actionlist)))
eventidstr = ",".join(str(i) for i in eventidlist)
for a in media_type_list:
for b in alert_receivera:
if a[0] in b:
actions = a[1]
receiverlist.append(b[1])
continue
if (len(receiverlist)) == 0:
continue
messageinfo.append(actions)
messageinfo.append(receiverlist)
receiverlisttwo.append(receiverlist)
receiverlist = []
if infonum == 1:
subject = "恢复%s, 故障持续%s,服务器:%s:%s已恢复!" % (triggerstatus, eventage, hoststr, triggername)
message = "恢复%s, 故障持续%s\n服务器:%s:%s已恢复!\n告警等级:%s\n分析时间:%s" % (
triggerstatus, eventage, hoststr, triggername, triggerseverity, currenttime)
messageinfo.append(subject)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
elif infonum > 1:
subject = "恢复%s, 故障持续%s,服务器组:%s共%s台服务器%s已恢复!,%s条相同恢复信息被压缩!" % (
triggerstatus, eventage, hostgroupstr, str(hostnum), triggername, str(infonum))
message = "恢复" + triggerstatus + ",故障持续" + eventage + \
"\n服务器组 : " + hostgroupstr + "共" + str(hostnum) + "台服务器" + triggername + "已恢复!" + \
"\n涉及服务器 : " + hoststr + \
"\n告警等级 : " + triggerseverity + \
"\n分析时间 : " + currenttime + \
"\n共" + str(infonum) + "条相同恢复信息被压缩!"
messageinfo.append(subject)
messageinfo.append(message)
messagelist.append(messageinfo)
messageinfo = []
logging.info("compresslog, Eventid:'%s', triggervalue:0, hostgroup:%s, host:%s, triggerkey:'%s', actions:%s, receiverlist:%s" % (
eventidstr, hostgroupstr, hoststr, triggerkey, actionstr, receiverlisttwo))
return messagelist
def sendalarmmessage(messagelist):
""" 根据media_type定义的告警脚本名称分别发送给对应的人 """
if len(messagelist) != 0:
for content in messagelist:
if content[0] == 'WECHAT':
tos = content[1]
message = content[2] + '\n' + content[3]
a = WeChat('https://qyapi.weixin.qq.com/cgi-bin')
a.SendWechatMessage(tos, message)
logging.info("media_type: %s, tos: %s, message: %s" % (content[0], tos, message.replace("\n", ', ')))
if content[0] == 'EMAIL':
tos = content[1]
subject = content[2]
message = content[3]
SendEmailMessage(tos, subject, message)
logging.info(
"alertlog, media_type: %s, tos: %s, message: %s" % (content[0], tos, message.replace("\n", ', ')))
if content[0] == 'SMS':
tos = ",".join(str(i) for i in content[1])
message = content[2]
SendSmsMessage(tos, message)
logging.info(
"alertlog, media_type: %s, tos: %s, message: %s" % (content[0], tos, message.replace("\n", ', ')))