跳至主要內容

zabbix 微信告警

Change Lee...大约 3 分钟大杂烩zabbix

申请企业微信

https://work.weixin.qq.com/?from=qyh_redirectopen in new window

image.png
1556698302988775.png

添加应用

image.png
1556698457169593.png

记录下 Agentid 和 Secret 方便在脚本中使用

image.png
1556698509220982.png

返回我的企业,记录下企业ID (corpid)

image.png
1556698637748334.png

登录企业微信api 测试页面

https://work.weixin.qq.com/api/devtools/devtool.phpopen in new window

输入你的 corpid(企业ID),和你应用的 Secret

image.png
1556698828979133.png

检查问题后可以查看结果如下则表示API 调用成功。

image.png
1556698885351971.png

目前zabbix 企业微信多作py 脚本来实现(2019-5-1)。测试通过的脚本 如下:

脚本 1 ,python2.7 可用,需要安装安装 pip ,使用 pip 安装 requests,脚本会产生日志文件,/tmp/weixin.log

#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sys
import os
import json
import logging

logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')

corpid='企业id'
appsecret='应用的secret'
agentid='应用的agentid'
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']

msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken

touser=sys.argv[1]
subject=sys.argv[2]
message=sys.argv[2] + "\n\n" +sys.argv[3]

params={
"touser": touser,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}

req=requests.post(msgsend_url, data=json.dumps(params))

logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)

脚本放在 zabbix 的 alertscripts 目录中。

给脚本授权

chmod +x weixin.py
#使用脚本测试
./weixin.py username "告警标题" '告警内容' 
#测试完需要修改/tmp/weixin.log 的权限给 zabbix 
chown zabbix.zabbix /tmp/weixin.log

python3 脚本 ,已经测试成功,同样需要给日志文件 /tmp/weixin_access_token.log授权

#!/usr/local/python3/bin/python3
# -*- coding:utf-8 -*-
import requests
import json
import sys

#企业号及应用相关信息

corp_id ='ww09fa9ae0d0682328'
corp_secret ='aoKWeIeLxwFXcwWAyLZmEo9mtfm3rNBSKqbnG8QtzFc'
agent_id =1000002

#存放access_token文件路径
file_path ='/tmp/weixin_access_token.log'
def get_access_token_from_file():
    try:
        f=open(file_path,'r+')
        this_access_token=f.read()
        #print('get success %s' % this_access_token)
        f.close()
        return this_access_token
    except Exception as e:
        print(e)
#获取token函数,文本里记录的token失效时调用
def get_access_token():
    get_token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s'%(corp_id,corp_secret)
    #print(get_token_url)
    r = requests.get(get_token_url)
    request_json = r.json()
    this_access_token = request_json['access_token']
    #print(this_access_token)
    r.close()
#把获取到的access_token写入文本
    try:
        f = open(file_path,'w+')
        f.write(this_access_token)
        f.close()
    except Exception as e:
        print(e)
#返回获取到的 access_token 值
    return this_access_token
#sendMessage
#列循环,直到消息成功发送
flag = True
while(flag):
    #从文本获取access_token
    access_token = get_access_token_from_file()
    try:
        to_user = '@all'
        message = sys.argv[3]
        send_message_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % access_token
        #print(send_message_url)
        message_params = {
                            "touser":to_user,
                            "msgtype":"text",
                            "agentid":agent_id,
                            "text":{
                                 "content":message
                            },
                            "safe":0
                        }
        r = requests.post(send_message_url, data=json.dumps(message_params))
        #print('post success %s' % r.txt)
#判断是否发送成功,如果不成功则跑出异常,让其执行异常处理里的函数
        request_json = r.json()
        errmsg = request_json['errmsg']
        #print(errmsg)
        if errmsg != 'ok': raise
#消息成功发送,停止列循环
        flag = False
    except Exception as e:
        print(e)
        access_token = get_access_token()

配置 zabbix

添加告警媒介 管理–告警媒介类型–创建媒体类型

image.png
1556699832415756.png

脚本参数:

{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}

可以不创建新用户,直接编辑现有用户 管理—用户—选中用户–编辑

image.png
1556699638898946.png

收件人为需要接收人的企业微信号,不是微信号是企业微信号。

image.png
1556699730628692.png

添加告警动作:

image.png
1556700599357304.png
image.png
1556700704982442.png

weixin-python2.7.tar.gzopen in new window

weixin-python3.tar.gzopen in new window

上次编辑于:
贡献者: change,lichangyangccm@163.com
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v3.1.3