摘自abcdocker网站

原文地址:https://www.abcdocker.com/abcdocker/2472

Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告警的及时处理。

http://wuhf2015.blog.51cto.com/8213008/1688614

写的很详细,但我好像有步骤错误没有成功

一、微信企业号申请

https://qy.weixin.qq.com/

提示:这里简单的说一下,微信企业号和微信公众号是不一样的!

 

二、配置微信企业号

需要确定管理员有权限使用应用发送消息,需要管理员的CorpID和Sercrt。(重要)

 

准备事项:

微信企业号 
企业号已经被部门成员关注 
企业号有一个可以发送消息的应用,一个授权管理员,可以使用应用给成员发送消息

 

需要得到的信息

 
  1. 成员账号
  2. 组织部门ID
  3. 应用ID
  4. CorpIDSecret
 

三、修改Zabbix.conf

 
# grep alertscripts /etc/zabbix/zabbix_server.conf
AlertScriptsPath=/usr/lib/zabbix/alertscripts
我们设置zabbix默认脚本路径,这样在web端就可以获取到脚本

 

 

四、设置python脚本

#安装simplejson

 (这里是安装python的一个模块,不然会执行失败)
#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脚本 
看注释,这就不解释了

 
 1 [root@localhost ~]# cat /usr/lib/zabbix/alertscripts/wechat.py
 2 #!/usr/bin/python
 3 #_*_coding:utf-8 _*_
 4 import urllib,urllib2
 5 import json
 6 import sys
 7 import simplejson
 8 reload(sys)
 9 sys.setdefaultencoding('utf-8')
10 def gettoken(corpid,corpsecret):
11 gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
12 print gettoken_url
13 try:
14 token_file = urllib2.urlopen(gettoken_url)
15 except urllib2.HTTPError as e:
16 print e.code
17 print e.read().decode("utf8")
18 sys.exit()
19 token_data = token_file.read().decode('utf-8')
20 token_json = json.loads(token_data)
21 token_json.keys()
22 token = token_json['access_token']
23 return token
24 def senddata(access_token,user,subject,content):
25 send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
26 send_values = {
27 "touser":user, #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
28 "toparty":"2", #企业号中的部门id。
29 "msgtype":"text", #消息类型。
30 "agentid":"2", #企业号中的应用id。
31 "text":{
32 "content":subject + '\n' + content
33 },
34 "safe":"0"
35 }
36 # send_data = json.dumps(send_values, ensure_ascii=False)
37 send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
38 send_request = urllib2.Request(send_url, send_data)
39 response = json.loads(urllib2.urlopen(send_request).read())
40 print str(response)
41 if __name__ == '__main__':
42 user = str(sys.argv[1]) #zabbix传过来的第一个参数
43 subject = str(sys.argv[2]) #zabbix传过来的第二个参数
44 content = str(sys.argv[3]) #zabbix传过来的第三个参数
45 corpid = '11111111111111' #CorpID是企业号的标识
46 corpsecret = '222222222222222222' #corpsecretSecret是管理组凭证密钥
47 accesstoken = gettoken(corpid,corpsecret)
48 senddata(accesstoken,user,subject,content)

 

执行py脚本,进行测试

 
[root@localhost 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'}

 

这里我公司没有部门id,我直接设置了1(貌似不影响)

 

五、zabbix web 界面配置

报警消息设置如下:

 
  1. hostname: ({HOST.NAME}
  2. Time:{EVENT.DATE} {EVENT.TIME}
  3. level:{TRIGGER.SEVERITY}
  4. message:{TRIGGER.NAME}
  5. event:{ITEM.NAME}:{ITEM.VALUE}
  6. url:www.abcdocker.com

恢复报警如下:

 
  1. hostname: ({HOST.NAME}
  2. Time:{EVENT.DATE} {EVENT.TIME}
  3. level:{TRIGGER.SEVERITY}
  4. message:{TRIGGER.NAME}
  5. event:{ITEM.NAME}:{ITEM.VALUE}
  6. url:www.abcdocker.com

提示: 不要忘记先点小的add-->小的update-->Update

 

六、测试

为了验证效果我们停掉zabbix-agent,进行查看报警

 
  1. [root@abcdocker ~]# systemctl stop zabbix-agent

相关文章: