1. 程式人生 > >zabbix微信告警

zabbix微信告警

__name__ ref art seve lib 事件 ejs msg 設置報警

1、註冊企業微信

註冊地址: https://work.weixin.qq.com,註冊過程就不記錄了

2、記錄幾個值

CorpID:

技術分享圖片

Secret跟AgentId:

技術分享圖片

成員賬號跟組織部門ID:

技術分享圖片技術分享圖片

3、查看zabbix腳本的路徑以及編寫腳本,圖片中放在/tmp/alertscripts

技術分享圖片

#!/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":"ZhangShun",    #企業號中的用戶
帳號,在zabbix用戶Media中配置,如果配置不正常,將按部門發送。 "toparty":"1", #企業號中的部門id。 "msgtype":"text", #消息類型。 "agentid":"1000002", #AgentId,企業號中的應用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 = ‘*********‘ #CorpID是企業號的標識
corpsecret = ‘********************************‘ #Secret是管理組憑證密鑰 accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content)

4、微信報警測試

[root@zabbix alertscripts]# ./qiyeweixingaojing.py ZhangShun  測試報警 error
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwf3f6202201dd3fde&corpsecret=pTC2RctRosVoWWPHPWSKHcrze0U55BdHJ53yGd7BpJQ
{u‘invalidparty‘: u‘1‘, u‘invaliduser‘: u‘‘, u‘errcode‘: 0, u‘errmsg‘: u‘ok‘}

 技術分享圖片

5、 zabbix web界面配置

5.1 配置報警媒介

5.2 創建報警用戶

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

5.4 創建動作、設置報警消息、恢復報警

報警消息:

故障{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}

恢復報警:

恢復{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}

參考博客:https://blog.csdn.net/weixin_39845407/article/details/81135946

zabbix微信告警