脚本如下
#!/usr/bin/python
#-*- coding: utf-8 -*-
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=712c3d89c613a3cce737bd1ed5ce8c7b9ebd5ee48c115236873b2a2a54c5efcf"
#说明:这里改为自己创建的机器人的webhook的值
user=sys.argv[1]
#发给钉钉群中哪个用户
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
user
],
"isAtAll": False
}
}
#钉钉API固定数据格式
headers = {'Content-Type': 'application/json'}
x=requests.post(url=webhook,data=json.dumps(data),headers=headers)
if os.path.exists("/usr/lib/zabbix/alertscripts/dingding.log"):
f=open("/usr/lib/zabbix/alertscripts/dingding.log","a+")
else:
f=open("/usr/lib/zabbix/alertscripts/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()
故障名称:{EVENT.NAME}
服务器:{HOST.NAME} 发生:{TRIGGER.NAME}故障! 告警主机:{HOST.NAME} 监控项目:{ITEM.NAME} 监控取值:{ITEM.LASTVALUE} 告警等级:{TRIGGER.SEVERITY} 当前状态:{TRIGGER.STATUS} 告警信息:{TRIGGER.NAME} 告警时间:{EVENT.DATE} {EVENT.TIME} 事件ID:{EVENT.ID}