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'}

看企业微信收到了不?

扫描二维码关注公众号,回复: 11362543 查看本文章

发现接收到了。

小结

上面的要注意的就讲解到此,详细可以参阅我的视频教程。

猜你喜欢

转载自blog.csdn.net/knight_zhou/article/details/106937276