zhangkai 发表于 2024-3-4 10:38:39

微信报警


#!/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='ww2a1f8fa4a4ce55ed'
appsecret='7oHs9WWabR_NeKeQBTpwwdOUV2GKWKd0YQJzkwQiA_M'
agentid='1000002'
#获取accesstoken
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
subject=sys.argv
#toparty='3|4|5|6'
message=sys.argv + "\n\n" +sys.argv
params={
"touser": touser,
# "toparty": toparty,
"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)


需要修改的部分有:corpid='微信企业号corpid'appsecret='应用的Secret'agentid=应用的id
脚本的三个参数都是zabbix内置变量,分别为:报警收件人:{ALERT.SENDTO},报警标题:{ALERT.SUBJECT},报警内容:{ALERT.MESSAGE}

页: [1]
查看完整版本: 微信报警