|
|
<h1><center>Zabbix钉钉报警</center></h1>
|
|
|
|
|
|
------
|
|
|
|
|
|
**作者:行癫(盗版必究)**
|
|
|
|
|
|
## 一:环境准备
|
|
|
|
|
|
#### 1.监控环境
|
|
|
|
|
|
zabbix-server正常运行,并且正常对zabbix-agent进行监控
|
|
|
|
|
|
#### 2.注册钉钉
|
|
|
|
|
|
![image-20230228204528919](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228204528919.png)
|
|
|
|
|
|
安装钉钉PC端,注册账户,添加好友(至少3好友)
|
|
|
|
|
|
创建群组
|
|
|
|
|
|
![image-20230228204728705](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228204728705.png)
|
|
|
|
|
|
![image-20230228204804255](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228204804255.png)
|
|
|
|
|
|
![image-20230228204900830](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228204900830.png)
|
|
|
|
|
|
创建机器人
|
|
|
|
|
|
![image-20230228205012078](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228205012078.png)
|
|
|
|
|
|
![image-20230228205045700](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228205045700.png)
|
|
|
|
|
|
![image-20230228205114319](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228205114319.png)
|
|
|
|
|
|
![image-20230228205155984](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228205155984.png)
|
|
|
|
|
|
注意:保存webhook
|
|
|
|
|
|
#### 3.python3环境
|
|
|
|
|
|
安装python3
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian ~]# yum -y install python3
|
|
|
[root@xingdian ~]# yum -y install python-pip
|
|
|
```
|
|
|
|
|
|
更换pip源
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian ~]# mkdir ~/.pip
|
|
|
[root@xingdian ~]# vim ~/.pip/pip.conf
|
|
|
[global]
|
|
|
index-url=http://mirrors.aliyun.com/pypi/simple/
|
|
|
|
|
|
[install]
|
|
|
trusted-host=mirrors.aliyun.com
|
|
|
```
|
|
|
|
|
|
安装requests模块
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian ~]# pip3 install --upgrade pip
|
|
|
[root@xingdian ~]# pip3 install requests
|
|
|
```
|
|
|
|
|
|
#### 4.配置监控脚本
|
|
|
|
|
|
创建钉钉脚本
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian ~]# cd /usr/lib/zabbix/alertscripts/
|
|
|
[root@xingdian alertscripts]# vim dingding.py
|
|
|
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
#-*- coding: utf-8 -*-
|
|
|
|
|
|
#zabbix钉钉报警
|
|
|
import requests,json,sys,os,datetime
|
|
|
|
|
|
webhook="https://oapi.dingtalk.com/robot/send?access_token=feb43aea482d9da72e781d92dbfc074f701642a166f31194ff347954f500a404"
|
|
|
|
|
|
#说明:这里改为自己创建的机器人的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("/var/log/zabbix/dingding.log"):
|
|
|
|
|
|
f=open("/var/log/zabbix/dingding.log","a+")
|
|
|
|
|
|
else:
|
|
|
|
|
|
f=open("/var/log/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()
|
|
|
|
|
|
#将发送的告警信息写入本地日志/var/log/zabbix/dingding.log中
|
|
|
|
|
|
[root@xingdian alertscripts]# chmod +x dingding.py
|
|
|
```
|
|
|
|
|
|
创建日志文件
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian alertscripts]# touch /var/log/zabbix/dingding.log
|
|
|
[root@xingdian alertscripts]# chown zabbix.zabbix /var/log/zabbix/dingding.log
|
|
|
```
|
|
|
|
|
|
测试命令
|
|
|
|
|
|
```shell
|
|
|
[root@xingdian alertscripts]# ./dingding.py xingdian test "这是条告警测试信息,请忽略"
|
|
|
```
|
|
|
|
|
|
参数说明
|
|
|
|
|
|
xingdian:钉钉群中任意一个人员的钉钉号
|
|
|
|
|
|
test:这里因脚本中没有设置接收参数,所以无实际意义,仅仅起到变量占位的作用
|
|
|
|
|
|
"这是条告警测试信息,请忽略":这是具体的告警信息,告警信息必须包含“安全设置”中自己设置的“自定义关键词”
|
|
|
|
|
|
![image-20230228210234266](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230228210234266.png)
|
|
|
|
|
|
## 二:配置Zabbix监控
|
|
|
|
|
|
#### 1.创建主机群组
|
|
|
|
|
|
#### 2.创建主机
|
|
|
|
|
|
#### 3.创建监控项
|
|
|
|
|
|
(监控nginx服务是否正常运行)
|
|
|
|
|
|
#### 4.创建触发器
|
|
|
|
|
|
#### 5.创建报警媒介
|
|
|
|
|
|
脚本参数:
|
|
|
|
|
|
```shell
|
|
|
{ALERT.SENDTO}
|
|
|
#对应脚本中的,user=sys.argv[1](发给钉钉群中哪个用户)
|
|
|
{ALERT.SUBJECT}
|
|
|
#代表发送的信息的标题,在"报表"的"动作日志"中可以看到
|
|
|
{ALERT.MESSAGE}
|
|
|
#对应脚本中的,text=sys.argv[3](发送的报警内容)
|
|
|
```
|
|
|
|
|
|
#### 6.创建动作
|
|
|
|
|
|
关联触发器
|
|
|
|
|
|
添加操作
|
|
|
|
|
|
```shell
|
|
|
"主题"对应"钉钉告警"媒介中的:{ALERT.SUBJECT}
|
|
|
"消息"对应"钉钉告警"媒介中的:{ALERT.MESSAGE}
|
|
|
|
|
|
主题:
|
|
|
|
|
|
故障名称(触发器名称):{EVENT.NAME}
|
|
|
|
|
|
消息:
|
|
|
|
|
|
告警主机:{HOSTNAME1}
|
|
|
告警时间:{EVENT.DATE} {EVENT.TIME}
|
|
|
告警等级:{TRIGGER.SEVERITY}
|
|
|
告警信息: {TRIGGER.NAME}
|
|
|
告警项目:{TRIGGER.KEY1}
|
|
|
问题详情:{ITEM.NAME}:{ITEM.VALUE}
|
|
|
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
|
|
|
事件ID:{EVENT.ID}
|
|
|
```
|
|
|
|
|
|
添加恢复操作
|
|
|
|
|
|
```shell
|
|
|
主题:故障恢复:{EVENT.NAME}
|
|
|
|
|
|
消息:
|
|
|
|
|
|
恢复主机:{HOSTNAME1}
|
|
|
恢复时间:{EVENT.DATE} {EVENT.TIME}
|
|
|
主机IP:{HOST.IP}
|
|
|
告警项目:{TRIGGER.KEY1}
|
|
|
问题详情:{ITEM.NAME}:{ITEM.VALUE}
|
|
|
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
|
|
|
事件ID:{EVENT.ID}
|
|
|
```
|
|
|
|
|
|
#### 7.用户关联报警媒介
|
|
|
|
|
|
#### 8.模拟报警产生
|
|
|
|
|
|
#### 9.查看报警信息
|
|
|
|
|
|
![image-20230301115458160](https://xingdian-image.oss-cn-beijing.aliyuncs.com/xingdian-image/image-20230301115458160.png) |