zabbix微信报警

1.1微信企业号的申请

注册地址
在这里插入图片描述

1.2创建用户

在这里插入图片描述

1.3创建应用

在这里插入图片描述

1.4调用微信接口

我们配置微信脚本 首先我们需要调用微信接口
调用微信接口需要一个调用接口的凭证:access_token
通过:GropID,Secret才能获取到access_token,但是获取到的token有效期为两分钟
微信企业号接口调试工具地址:http://qydev.weixin.qq.com/debug
打开上面的url,使用上面monitops企业号的monit-manager管理组的CorpIDSecret号获得调用接口凭证access_token,如下

在这里插入图片描述

1.5配置zabbix脚本

[root@c7-41 alertscripts]# pwd
/usr/lib/zabbix/alertscripts
[root@c7-41 alertscripts]# wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
[root@c7-41 alertscripts]# tar -zxvf simplejson-3.8.2.tar.gz
[root@c7-41 alertscripts]# python setup.py build
[root@c7-41 alertscripts]# python setup.py install
[root@c7-41 alertscripts]# cat weixin.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":"***",   #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
        "toparty":"1",        #企业号中的部门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 =  'wwffd78d********'     #企业号的标识(上文中提到获取位置)
    corpsecret = 'jIBdzJfxwVl11riAvHO3fPVbuO7************'    #管理组凭证密钥(上文中提到获取位置)
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)
    
[root@c7-41 alertscripts]# chmod 755 weixin.py 
[root@c7-41 alertscripts]# chown -R zabbix.zabbix weixin.py

1.6手动测试weixin.py脚本

#可以使用用户账号或应用ID去测试,观察微信是否能收到信息:
#cr7是我们成员的账号
[root@c7-41 alertscripts]# ./weixin.py cr7 test test
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwffd78d1c0a4eaf70&corpsecret=jIBdzJfxwVl11riAvHO3fPVbuO7rwebubDDEAPHNUwo
{u'invalidparty': u'1', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}

1.7创建报警媒介

(这里我选用python脚本:weixin.py)
注意下面要填写的脚本参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

在这里插入图片描述在这里插入图片描述

1.8创建用户weixin

密码一栏可以选择跟之前的超级管理员zabbix一样的密码;用户类型也可以设置成超级管理员

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

1.9创建动作

{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机IP: {HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

root@c7-42 ~]# systemctl stop httpd

在这里插入图片描述

到这里我们就配置完毕了 但是这样的问题是只能用企业微信接到消息 那么如何让我们平时使用的微信也能接到消息呢

在这里插入图片描述

这里我们用手机扫描二维码进行关注 下面我们来测试下

root@c7-42 ~]# systemctl stop httpd

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云原生解决方案

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值