首页 > 技术文章 > zabbix微信报警脚本

wangjie20200529 2021-01-22 17:34 原文

yum -y install python-pip
若yum无法下载安装,则:
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py
pip install --upgrade pip
pip install requests

vim zabbix_wx.py

!/usr/bin/env python

-- coding: utf-8 --

import requests
import sys
import os
import json
import logging

config for log

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('/web/zabbix/share/zabbix/alertscripts','weixin.log'),
filemode = 'a')

agentid = 'xxx' ##AgentId
corpid = 'xxx' ##企业ID
corpsecret = 'xxx' ##Secret

Accesstoken

token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']

Message send

msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]

toparty='3|4|5|6'

message=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)

推荐阅读