钉钉报警
一:设置钉钉机器人
二:zabbix服务器server端配置
1.修改zabbix_server.conf文件
[root@server ~]# vim /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/usr/local/zabbix/logs/zabbix_server.log
PidFile=/tmp/zabbix_server.pid
DBHost=192.168.200.11
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/var/lib/mysql/mysql.sock
Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf
AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts #设置钉钉获取脚本路径
2.安装python环境
[root@server ~]# yum -y install python-requests python
3.编写钉钉报警脚本
[root@server ~]# vim /usr/local/zabbix/share/zabbix/alertscripts/dingding.by
#!/usr/bin/env python
#coding:utf-8
#zabbix钉钉报警
import requests,json,sys,os,datetime
webhook="https://oapi.dingtalk.com/robot/send?access_token=6d0a602e6a20fc5b8ef61a6b10c4fbca4
15a33b7b8aead41b5217555ad9587d3" #添加之前复制的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("
"+"--"*30)
if x.json()["errcode"] == 0:
f.write("
"+str(datetime.datetime.now())+" "+str(user)+" "+"发送成功"+"
"+str(te
xt))
f.close()
else:
f.write("
"+str(datetime.datetime.now()) + " " + str(user) + " " + "发送失败" + "
" + str(text))
f.close()
4.给脚本设置权限及归属组
[root@server ~]# chown zabbix:zabbix /usr/local/zabbix/share/zabbix/alertscripts/dingding.by #设置归属组
[root@server ~]# chmod +x /usr/local/zabbix/share/zabbix/alertscripts/dingding.by #加执行权限
[root@server ~]# killall -9 zabbix_server #杀死zabbix_server服务
[root@server ~]# zabbix_server #开启zabbix_server服务
三:web页面设置
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
三个都做同样的配置(可以之做一个配置)
配置模板
标题:故障{TRIGGER.STATUS}, 服务器:{HOSTNAME1}发生:{TRIGGER.NAME}故障
内容:告警信息:{TRIGGER.NAME}
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
#模拟ftp故障
[root@agent ~]# systemctl stop vsftpd
[root@agent ~]# systemctl start vsftpd