Zabbix 4.0.5 配置微信报警


企业微信注册地址:
https://work.weixin.qq.com/

1.开始申请企业微信(个人公众号暂未验证,不确定能否实现API调用)
在这里插入图片描述
2.配置企业微信-新增组织部门
在这里插入图片描述
3.创建第三应用
在这里插入图片描述
在这里插入图片描述
选择指定的部门才可以看见Zabbix告警信息
在这里插入图片描述
在这里插入图片描述
需要得到的信息
成员账号
组织部门ID
AgentID
CorpID
Secret
以上信息可以参考企业微信API文档帮助
https://work.weixin.qq.com/api/doc#10013
以上准备工作完成后开始登入Zabbix配置

4.登入zabbix Console
a.输入python,确认python运行环境,exit()退出检测

[root@ICC-Zabbix-MIS /]# python
Python 2.7.5 (default, Oct 30 2018, 23:45:53)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
[root@ICC-Zabbix-MIS /]#

b.确认并启用zabbix报警脚本路劲

[root@ICC-Zabbix-MIS /]# find / -name "alertscripts"
/usr/lib/zabbix/alertscripts

如没有以上路劲,请自行创建该路劲,并修改zabbix_Server.conf文件

[root@ICC-Zabbix-MIS /]# find /etc -name "zabbix_server*"
/etc/zabbix/zabbix_server.conf
[root@ICC-Zabbix-MIS /]#vi /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/opt/scripts/zabbix/alertscripts             <------修改此处到正确的路劲

c.安装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 instal

注意此处有一个大坑,在安装simplejson时注意有没有什么报错信息,我的在安装时就出现错误信息
“xxxxxxxxx 致命错误:Python.h:没有那个文件或目录”
问题原因:

缺失python-devel开发包所导致,python.h存在于python-devel开发包

解决办法:

yum install python3.4-devel

d.进入zabbix 报警脚本目录,创建wechat.py,赋予wechat.py可执行权限

[root@ICC-Zabbix-MIS /]# cd /usr/lib/zabbix/alertscripts/
[root@ICC-Zabbix-MIS alertscripts]# touch wechat.py && chmod u+x,g+x,o+x wechat.py
[root@ICC-Zabbix-MIS alertscripts]# ll
总用量 4
-rwxr-xr-x. 1 root root 2024 3月  21 13:58 wechat.py

配置wechat.py
[root@ICC-Zabbix-MIS alertscripts]# vi 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":"denni",    #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"3",    #企业号中的部门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 =  'ww14136a649f0a8e'   #CorpID是企业号的标识
    corpsecret = 'hUjcv5uJS-`Byrntjg_Yc4wBXtEHx**g2NCW**z20F4Q***`'  #corpsecretSecret是管理组凭证密钥
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)

e.wechat报警测试

[root@ICC-Zabbix-MIS alertscripts]# ./wechat.py test@uwe.com 报警标题 报警内容xxxxxxxx
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww1413a69f890a8e&corpsecret=hUjcv5uJS-Byrntjg_YcwBXtEHx3g2NC5Wz20F4Q
{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
[root@ICC-Zabbix-MIS alertscripts]#

#此处test@uwe.com 输随意乱输入的  找不到正确账号 以部门ID发送信息

**

备注: 用户端必须安装企业版微信才可以正常收到报警信息

**


在这里插入图片描述

5.通过zabbix Web Console配置微信报警媒介
a.添加weChat报警媒介
在这里插入图片描述
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

b.关联动作触发操作
在这里插入图片描述
c.关联报警用户
在这里插入图片描述
这是最终的微信报警效果确认
在这里插入图片描述

如报警不成功,可去监控日志查看具体的原因,我一开始就忘记配置wechat报警用户了
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值