You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

247 lines
5.5 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<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)