1. 钉钉报警:
群机器人是钉钉群的高级扩展功能,群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。群机器人支持Webhook协议的自定义接入,支持更多可能性. 比如我们可将Zabbix运维报警提醒通过自定义机器人聚合到钉钉群中,以实现钉钉告警。
1.1 创建群:



1.2 给群添加机器人:






1.3 钉钉告警脚本:
通过脚本调用接口进行告警:
### 创建脚本:
cat > /usr/lib/zabbix/alertscripts/dingding <<EOF
#!/usr/bin/python
#coding:utf-8
import requests,json,sys,os,datetime
# 机器人的Webhook地址
webhook="https://oapi.dingtalk.com/robot/send?access_token=87b9fba6fc1b3b2dc692d22eb74415b2c1018d86099178c6ca58bcf95e799daa"
user=sys.argv[1]
text=sys.argv[3]
data={
"msgtype": "text",
"text": {
"content": text
},
"at": {
"atMobiles": [
# 在web端用户绑定媒介中通过指定用户注册是的手机号,可以通知指定人员,否则只发消息,不艾特具体人
user
],
"isAtAll": False
# 设置为1,则@所有人
# "isAtAll": 1
}
}
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()
EOF
### 安装脚本依赖的模块:
yum install python-pip -y && pip install requests
### 设置权限:
chmod +x /var/lib/zabbixsrv/alertscripts/dingding
1.4 web端创建报警媒介:
点击管理员--媒介类型--创建媒介:




1.5 用户绑定媒介:
点击管理员--选择用户--选择admin:


1.6 编辑动作:
把动作中的邮件告警改为钉钉告警即可:
点击配置--动作:





1.7 测试告警:
### 将122服务器的redis服务关闭:
[root@localhost ~]# systemctl stop redis


### 测试恢复告警:
[root@localhost ~]# systemctl start redis

1.8 @某人告警:
如果想提醒某人,可以在用户绑定媒介那里,设置电话号码实现:

设置好后,故障通知会直接@某人:

1.9 @全员告警:
如果想@全员告警,可以修改脚本:

修改之后,再次告警就是@群内所有人了:

经过简单的设置,钉钉告警就完成了,zabbix 5添加了很多告警类型,都可以去尝试一下。
欢迎点赞,收藏,你的喜欢就是我原创的动力,获取最新文章更新,以及常用软件,可以关注公众号: 运维朱工
网友评论