实现zabbix告警发送性能图主要实现思路:
zabbix告警传入item.ky参数 -> 利用item.key获取当前的性能图 -> 保存到本地 -> 在发送邮件的时候构建邮件文本内容
首先先讲一下zabbix的性能图:
?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>%s</span>" % (D['TRIGGER.STATUS'])
mailSubject = '<div>故障恢复</div>'
else:
triggerStatus = "<strong>当前状态: </strong><span>%s</span>" % (D['TRIGGER.STATUS'])
mailSubject = '<div>故障通知</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=playLoad)
testUrl = "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目录下。
获取到图片之后,再进行一个邮件构建,就可以完成啦,下面是邮件的构建部分: