环境
2.查看脚本执行目录,并在目录下创建如下脚本 vim /usr/lib/zabbix/alertscripts/dingding.py#!/usr/bin/env python#coding:utf-8#zabbix钉钉报警import requests,json,sys,os,datetimewebhook="上面创建钉钉机器人的webhook地址"user=sys.argv[1]text=sys.argv[3]data={ "msgtype": "text", "text": { "content": text }, "at": { "atMobiles": [ user ], "isAtAll": False }}headers = {'Content-Type': 'application/json'}x=requests.post(url=webhook,data=json.dumps(data),headers=headers)if os.path.exists("/tmp/zabbix_dingding.log"): f=open("/tmp/zabbix_dingding.log","a+")else: f=open("/tmp/zabbix_dingding.log","w+")f.write("\n"+"--"*30)if x.json()["errcode"] == 0: f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text)) f.close()else: f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text)) f.close()vim /usr/lib/zabbix/alertscripts/dingding.py#!/usr/bin/env python#coding:utf-8#zabbix钉钉报警import requests,json,sys,os,datetimewebhook="上面创建钉钉机器人的webhook地址"user=sys.argv[1]text=sys.argv[3]data={ "msgtype": "text", "text": { "content": text }, "at": { "atMobiles": [ user ], "isAtAll": False }}headers = {'Content-Type': 'application/json'}x=requests.post(url=webhook,data=json.dumps(data),headers=headers)if os.path.exists("/tmp/zabbix_dingding.log"): f=open("/tmp/zabbix_dingding.log","a+")else: f=open("/tmp/zabbix_dingding.log","w+")f.write("\n"+"--"*30)if x.json()["errcode"] == 0: f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text)) f.close()else: f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text)) f.close() 3.赋予脚本可执行权限:chmod +x /usr/lib/zabbix/alertscripts/dingding.py 踩过的坑:切换到/usr/lib/zabbix/alertscripts 目录下 chmod -x dingding.py还是纯文本 测试脚本可用性方法:python dingding.py 18710140128 "这是一条测试文字" 脚本使用文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq
至此,监控警报工程已完工。 技术交流欢迎加入Q群:177428068 |
5.png (0 Bytes, 下载次数: 0)
错误信息