Prometheus 微信告警注意事項

注意事項

我們在使用Prometheus發送微信告警的時候,有時候卻收不到告警,是因爲你沒有配置好。所以有幾個坑以及注意事項 我在這裏講解下。請看下圖:

 

注意: 上面的id爲1 默認是系統保留,所以你看到的部門id一定要大於1

那我怎麼知道我的企業微信是否正常了?

要檢測企業微信是否正常,可以使用如下的python腳本(因爲教程是Centos7.x的版本,你直接使用系統的Python版本2.7.x執行即可),如果提示缺少庫文件,百度安裝庫就行了,下面的腳本也是使用zabbix的微信告警腳本來的。所以可以用來測試企業微信是否正常。

先安裝Python的依賴庫

yum install python-simplejson -y

wechat.py內容如下:

#!/bin/python
#coding:utf-8
# https://work.weixin.qq.com/?from=qyh_redirect
# 

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":"zhoulong",    #企業號中的用戶帳號,在zabbix用戶Media中配置,如果配置不正常,將按部門發送。
        "toparty":"2",    
        "msgtype":"text", #消息類型。
        "agentid":"1000002",    #企業號中的應用id。
        "text":{
            "content":subject + '\n' + content
           },
        "safe":"0"
        }

    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 =  'ww2c68f6f17cafd1a1'   #CorpID是企業號的標識
    corpsecret = 'xxoo'  #corpsecretSecret是管理組憑證密鑰
    accesstoken = gettoken(corpid,corpsecret)
    senddata(accesstoken,user,subject,content)

我們來執行一個這個腳本,爲了測試部門 id看是否正常,我們先改toparty爲1試一試:

$ python wechat.py xx oo fdsaf
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww2c68f6f17cafd1a1&corpsecret=g9PPFEHgC0TIiHV5GRFG9GiaZEb06g1uZ_AHeSWrUTE
{u'invalidparty': u'1', u'invaliduser': u'zhoulong', u'errcode': 81013, u'errmsg': u'user & party & tag all invalid, hint: [1592963028_77_31450f269d948771df4a3ea80370b1ba], from ip: 139.9.52.247, more info at https://open.work.weixin.qq.com/devtool/query?e=81013'}

結果報錯了。好吧那我們改成 toparty 爲 2或者以上。

$ python wechat.py xx oo bb

返回結果爲:

https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=ww2c68f6f17cafd1a1&corpsecret=g9PPFEHgC0TIiHV5GRFG9GiaZEb06g1uZ_AHeSWrUTE
{u'invaliduser': u'zhoulong', u'errcode': 0, u'errmsg': u'ok'}

看企業微信收到了不?

發現接收到了。

小結

上面的要注意的就講解到此,詳細可以參閱我的視頻教程。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章