系统环境:Centos 7.3
软件版本:Zabbix3.0
一、企业微信号注册和使用
企业号注册:https://work.weixin.qq.com/
二、企业号使用教程
- 添加员工通讯录
登录新建的企业号,通过提前把企业成员信息添加到组织或者部门,需要填写手机号、微信号或邮箱,通过这样方式让别人扫码关注企业公众号,为了后面企业号推送消息给企业成员。
新增账户,填写信息,红框部分是必须填写的
三、创建应用
企业应用 —-> 自定义应用 —-> 创建应用
四、设置管理员权限
我的企业—->权限管理—->新建管理组
管理员需要事先关注企业号,并且设置好邮箱地址
需要确定管理员有权限使用应用发送消息,需要管理员的CorpID和Sercrt。(重要)
CorpID获取:通过PC端登陆企业微信,点击我的企业—->企业信息
Sercrt获取:点击企业应用—>自建应用中的zabbix_test
准备事项:
(1)微信企业号
(2)企业号已经被部门成员关注
(3)企业号有一个可以发送消息的应用(zabbix_test),一个授权管理员(zabbix_test),可以使用应用给成员发送消息
(4)需要先添加管理员信息,然后使其关注企业号
(5)需要得到的信息:
①成员账号
②组织部门ID
③应用ID
④CorpID和Secret
五、微信接口调用
调用微信接口需要一个调用接口的凭证:access_token
通过CorpID和Secret可以获得access_token
微信企业号接口调试地址: http://qydev.weixin.qq.com/debug
六、设置脚本
- shell脚本
编辑shell脚本:
[root@172 alertscripts]# vim wechat.sh #!/bin/bash ######################################################################### # File Name: wechat.sh # Author: shaonbean # Email: shaonbean@qq.com # Created Time: Sun 24 Jul 2016 05:48:14 AM CST ######################################################################### # Functions: send messages to wechat app # set variables CropID='wwe7701d61234baa94' Secret='2zgBmO3FiZHUde1m0RKlaasffweMgDJO09ZkMgwRMysw' 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=1000002 #企业号中的应用id local UserID="devops" #部门成员id,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 $! $2 $3)" $PURL
获取 AccessToken:
curl -s -G url
传送凭证调用企业号接口:
curl --data url
测试shell脚本:
[root@172 alertscripts]# bash wechat.sh test hello.world! {"errcode":0,"errmsg":"ok","invaliduser":"all user invalid"}
2.Python脚本
安装simplejson:
pip install simplejson
编辑Python脚本:
[root@172 alertscripts]# vim wechat.py #!/usr/bin/env python2.7 #_*_coding:utf-8 _*_ import urllib,urllib2 import json import sys import simplejson reload(sys) sys.setdefaultencoding('utf-8') def gettoken(corpid,corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret print gettoken_url try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print e.read().decode("utf8") sys.exit() token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token def senddata(access_token,user,subject,content): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token send_values = { "touser":"devops", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。 "toparty":"2", #企业号中的部门id。 "msgtype":"text", #消息类型。 "agentid":"1000002", #企业号中的应用id。 "text":{ "content":subject + '\n' + content }, "safe":"0" } # send_data = json.dumps(send_values, ensure_ascii=False) send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8') send_request = urllib2.Request(send_url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) print str(response) if __name__ == '__main__': user = str(sys.argv[1]) #zabbix传过来的第一个参数 subject = str(sys.argv[2]) #zabbix传过来的第二个参数 content = str(sys.argv[3]) #zabbix传过来的第三个参数 corpid = 'wwe7701d6saa98baa94' #CorpID是企业号的标识 corpsecret = '2zgBmO3FiZHUde1m0RKlaAhsfwsDJO09ZkMgwRMysw' #corpsecretSecret是管理组凭证密钥 accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content)
测试python脚本:
[root@zabbix alertscripts]# ./wechat.py test-msg test hello https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwe7701d6saa98baa94&2zgBmO3FiZHUde1m0RKlaAhsfwsDJO09ZkMgwRMysw {u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'}
3.设置脚本权限:
将脚本放到zabbix默认执行路径下,并设置权限
[root@172 ~]# mv wechat.py wechat.sh /usr/local/zabbix/share/zabbix/alertscripts/ [root@172 ~]# chown zabbix:zabbix /usr/local/zabbix/share/zabbix/alertscripts/wechat.py [root@172 ~]# chmod 755 /usr/local/zabbix/share/zabbix/alertscripts/wechat.py [root@172 ~]# chown zabbix:zabbix /usr/local/zabbix/share/zabbix/alertscripts/wechat.sh [root@172 ~]# chmod 755 /usr/local/zabbix/share/zabbix/alertscripts/wechat.sh
编辑zabbix_server.conf文件,添加以下内容
[root@172 ~]# vim /usr/local/zabbix/etc/zabbix_server.conf AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts
重启zabbix_server
[root@172 ~]# /etc/init.d/zabbix_server restart
七、Zabbix-web前端设置
- 设置通知媒介
点击Administration —-> Media types —->Create media type 填写相应信息
2.创建用户
点击Administration —-> Users —->Create user 填写相应信息
3.创建触发动作以及告警内容:
点击 Configuration —-> Actions —-> Create action 告警内容
设置触发动作:
告警主题:
告警节点: {TRIGGER.HOSTGROUP.NAME} 告警主机:{HOST.NAME} 告警等级:{TRIGGER.SEVERITY} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 告警时间:{EVENT.DATE} {EVENT.TIME} Original event ID: {EVENT.ID}
恢复主题:
告警节点:{TRIGGER.HOSTGROUP.NAME} 告警主机:{HOST.NAME} 告警等级:{TRIGGER.SEVERITY} 告警项目:{TRIGGER.KEY1} 问题详情:{ITEM.NAME}:{ITEM.VALUE} 当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1} 恢复时间:{EVENT.DATE} {EVENT.RECOVERY.TIME} Original event ID: {EVENT.ID}
八、测试
主动触发相关trigger告警,查看微信发送状态:
到此Zabbix-3.0.结合微信告警就完成了。
注意事项及报错处理:
Zabbix-web页面新增用户权限处理,发送对象选择(应用ID)
应用当中可见范围选择注意(选这要发送的对象(部门,及部门成员))