zabbix之微信企业号报警
参考:
http://www.cnyunwei.com/thread-29593-1-1.html
http://xumingjiang.blog.51cto.com/703960/1885229
第一步:注册微信企业号
注册地址:https://qy.weixin.qq.com/
注册过程:略
第二步:操作企业号
1、通讯录添加企业成员
要提前把成员信息添加进组织部门,必填项+手机号或者微信号,这样别人扫描二维码的时候才能成功关注企业号
这里有两个我们要用到信息,一个组织部门的ID,一个部门成员的账号(账号是自己手动指定的,不同于微信号,最好是字母加数字)
点击修改部门可以获取部门ID,如下:
以下是部门成员账号:
2、应用中心创建应用
我们要在这里创建应用,因为要通过应用发送消息给部门成员
这里要记住一个值,应用ID
选择消息类应用
如下是建好的一个应用:
3、给部门设置管理员
管理员必须事先已经关注了企业号,并是通讯录中的成员
设置--->权限管理---->新建普通管理组
设置完后,我们需要关注两个值,CorpID 和 Secret
4、我们要准备这些东西
一个微信企业号
企业号已经被部门成员关注
企业号里有一个可以发消息的应用 一个授权管理员,可以使用该应用给成员发消息
5、我们要取到这些信息
成员账号
组织部门ID
应用ID
CropID
Secret
第四步:调用微信接口,编写shell脚本
1、原理
调用微信接口需要一个调用接口的凭证:access_token
通过 :CropID 、Secret 才能获取到access_token,但是获取到的token有效期为两分钟
使用:curl -s -G url 获取 AccessToken
使用:curl --data url 传送凭证调用企业号接口
2、shell脚本
[root@localhost alertscripts]# vim wechat.sh #!/bin/bash ######################################################################### # File Name: wechat.sh ######################################################################### # Functions: send messages to wechat app # set variables #申请完企业号可以得到CropID和Secret CropID='wx9ccd6b7ed6bf9e' Secret='KrxkXdZY8PxPjII1dM_7z2bBTLWj6Nio-bjdxdfnkncLJHTBSq6Q021haHnCx' GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret" #get acccess_token Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $4}') PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken" # function body() { local int AppID=1 #企业号中的应用ID local UserID=$1 #部门成员账号,zabbix中定义的微信接收者 local PartyID=2 #部门ID,定义了范围,组内成员都可接收到消息 local Msg=$(echo "$@" | cut -d" " -f3-) #过滤出zabbix传递的第三个参数 printf '{\n' printf '\t"touser": "'"$UserID"\"",\n" printf '\t"toparty": "'"$PartyID"\"",\n" printf '\t"msgtype": "text",\n' printf '\t"agentid": "'" $AppID "\"",\n" printf '\t"text": {\n' printf '\t\t"content": "'"$Msg"\""\n" printf '\t},\n' printf '\t"safe":"0"\n' printf '}\n' } /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
zabbix会传递三个参数给脚本,$1是消息接收账号,$2报警标题,$3报警内容
3、zabbix server端配置
[root@localhost alertscripts]# pwd /usr/local/zabbix/alertscripts [root@localhost alertscripts]# chmod +x wechat.sh [root@localhost alertscripts]# ll total 4 -rwxr-xr-x 1 root root 1335 Dec 30 00:27 wechat.sh
[root@localhost alertscripts]# vim /usr/local/zabbix/etc/zabbix_server.conf #添加报警脚本目录参数 AlertScriptsPath=/usr/local/zabbix/alertscripts
[root@localhost etc]# /etc/init.d/zabbix_server restart Shutting down zabbix_server: [ OK ] Starting zabbix_server: [ OK ]
4、脚本测试
[root@localhost alertscripts]# ./wechat.sh shencj test test1111 {"errcode":0,"errmsg":"ok"}[root@localhost alertscripts]#
第五步:zabbix页面配置
第六步:后续添加报警成员
只需要在通讯录,子部门中新增成员,并且新成员关注企业号即可
转载于:https://blog.51cto.com/732233048/1887667