ZABBIX-微信報警

Zabbix能夠經過多種方式把告警信息發送到指定人,經常使用的有郵件,短信報警方式,可是愈來愈多的企業開始使用zabbix結合微信做爲主要的告警方式,這樣能夠及時有效的把告警信息推送到接收人,方便告警的及時處理。python

一、註冊企業微信web

註冊地址: https://work.weixin.qq.comjson

 

 

二、配置企業微信api

2.1 建立部門bash

 

 

2.2 添加成員服務器

 

 

 

也可經過二維碼邀請成員微信

 

 

記住成員帳號(後面會用到)測試

 

 

3. 建立應用ui

 

 

填寫應用信息(應用名字和可見成員)url

 

 

 

 

記住應用(AgentId、Secret後面會用到)

 

 

四、配置監控腳本
---------------------

準備事項:

微信企業號 
企業號已經被部門成員關注 
企業號有一個能夠發送消息的應用,一個受權管理員,可使用應用給成員發送消息

須要獲得的信息

成員帳號
組織部門ID
應用ID
CorpID和Secret
4.1 修改zabbix_agentd

[root@localhost ~]# grep alertscripts /etc/zabbix/zabbix_server.conf
# AlertScriptsPath=${datadir}/zabbix/alertscripts
AlertScriptsPath=/opt/scripts/zabbix/alertscripts
4.2 安裝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 install
4.3 配置監控腳本

[root@localhost alertscripts]# cat /opt/scripts/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":'damaiyunwei',    #企業號中的用戶賬號,在zabbix用戶Media中配置,若是配置不正常,將按部門發送。
        "toparty":"2",    #企業號中的部門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')
    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 =  'ww758c347e5635e284'   #CorpID是企業號的標識
    corpsecret = 'Sf5m6Xfu3laloq36k9mIrrpjYufGl4U61zyUApCWNcE'  #corpsecretSecret是管理組憑證密鑰
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)

4.4 報警測試

./wechat.py 1 報警測試 error

{"text": {"content": "報警測試\nerror"}, "safe": "0", "msgtype": "text", "touser": "damaiyunwei", "agentid": "1000002", "toparty": "2"}
{u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}

 

 

五、 zabbix web界面配置

5.1 配置報警媒介

5.2 建立報警用戶

五、 zabbix web界面配置

5.1 配置報警媒介

5.2 建立報警用戶

5.3 配置用戶報警媒介(選擇剛纔建立的)

 

5.4 建立動做

 

 

5.5 設置報警消息

 

故障{TRIGGER.STATUS},服務器:{HOSTNAME1}發生: {TRIGGER.NAME}故障!
 
告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}


5.6 恢復報警

 

恢復{TRIGGER.STATUS}, 服務器:{HOSTNAME1}: {TRIGGER.NAME}已恢復!
 
告警主機:{HOSTNAME1}
告警時間:{EVENT.DATE} {EVENT.TIME}
告警等級:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警項目:{TRIGGER.KEY1}
問題詳情:{ITEM.NAME}:{ITEM.VALUE}
當前狀態:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}

到此,zabbix微信報警已完成,自行測試便可

相關文章
相關標籤/搜索