zabbix配置微信报警实战案例
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.企业微信号注册步骤详解及zabbix报警媒介配置
博主推荐阅读: https://www.cnblogs.com/yinzhengjie/p/10829156.html
二.zabbix server编写微信报警脚本
1>.安装python的依赖模块
[root@zabbix203.yinzhengjie.org.cn ~]# apt install python2.7 [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# apt -y install python-pip [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# pip install requests #该模块可以用来发起URL请求 [root@zabbix203.yinzhengjie.org.cn ~]#
2>.编写微信脚本
[root@zabbix203.yinzhengjie.org.cn ~]# cat /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py #!/usr/bin/env python #_*_coding:utf-8_*_ #@author :yinzhengjie #blog:http://www.cnblogs.com/yinzhengjie #EMAIL:y1053419035@qq.com #导入python模块 import requests import sys import os import json import logging #自定义logging模块的格式,具体日志会存放在"/var/log/weixin.log"中,你也可以自定义到其它目录中,该文件默认以追加方式写入,你可以写一个周期性任务进行滚动操作 logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s', datefmt = '%a, %d %b %Y %H:%M:%S', filename = os.path.join('/var/log','weixin.log'), filemode = 'a') #指定企业ID corpid='ww245d826cb82c74bf' #指定应用管理的Secret appsecret="qayESLiAQU9l77N_CyE-w098bsn1xCMXWshMkhy9A_o" #指定应用管理的AgentId agentid="1000002" #指定Token_url token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret #获取tocken req=requests.get(token_url) #获取tocken中access_token字段对应的值 accesstoken=req.json()['access_token'] #定义消息发送的URL msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken #定义要发送的人,也就是zabbix webUI配置的"{ALERT.SENDTO}"参数对应的值 touser=sys.argv[1] #定义要发送的主题,也就是zabbix webUI配置的"{ALERT.SUBJECT}"参数对应的值 subject=sys.argv[2] #定义要发送的消息内容,也就是zabbix webUI配置的"{ALERT.MESSAGE}"参数对应的值 message=sys.argv[2] + " " +sys.argv[3] #指定参数 params={ "touser": touser, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe":0 } #基于requests模块的post方法发送企业微信报警消息 req=requests.post(msgsend_url, data=json.dumps(params)) #发送消息后记得打印日志信息,在本地的配置文件中可以查看 logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message) [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]#
3>.为脚本添加执行权限
[root@zabbix203.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py -rw-r--r-- 1 root root 1971 3月 5 14:50 /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# chmod +x /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py -rwxr-xr-x 1 root root 1971 3月 5 14:50 /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py* [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]#
4>.测试脚本的可用性
[root@zabbix203.yinzhengjie.org.cn ~]# python /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py jason2020 "微信脚本测试" "改内容只是用于测试,请自动忽略~" [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# ll /var/log/weixin.log -rw-r--r-- 1 root root 955 3月 5 14:57 /var/log/weixin.log [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# cat /var/log/weixin.log Thu, 05 Mar 2020 14:57:32, connectionpool.py, DEBUG, Starting new HTTPS connection (1): qyapi.weixin.qq.com:443 Thu, 05 Mar 2020 14:57:32, connectionpool.py, DEBUG, https://qyapi.weixin.qq.com:443 "GET /cgi-bin/gettoken?corpid=ww245d826cb82c74bf&corpsecret=qcyERLiBQU9l77N_CyE-w098bsn1xCMXWshMkhy9A_o HTTP/1.1" 200 277 Thu, 05 Mar 2020 14:57:32, connectionpool.py, DEBUG, Starting new HTTPS connection (1): qyapi.weixin.qq.com:443 Thu, 05 Mar 2020 14:57:33, connectionpool.py, DEBUG, https://qyapi.weixin.qq.com:443 "POST /cgi-bin/message/send?access_token=O59W-_6KbaHgcPT-6bbQCV6wBpCOJUnKoO5cegGikzhkwDjiNTqYFEbFD_xRVyZWIPXq7587ogoRYbJIPT6BxSQ0yb7mQcsDNaqeDyT5bhelJV5jcN27JdWGjfUfrv66Ahv6r4sl0hHs1oRO Hb-KHvPSkv5uNpK_WTfeiw7cCNtaVjXs6yVdude8RbSTo67E5e2P2EjBmgnHJHn_prKBww HTTP/1.1" 200 235Thu, 05 Mar 2020 14:57:33, weixin.py, INFO, sendto:jason2020;;subject:微信脚本测试;;message:微信脚本测试 改内容只是用于测试,请自动忽略~ [root@zabbix203.yinzhengjie.org.cn ~]#
5>.将的权限也授权给zabbix用户
[root@zabbix203.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py -rwxr-xr-x 1 root root 1971 3月 5 14:50 /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py* [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# chown zabbix:zabbix /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]# ll /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py -rwxr-xr-x 1 zabbix zabbix 1971 3月 5 14:50 /yinzhengjie/softwares/zabbix/share/zabbix/alertscripts/weixin.py* [root@zabbix203.yinzhengjie.org.cn ~]# [root@zabbix203.yinzhengjie.org.cn ~]#
三.后续测试步骤和邮件告警类似,我这里就不赘述啦
博主推荐阅读: https://www.cnblogs.com/yinzhengjie2020/p/12354418.html