zoukankan      html  css  js  c++  java
  • zabbix3.4实现微信报警,

    说明:脚本参考别的博客,- - 反正不是自己写的

    实验环境:Zabbix监控服务器、客户端都已经部署完成,被监控主机已添加完成,Zabbix监控运行正常。服务器,selinux,已经关闭

    zabbix端  :ip 10.10.251.48   hostname=zabbix-server-server

    被监控端: ip 10.10.251.58   hostname=zabbix-agent-centos65

    实现目的:被监控端检测到数据达到触发器预设值后,会自动发送报警邮件到指定的微信,报警结束后,会发邮件说明,

    大致流程

    (1)获取微信重要的信息corpid(企业id),corpsecret, 部门id,参考https://www.cnblogs.com/kumarhua/p/7645399.html来获取信息。

    (2)获取Access_Token: 根据自己的corpid和corpsecret获取AccessToken,这个像微信发送的接口,一定确认过去正确

    (3)添加微信调用脚本,附加3个参数,报警时会自动发送微信

    (2)去web端添加卫星触发动作,和需要发送报警内容。设置邮件间隔时间间隔(测试设置时间间隔要短,有错误时,改正后方便确认,)。

    一:

    1)AccessToken说明

    2)写微信脚本,在server端确认脚本路径

    3)需要安装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

    4)vi  /usr/lib/zabbix/alertscripts/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":"xxxxxxx", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。 "toparty":"x", #企业号中的部门id。 "msgtype":"xxxx", #消息类型。 "agentid":"xxxx", #企业号中的应用id。 "text":{ "content":subject + ' ' + 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 = 'xxxxxx' #企业号的标识(上文中提到获取位置) corpsecret = 'xxxxxx' #管理组凭证密钥(上文中提到获取位置) accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content)

    5)测试脚本是否可以正常发到微信,

     

    看到这个就成功了

    二,配置web端报警

     管理==》报警媒介类型==》类型选择(脚本)==》添加三个参数

     配置==》动作==》操作==》通过微信发送

    三,测试微信报警,和邮件报警一样

    There are no shortcuts to any place worth going. -- Beverly Sills
  • 相关阅读:
    How to alter department in PMS system
    Can't create new folder in windows7
    calculate fraction by oracle
    Long Wei information technology development Limited by Share Ltd interview summary.
    ORACLE BACKUP AND RECOVERY
    DESCRIBE:When you mouse click right-side is open an application and click left-side is attribution.
    ORACLE_TO_CHAR Function
    电脑BOIS设置
    JSP点击表头排序
    jsp+js实现可排序表格
  • 原文地址:https://www.cnblogs.com/zdoubly/p/9230586.html
Copyright © 2011-2022 走看看