Bootstrap

zabbix告警使用钉钉通知

1、钉钉群创建机器人

        先在钉钉群中创建自定义机器人,复制保存webhook

2、zabbix服务器安装python模块

yum install python3 python3-requests
pip install requests

3、编写钉钉通知程序

        在/usr/lib/zabbix/alertscripts目录中创建dingding.py脚本
        cd /usr/lib/zabbix/alertscripts/
        vi dingding.py

        增加权限

        touch /tmp/zabbix_dingding.log
        chmod +x /tmp/zabbix_dingding.log
        chown zabbix.zabbix /tmp/zabbix_dingding.log
        chown zabbix.zabbix /usr/lib/zabbix/alertscripts/dingding.py
        chmod +x dingding.py

代码如下:

#!/usr/bin/env python3
#coding:utf-8
#zabbix钉钉报警

import requests
import json
import sys
import os
import datetime

webhook="https://oapi.dingtalk.com/robot/send?access_token=你的webhook"
user=sys.argv[1]
text=sys.argv[2]#告警信息
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()

4、设置zabbix配置

在zabbix_server.conf中配置告警脚本的路径:

AlertScriptsPath=/usr/lib/zabbix/alertscripts

5、zabbix的web界面设置

       a. 报警媒介类型设置,参数:

                {ALERT.SENDTO}-------#发送给谁

                {ALERT.MESSAGE}-------#消息内容

 

        b. 动作设置:

                消息:
                告警主机:{HOSTNAME1}
                告警地址:{HOST.IP}
                告警时间:{EVENT.DATE} {EVENT.TIME}
                告警等级:{TRIGGER.SEVERITY}
                告警信息: {TRIGGER.NAME}
                告警项目:{TRIGGER.KEY1}
                问题详情:{ITEM.NAME}:{ITEM.VALUE}
                当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
                事件ID:{EVENT.ID}

 

        c. 用户设置:

;