首页 > 代码库 > Zabbix告警发送性能图
Zabbix告警发送性能图
前言:
实现zabbix告警发送性能图主要实现思路:
zabbix告警传入item.ky参数 -> 利用item.key获取当前的性能图 -> 保存到本地 -> 在发送邮件的时候构建邮件文本内容
首先先讲一下zabbix的性能图:
http://127.0.0.1/zabbix/chart.php?period=864000&stime=20180317191110&itemids%5B0%5D=25464&type=0&updateProfile=1&profileIdx=web.item.graph&profileIdx2=25464&width=1778&sid=37a0a2403c5192c8&screenid=&curtime=1490613155473
分析上面链接,调试后发现如下:
period # zabbix数据时间范围 stime # 开始时间 itemids[0] # item.key type # 图表类型 profiledx # 项类型 width # 图表宽度 sid # 用户sid
必须的参数有:
"period" : "3600" # 一小时内的性能图 "itemids[0]" : itemID # item的id "type" : "0" # 图表类型 "profileIdx" : "web.item.graph" # 项 "width" : "700" # 宽度
仔细观察这些参数,其实发现变化的只有itemid 其他都是可以不变的,查看官网发现item.ky是在告警的时候可以直接传入的:
https://www.zabbix.com/documentation/3.2/manual/appendix/macros/supported_by_location
→ Trigger-based notifications
→ Internal notifications
意思就是说包含在 {ALERT.MESSAGE} 里面
在Actions动作里面可以自定义传入item.key的宏。具体配置:
我是在actions里将我需要的参数配置进去
actions -> Default message 传入
HOST.HOST1:{HOST.HOST1}|HOST.IP1:{HOST.IP1}|EVENT.DATE:{EVENT.DATE}| EVENT.TIME:{EVENT.TIME}|TRIGGER.SEVERITY:{TRIGGER.SEVERITY}| TRIGGER.NAME:{TRIGGER.NAME}|ITEM.KEY1:{ITEM.KEY1}|ITEM.NAME1:{ITEM.NAME1}| ITEM.VALUE1:{ITEM.VALUE1}|ITEM.ID:{ITEM.ID}|TRIGGER.STATUS:{TRIGGER.STATUS}
配置完成后,zabbix告警的时候会发出item.ky,获取到item.key后就要让脚本接收到参数
D = {} subject = sys.argv[2] J = sys.argv[3].replace("‘","").replace("\"","").replace(" ","").split("|") for i in range(0,len(J)): D[J[i].split(":")[0]] = J[i].split(":")[1] triggerHost = "<strong>告警主机: </strong>%s (%s)" % (D[‘HOST.HOST1‘],D[‘HOST.IP1‘]) triggerTime = "<strong>告警时间: </strong>%s %s" % (D[‘EVENT.DATE‘],D[‘EVENT.TIME‘]) triggerLevel = "<strong>告警等级: </strong>%s" % (D[‘TRIGGER.SEVERITY‘]) triggerName = "<strong>告警信息: </strong> %s" % (D[‘TRIGGER.NAME‘]) triggerKey = "<strong>告警项目: </strong> %s" % (D[‘ITEM.KEY1‘]) triggerText = "<strong>问题详情: </strong>%s %s" % (D[‘ITEM.NAME1‘],D[‘ITEM.VALUE1‘]) triggerItemID = D[‘ITEM.ID‘] if D[‘TRIGGER.STATUS‘] == ‘OK‘: triggerStatus = "<strong>当前状态: </strong><span style=‘font-weight:bold;color:green;‘>%s</span>" % (D[‘TRIGGER.STATUS‘]) mailSubject = ‘<div style="border-bottom:1px #D9D9D9 solid;font-size:18px;line-height:40px;color:#7BB009;">故障恢复</div>‘ else: triggerStatus = "<strong>当前状态: </strong><span style=‘font-weight:bold;color:red;‘>%s</span>" % (D[‘TRIGGER.STATUS‘]) mailSubject = ‘<div style="border-bottom:1px #D9D9D9 solid;font-size:18px;line-height:40px;color:#F00000;">故障通知</div>‘
上面我将参数接收后进行一个处理,处理完这些参数,我们要利用获取到的item来获取性能图:
import requests myRequests = requests.Session() def GetGraph(itemID,pName=None): try: loginUrl = "http://%s/zabbix/index.php" % HOST print loginUrl loginHeaders={ "Host":HOST, "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", } playLoad = { "name":USER, "password":PASSWD, "autologin":"1", "enter":"Sign in", } res = myRequests.post(loginUrl,headers=loginHeaders,data=http://www.mamicode.com/playLoad)"http://%s/zabbix/chart.php" % HOST testUrlplayLoad = { "period" :"3600", "itemids[0]" : itemID, "type" : "0", "profileIdx" : "web.item.graph", "width" : "700", } testGraph = myRequests.get(url=testUrl,params=testUrlplayLoad) IMAGEPATH = os.path.join(‘/tmp‘, pName) f = open(IMAGEPATH,‘wb‘) f.write(testGraph.content) f.close() return pName except Exception as e: print e return False finally: myRequests.close()
这样传入item的参数就可以获取到的图片到本地/tmp目录下。
获取到图片之后,再进行一个邮件构建,就可以完成啦,下面是邮件的构建部分:
mailText = """ <html xmlns="http://www.w3.org/1999/xhtml"> <html><head><meta charset="utf-8" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>$sub</title></head> <body style="margin:0;padding:0;"> <table cellspacing="0" cellpadding="0" width="100%%" style="min-height:69px;border:1px #7BB009 solid;border-top-width:6px;font-family:‘微软雅黑‘,sans-serif;font-size:14px;"> <tbody><tr><td><center><div style="text-align:left;max-width:100%%;min-height:69px;"><div style="float:left;height:69px;"> <img style="border:none;" src="http://www.mamicode.com/%s" height="50" width="200" alt="591" title="591"/> </a></div></div></center></td></tr></tbody></table> <table cellspacing="0" cellpadding="0" width="100%%" style="border:1px #7BB009 solid;border-width:0 1px 0 1px;font-family:‘微软雅黑‘,sans-serif;font-size:13px;"> <tbody><tr><td><center><div style="max-width:100%%;text-align:left;border-bottom:1px #D9D9D9 dashed;padding-bottom:20px;font-size:13px;"> <table width="100%%" cellspacing="0" cellpadding="8" style="font-size:13px;"><tbody><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> %s</td></tr><tr><td> <img src="http://www.mamicode.com/cid:p"></tr><tr><td> </td></tbody></table></td></tr></tbody></table></div></center> </td></tr></tbody></table> <table cellspacing="0" cellpadding="0" width="100%%" style="border:1px #7BB009 solid;border-top:none;font-family:‘微软雅黑‘,sans-serif;font-size:12px;line-height:35px;padding-bottom:30px;"></table></body> </html> """ % (logPng,mailSubject,triggerHost,triggerTime,triggerLevel,triggerName,triggerKey,triggerText,triggerStatus) def SendMail(Recipient,Subject,Text,Img=None): msg = MIMEMultipart(‘alternative‘) msg[‘From‘]= "%s<%s>" % (MAILNAME,MAILUSER) msg[‘Subject‘] = Header(Subject,‘utf-8‘).encode() msg[‘To‘] = Recipient if Img : fp = open(Img,‘rb‘) msgImage = MIMEImage(fp.read()) fp.close() msgImage.add_header(‘Content-ID‘,‘p‘) msg.attach(msgImage) part = MIMEText(Text, ‘html‘, ‘utf-8‘) msg.attach(part) smtp = smtplib.SMTP_SSL(SMTPSERVER,‘465‘) try: smtp.login(MAILUSER,MAILPASS) smtp.sendmail(MAILUSER,Recipient,msg.as_string()) except Exception as e: print e finally: smtp.quit()
最后脚本最前面进行一个判断,判断一分钟内的性能图就不再重复去zabbix上读,直接从本地上读。
然后计划任务再计划清除图片
*/5 * * * * find /tmp -cmin +1 -name ‘zabbix-graph-itemID*‘ -delete
最后效果图:
本文出自 “猫妖的博客” 博客,请务必保留此出处http://maoyao.blog.51cto.com/8253780/1910892
Zabbix告警发送性能图