Zabbix-3.2.3实现微信(WeChat)告警
zabbix
Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告警的及时处理。
关于邮件报警可以参考:Zabbix Web 邮件报警
一、微信企业号申请
提示:这里简单的说一下,微信企业号和微信公众号是不一样的!
二、配置微信企业号
设置完成之后如下图所示!
提示:我们需要记录应用ID,在接收邮件时会使用
需要确定管理员有权限使用应用发送消息,需要管理员的CorpID和Sercrt。(重要)
准备事项:
微信企业号
企业号已经被部门成员关注
企业号有一个可以发送消息的应用,一个授权管理员,可以使用应用给成员发送消息
需要得到的信息
- 成员账号
- 组织部门ID
- 应用ID
- CorpID和Secret
三、修改Zabbix.conf
- [root@abcdocker ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
- AlertScriptsPath=/usr/lib/zabbix/alertscripts
- 我们设置zabbix默认脚本路径,这样在web端就可以获取到脚本
四、设置python脚本
#安装simplejson
- wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
- tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
- python setup.py build
- python setup.py install
下载wechat.py脚本
- git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
- cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/
- cd /usr/lib/zabbix/alertscripts/
- chmod +x wechat.py && chown zabbix:zabbix wechat.py
提示:这里需要修改py脚本
看注释,这就不解释了
- [root@abcdocker ~]# cat /usr/lib/zabbix/alertscripts/wechat.py
- #!/usr/bin/python
- #_*_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":user, #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
- "toparty":"2", #企业号中的部门id。
- "msgtype":"text", #消息类型。
- "agentid":"2", #企业号中的应用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 = '11111111111111' #CorpID是企业号的标识
- corpsecret = '222222222222222222' #corpsecretSecret是管理组凭证密钥
- accesstoken = gettoken(corpid,corpsecret)
- senddata(accesstoken,user,subject,content)
执行py脚本,进行测试
- [root@abcdocker alertscripts]# ./wechat.py www www 123
- https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx6dadb9cc293b793e&corpsecret=JjesoeixbFt6dDur7_eXtamVBx2SjPBuXMQ0Jte3YLkz8l-VBnr0JvU12P0kvpGJ
- {u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'}
五、zabbix web 界面配置
报警消息设置如下:
- hostname: ({HOST.NAME}
- Time:{EVENT.DATE} {EVENT.TIME}
- level:{TRIGGER.SEVERITY}
- message:{TRIGGER.NAME}
- event:{ITEM.NAME}:{ITEM.VALUE}
- url:www.abcdocker.com
恢复报警如下:
- hostname: ({HOST.NAME}
- Time:{EVENT.DATE} {EVENT.TIME}
- level:{TRIGGER.SEVERITY}
- message:{TRIGGER.NAME}
- event:{ITEM.NAME}:{ITEM.VALUE}
- url:www.abcdocker.com
提示: 不要忘记先点小的add–>小的update–>Update
六、测试
为了验证效果我们停掉zabbix-agent,进行查看报警
- [root@abcdocker ~]# systemctl stop zabbix-agent
本文参考:
Zabbix-3.0.3实现微信(WeChat)告警
以及强哥的技术支持
更多Zabbix文章请访问我们ZABBIX板块
ZABBIX板块
原创文章,作者:abcdocker,如若转载,请注明出处:http://www.178linux.com/73828