#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests,time
import json,sys,re,os
zabbixserver_url ='http://192.168.99.200/index.php'
#定义远端的web服务器地址,将图片复制到远端的web目录下
pname_path='http://47.103.15.51/dingding_pic/'
#定义获取的图片地址
testUrl = "http://192.168.99.200/chart.php"
host='192.168.99.200'
def get_itemid():
itemid=re.search(r'ITEM ID:(\d+)',sys.argv[2]).group(1)
return itemid
def get_picture(itemid,pname):
#构建session,或者可以一次构建之后使用cookie登录
myRequests = requests.Session()
try:
loginHeaders = {
"Host":host,
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"
}
# 构建登录所需的信息
playLoad = {
"name": "Admin",
"password": 'zabbix',
"autologin": "1",
"enter": "Sign in",
}
myRequests.post(url=zabbixserver_url, headers=loginHeaders, data=playLoad)
testUrlplayLoad = {
"from": "now-10m",
"to": "now",
"itemids": itemid,
"width": "700",
}
testGraph = myRequests.get(url=testUrl,params=testUrlplayLoad)
IMAGEPATH = os.path.join('/usr/lib/zabbix/alertscripts/dingding_pic/', pname)
#将获取到的图片数据写入到文件中去
with open(IMAGEPATH,'wb') as f:
f.write(testGraph.content)
os.system("sudo scp %s root@47.103.15.51:/usr/share/nginx/html/dingding_pic" %IMAGEPATH)
pname_url = pname_path+pname
return pname_url
except Exception as e:
print(e)
return False
#构造发送消息的请求
def send_msg(pname_url,info3):
headers = {'Content-Type': 'application/json;charset=utf-8'}
print(info3)
data = {
"msgtype": "markdown",
"markdown": {
"title": info1,
"text": "## 通知:\n"+info3+"![screenshot](%s)\n"%(pname_url)
},
"at":{
"atMobiles": reminders,
"isAtAll": False,
},
}
r = requests.post(url=webhook_url,json=data,headers=headers)
print(r.text)
#对报警信息进行格式化
def info_text():
new_text = ""
x = info2.split('\n')
for i in x:
if re.search('ITEM ID',str(i)):
pass
else:
new_text+="- "+str(i)+('\n')
print(type(new_text))
return new_text
if __name__ == '__main__':
#将报警信息写入日志
os.system("echo hello >> /tmp/syslog.md")
pname = str(int(time.time()))+'.png'
info1 = str(sys.argv[1])
info2 = str(sys.argv[2])
info3 = info_text()
with open('/tmp/syslog.md','a') as f:
f.write(info1)
f.write(info2)
f.close()
reminders = []
webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=771ded387e6be652c51a2b6c83cade4e048e3da4fdfe128f1db6b124b87df18a'
itemid = get_itemid()
pname_url=get_picture(itemid,pname)
print(pname_url)
send_msg(pname_url,info3)
点击配置菜单中的动作,创建动作,然后根据图片进行填写
操作
默认标题 Zabbix告京:服务器:{HOSTNAME}发生: {TRIGGER.NAME}故障!
ITEMID:{ITEM.ID}
告警主机:{HOST.NAME}
告警主机:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}
恢复操作
Zabbix告京:服务器:{HOST.NAME}发生: {TRIGGER.NAME}已恢复!
ITEMID:{ITEM.ID}
告警主机:{HOST.NAME}
告警主机:{HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE}
事件ID:{EVENT.ID}
可以手动触发一个报警测试效果
欢迎关注个人公号“没有故事的陈师傅”
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。