python微信报警脚本

import urllib.request
import json,sys

def get_token(url, corpid, corpsecret):
    token_url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url, corpid, corpsecret)
    token = json.loads(urllib.request.urlopen(token_url).read().decode())['access_token']
    return token

def messages(msg):
    values = {
        "touser": '**********',
        "msgtype": 'text',
        "agentid": 1000002, #注意修改为对应应用的agentid
        "text": {'content': msg},
        "safe": 0
        }
    msges=(bytes(json.dumps(values), 'utf-8'))
    return msges
def send_message(url,token, data):
        send_url = '%s/cgi-bin/message/send?access_token=%s' % (url,token)
        respone=urllib.request.urlopen(urllib.request.Request(url=send_url, data=data)).read()
        x = json.loads(respone.decode())['errcode']
        # print(x)
        if x == 0:
            print ('Succesfully')
        else:
            print ('Failed')

if __name__ == '__main__':
    corpid = '*********************'
    corpsecret = '******************************'
    url = 'https://qyapi.weixin.qq.com'
    msg = str(sys.argv[1])
    #函数调用
    test_token=get_token(url, corpid, corpsecret)
    msg_data= messages(msg)
    send_message(url,test_token, msg_data)    
以下是一个基于 Python 实现的 Zabbix 微信报警脚本,需要先安装 WeixinBot 和 requests 库: ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import sys class WeChat(object): def __init__(self, corpid, corpsecret): self.url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' self.corpid = corpid self.corpsecret = corpsecret self.params = { 'corpid': self.corpid, 'corpsecret': self.corpsecret } self.access_token = self.get_access_token() def get_access_token(self): r = requests.get(self.url, params=self.params) return r.json()['access_token'] def send_message(self, message, agentid, touser=None, toparty=None): url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send' data = { "touser": touser, "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe": 0 } params = { 'access_token': self.access_token } headers = {'Content-Type': 'application/json;charset=utf-8'} r = requests.post(url, params=params, data=json.dumps(data), headers=headers) return r.json() if __name__ == "__main__": corpid = 'YOUR_CORPID' corpsecret = 'YOUR_CORPSECRET' agentid = 'YOUR_AGENTID' touser = sys.argv[1] subject = sys.argv[2] content = sys.argv[3] message = u"收件人:%s\n主题:%s\n内容:%s" % (touser, subject, content) wechat = WeChat(corpid, corpsecret) response = wechat.send_message(message, agentid, touser=touser) print(response) ``` 使用方法: 1. 用企业微信扫描二维码,加入企业; 2. 创建一个应用,并记录下应用的 `corpid`、`corpsecret`和 `agentid`; 3. 安装 WeixinBot 和 requests 库; 4. 将上述代码保存到文件 `wechat.py` 中; 5. 在 Zabbix 中配置告警媒介,将 `wechat.py` 的路径作为脚本名,并在参数中依次填入收件人、主题和内容。例如:`/usr/bin/python /path/to/wechat.py {ALERT.SENDTO} "{ALERT.SUBJECT}" "{ALERT.MESSAGE}"`; 6. 测试告警是否正常发送。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值