更新 'zabbix-md/zabbix钉钉报警.md'

This commit is contained in:
diandian 2023-05-18 10:47:20 +08:00
parent 43aef6a0a6
commit c94db5fae6
1 changed files with 246 additions and 246 deletions

View File

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