zoukankan      html  css  js  c++  java
  • Zabbix 微信监控报警

    Zabbix-Server 设置

    # 使脚本目录生效
    [root@zabbix ~]# grep 'script' /etc/zabbix/zabbix_server.conf 
    # AlertScriptsPath=${datadir}/zabbix/alertscripts
    AlertScriptsPath=/usr/lib/zabbix/alertscripts
    

    Python 脚本

    # 在写 Python 脚本前,我们需要几个 Python 模块
    [root@zabbix ~]# wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
    [root@zabbix ~]# tar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2
    [root@zabbix ~]# python setup.py build
    [root@zabbix ~]# python setup.py install
    
    
    # 进入脚本目录,编写 Python 脚本
    [root@zabbix ~]# cd /usr/lib/zabbix/alertscripts
    [root@zabbix alertscripts]# vim 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":'damaiyunwei',    # 企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
            "toparty":"2",    # 企业号中的部门id。
            "msgtype":"text", # 消息类型。
            "agentid":"1000002",    # 企业号中的应用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')
        print(send_data)
        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 =  'xxxxxxxxxxxxxx'   # CorpID 是企业号的标识
        corpsecret = 'xxxxxxxxxxxxxxx-xxxxxx-xxxxx'  # corpsecretSecret 是管理组凭证密钥
        accesstoken = gettoken(corpid,corpsecret)
        senddata(accesstoken,user,subject,content)
    
    
    # 给脚本文件加可执行权限
    [root@zabbix alertscripts]# chmod +x wechat.py
    




    关注企业微信插件,无需下载企业微信即可获取报警信息:

    告警动作中文格式

    # ====================== 故障告警格式 =========================== #
    故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
    
    接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}
    告警主机:{HOST.NAME}
    主机地址:{HOST.IP}
    告警时间:{EVENT.DATE} {EVENT.TIME}
    告警等级:{TRIGGER.SEVERITY}
    告警信息:{TRIGGER.NAME}
    问题详情:{ITEM.NAME}:{ITEM.VALUE}
    事件代码:{EVENT.ID}
    
    # ====================== 恢复通知格式 =========================== #
    恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
    
    接收人:{TRIGGER.STATUS}: {TRIGGER.NAME}
    恢复主机:{HOST.NAME}
    主机地址:{HOST.IP}
    恢复时间:{EVENT.DATE} {EVENT.TIME}
    恢复等级:{TRIGGER.SEVERITY}
    恢复信息:{TRIGGER.NAME}
    问题详情:{ITEM.NAME}:{ITEM.VALUE}
    事件代码:{EVENT.ID}
    
  • 相关阅读:
    c# 日期函数
    js中的replace问题和textarea回车符问题
    项目代码风格要求
    重温Observer模式--热水器·改
    xcode 编译glfw , 导出.h
    开发者所需要知道的 iOS 11 SDK 新特性
    RAC基础笔记(2)
    RAC基础笔记
    NSString copy,strong 修饰问题
    git 常用操作
  • 原文地址:https://www.cnblogs.com/zzzwqh/p/13669331.html
Copyright © 2011-2022 走看看