一、添加钉钉机器人
1.
2.
复制webhook后面脚本用到:https://oapi.dingtalk.com/robot/send?access_token=36e69dd50bbcc54b7b2a84abe1b452dceb54f1daeca83c185c357662c8037692
二、Zabbix设置
1.查看服务端脚本存放路径
[root@GYQ-Prod-Zabbix ~]# cat /etc/zabbix/zabbix_server.conf|grep AlertScriptsPath
### Option: AlertScriptsPath
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/usr/lib/zabbix/alertscripts
登录到/usr/lib/zabbix/alertscripts下编写dingding.py脚本(脚本中包含钉钉机器人的webhook值):
[root@GYQ-Prod-Zabbix ~]# cd /usr/lib/zabbix/alertscripts
[root@zabbix01 monitor_scripts]# cat dingding.py
[root@GYQ-Prod-Zabbix alertscripts]# cat dingding.py
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉报警
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=36e69dd50bbcc54b7b2a84abe1b452dceb54f1daeca83c185c357662c8037692"
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("/usr/lib/zabbix/logs/dingding.log"):
f=open("/usr/lib/zabbix/logs/dingding.log","a+")
else:
f=open("/usr/lib/zabbix/logs/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()
创建上面脚本中的日志路径:
[root@GYQ-Prod-Zabbix alertscripts]# mkdir /usr/lib/zabbix/logs
[root@GYQ-Prod-Zabbix alertscripts]# touch /usr/lib/zabbix/logs/dingding.log
[root@GYQ-Prod-Zabbix alertscripts]# chown zabbix.zabbix dingding.py
[root@GYQ-Prod-Zabbix alertscripts]# chmod 755 dingding.py
[root@GYQ-Prod-Zabbix alertscripts]# chown zabbix.zabbix /usr/lib/zabbix/logs/dingding.log
手动测试脚本发信是否正常:
[root@GYQ-Prod-Zabbix alertscripts]# ./dingding.py test 15377937463 "hehe"
===========================================================
注意:该脚本要求至少python2.6.6版本,且具有requests模块.
否则会报错:
Traceback (most recent call last):
File "./dingding.py", line 4, in <module>
import requests,json,sys,os,datetime
ImportError: No module named requests
解决办法:
yum安装
[root@zabbix monitor_scripts]# yum install python-pip
[root@zabbix monitor_scripts]# pip -v
[root@zabbix monitor_scripts]# pip install requests
===========================================================
源码安装
1.下载:
在它的官网可以下载到安装包:
https://pypi.python.org/pypi/setuptools
下载路径:
#wget --no-check-certificate https://pypi.python.org/packages/source/s/setuptools/setuptools-12.0.3.tar.gz#md5=f07e4b0f4c1c9368fcd980d888b29a65
2. 安装
# tar xvf setuptools-12.0.3.tar.gz
# cd setuptools-12.0.3
# python setup.py install
# cd pip-18.0
# python setup.py install
三、设置zabbix报警媒介
1.添加报警媒介类型
脚本参数添加3个:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
2.设置用户告警
3.添加告警动作
4.
1、报警
{TRIGGER.STATUS}: {TRIGGER.NAME}
{TRIGGER.NAME} 故障!
告警主机:{HOST.NAME}
告警IP:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID} 2、报警恢复
{TRIGGER.STATUS}: {TRIGGER.NAME}
{TRIGGER.NAME} 已恢复!
告警主机:{HOST.NAME}
告警IP:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
恢复时间:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
持续时间:{EVENT.AGE}
事件ID:{EVENT.ID} 3、报警确认
服务器:{HOST.NAME}: 报警确认
确认人:{USER.FULLNAME}
时间:{ACK.DATE} {ACK.TIME}
确认信息如下:
"{ACK.MESSAGE}"
问题服务器IP:{HOSTNAME1}
问题ID:{EVENT.ID}
当前的问题是: {TRIGGER.NAME}
结果: