思路
- 将产生的告警事件对应的subjectID(对应一条告警事件)、actionID(对应告警接收组)推送到redis
- 每隔1分钟获取redis存储的ID查询zabbix数据库,同时清空redis已经读取过的key
- 将获取的所有告警事件,根据监控项 [triggerkey](或IP地址)分组
- 分组后的信息发送时,判断该组告警的条数,同时对详细信息去重
验证
分组后信息格式
- 下面展示的是两组监控项,非关键内容省略
[
[ # 第一组 system.cpu.load[percpu,avg5]
{
'triggerkey': 'system.cpu.load[
percpu,
avg5
]',
'actionid': '22'
},
{
'triggerkey': 'system.cpu.load[
percpu,
avg5
]',
'actionid': '22'
}
],
[ # 第二组 vm.memory.size[available]
{
'triggerkey': 'vm.memory.size[
available
]',
'actionid': '22'
},
{
'triggerkey': 'vm.memory.size[
available
]',
'actionid': '22'
},
{
'triggerkey': 'vm.memory.size[
available
]',
'actionid': '22'
}
]
]
实现代码
alarmGroupCompress.py # 计划任务1分钟执行一次,拉取告警
EmailSend # 笔记邮件发送
sendRedis.py # zabbix调用自动以脚本存入告警
- 实现分组
# 根据监控项分组
for triggerkey in triggerkeylist:
for problem in problemlist:
if problem['triggerkey']==triggerkey:
sorts.append(problem)
if len(sorts)>=2:
alarminfo.append(sorts)
else:
problemlist1.append(sorts)
sorts=[] # 告警事件分组,同一组的放到一个切片中
# 根据IP地址分组 ( 如果不能根据监控项分类 )
for problem in problemlist1:
if problem[0]['ipaddress'] not in hostlist:
hostlist.append(problem[0]['ipaddress'])
for host in hostlist:
for problem in problemlist1:
if problem[0]['ipaddress']==host:
sorts.append(problem[0])
alarminfo.append(sorts) # 每个ip一个分组
sorts=[]
- 实现收敛
for info in alarminfo:
hostlist=''
hostgroup=''
triggernamelist=''
# 每组告警的条数统计
infonum=len(info)
for host in info:
triggername=host['triggername']
itemvalue=host['itemvalue']
ipaddr=host['ipaddress']
triggeritems=host['triggeritems']
triggernseverity=host['triggernseverity']
hostinfo=host['hostname']+':'+host['ipaddress']+'\n'
# 针对告警要发送的详细信息做收敛
if host['hostgroup'] not in hostgroup:
hostgroup+=host['hostgroup']+'\n'
if host['hostname'] not in hostlist:
hostlist+=host['hostname']+', '
if host['triggername'] not in triggernamelist:
triggernamelist+=host['triggername']+'\n'
# 每组告警条数统计发送规则
if infonum >= 2 and infonum <= 6:
message='【'+revel[str(triggernseverity)]+'】'+'\n告警数量: '+str(infonum)+'项\n'+hostlist+'\n相关项目: \n'+triggernamelist+'\n'+'分析时间: '+currenttime
messagelist.append(message)
elif infonum > 6:
message='【'+revel[str(triggernseverity)]+'】'+'\n当前存在大量相同告警项,可能发生网络故障!\n详情请咨询运维人员!\n'+'告警主机: '+str(infonum)+'台\n'+'告警项目: '+triggername+'\n'+'分析时间: '+currenttime
messagelist.append(message)
# 一条也发送
else:
message='【'+revel[str(triggernseverity)]+'】'+'\n告警主题: '+triggername+'\n告警项目: '+ triggeritems +'\n告警主机: '+ipaddr+'\n当前值: '+itemvalue+'\n分析时间: '+currenttime
messagelist.append(message)













网友评论