ZABBIX4.0 微信告警

使用微信告警,需要用到企业微信中的应用功能,类似于钉钉报警的小机器人,它会像公众号一样的,推送消息

1.首先申请一个微信公众号: https://qy.weixin.qq.com/

2.然后自建一个应用程序

 需要记录以下信息:

①Agentld:1000002

② Secret:0U4JRxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

zabbix-server服务端配置脚本

查找zabbix_server.conf配置文件位置,修改(已存在可不修改)

 然后进入该目录,配置脚本

[root@zabbix alertscripts]# cd /data/findsec/zabbix/share/zabbix/alertscripts
[root@zabbix alertscripts]# ls
sendmail.sh  wechat.sh
[root@zabbix alertscripts]# cat wechat.sh
#!/bin/bash
###SCRIPT_NAME:wechat.sh###
###send message from weixin for zabbix monitor###

CropID='wwb2146de7799db779'   #Agentld ID号
Secret='0U4JRmxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'   Secret ID号
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F" '{print $10}')

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

function body() {
        #企业号中的应用id
        local int AppID=1000002   #此项不明,我这里写的1000002也能成功
        #部门成员id,zabbix中定义的微信接收者
        local UserID=$1
        #部门id,定义了范围,组内成员都可接收到消息
        local PartyID=2
        #过滤出zabbix传递的第三个参数
        local Msg=$(echo "$@" | cut -d" " -f3-)
        printf '{
'
        printf '	"touser": "'"$UserID""",
"
        printf '	"toparty": "'"$PartyID""",
"
        printf '	"msgtype": "text",
'
        printf '	"agentid": "'" $AppID """,
"
        printf '	"text": {
'
        printf '		"content": "'"$Msg"""
"
        printf '	},
'
        printf '	"safe":"0"
'
        printf '}
'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL##

赋予脚本权限

[root@zabbix alertscripts]# chown zabbix.zabbix wechat.sh
[root@zabbix alertscripts]# chmod 744 wechat.sh

配置完成后,无需重启server服务

直接测试

zhen test test^C
[root@zabbix alertscripts]# ./wechat.sh dongweizhen test test
{"errcode":0,"errmsg":"ok. Warning: wrong json format. ","invaliduser":""}

再查看手机企业微信,发现已经有了信息

接下来的步骤就是跟配置邮箱告警类似了

 {ALERT.SENDTO}

 {ALERT.SUBJECT}

 {ALERT.MESSAGE}

添加一个用户,群组很重要,关系到监控主机

 添加用户的报警媒介,这里的收件人需要和脚本中定义的收件人对应,都为1

 需要注意的是用户组ops中有GIS主机组,如果没有对GIS主机组有读写的权限,告警信息是发送不出去的

 

到这里,就配置结束了,模拟告警,可以看到企业微信中自己创建的应用程序返回信息了

 

嗨~如果有帮助,请帮忙点个赞吧,谢谢 -致敬每一个正在努力的人
原文地址:https://www.cnblogs.com/dongweizhen/p/14203771.html