1 #!/usr/bin/env python 2 #coding:utf-8 3 #zabbix钉钉报警 4 import requests,json,sys,os,datetime 5 webhook="https://oapi.dingtalk.com/robot/send?access_token=52c39155fcb9f2fecda75ef8cec6d2ffa318e4818982f772ecb9f779a630151d" 6 user=sys.argv[1] 7 text=sys.argv[3] 8 data={ 9 "msgtype": "text", 10 "text": { 11 "content": text 12 }, 13 "at": { 14 "atMobiles": [ 15 user 16 ], 17 "isAtAll": False 18 } 19 } 20 headers = {‘Content-Type‘: ‘application/json‘} 21 x=requests.post(url=webhook,data=json.dumps(data),headers=headers) 22 if os.path.exists("/usr/local/zabbix/logs/dingding.log"): 23 f=open("/usr/local/zabbix/logs/dingding.log","a+") 24 else: 25 f=open("/usr/local/zabbix/logs/dingding.log","w+") 26 f.write("\n"+"--"*30) 27 if x.json()["errcode"] == 0: 28 f.write("\n"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"\n"+str(text)) 29 f.close() 30 else: 31 f.write("\n"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "\n" + str(text)) 32 f.close()
原文地址:https://www.cnblogs.com/zhj5551/p/11198227.html
时间: 2024-11-05 09:19:29