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

Zabbix Web 邮件报警

 

一、微信企业号申请

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

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

 

二、配置微信企业号

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

 

准备事项:

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

 

需要得到的信息

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

三、修改Zabbix.conf

 
  1. [root@abcdocker ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
  2. AlertScriptsPath=/usr/lib/zabbix/alertscripts
  3. 我们设置zabbix默认脚本路径,这样在web端就可以获取到脚本
 

四、设置python脚本

#安装simplejson

 
  1. wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
  2. tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
  3. python setup.py build
  4. python setup.py install

下载wechat.py脚本

 
  1. git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
  2. cp Zabbix-Alert-WeChat/wechat.py /usr/lib/zabbix/alertscripts/
  3. cd /usr/lib/zabbix/alertscripts/
  4. chmod +x wechat.py && chown zabbix:zabbix wechat.py

提示:这里需要修改py脚本 
看注释,这就不解释了

 
  1. [root@abcdocker ~]# 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脚本,进行测试

 
  1. [root@abcdocker alertscripts]# ./wechat.py www www 123
  2. https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx6dadb9cc293b793e&corpsecret=JjesoeixbFt6dDur7_eXtamVBx2SjPBuXMQ0Jte3YLkz8l-VBnr0JvU12P0kvpGJ
  3. {u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'}

 

五、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

相关文章:

  • 2021-12-20
  • 2021-05-19
  • 2021-11-24
  • 2022-12-23
  • 2021-11-18
猜你喜欢
  • 2021-12-17
  • 2021-07-25
  • 2021-08-27
  • 2022-02-15
  • 2021-11-25
相关资源
相似解决方案