#!/usr/bin/python
# -*- coding: utf-8 -*-
# zabbix notification confirmation script
# python2.7 or above
import requests
import json
import os
import sys
Toparty = "13" #部门id
AgentID = 1000008 #应用id
#修改为企业CropID和Secret
CropID = '1·#!·#!·#!·#!·#!·3'
Secret = '!·#!·#!·#!#!·#!·#'
#获取Token
Gtoken ="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+ CropID + "&corpsecret=" + Secret
headers = {'Content-Type': 'application/json'}
json_data = json.loads(requests.get(Gtoken).content.decode())
token = json_data["access_token"]
#消息发送接口
Purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=" + token
#消息发送函数
def msg(title,message):
weixin_msg = {
"toparty" : Toparty,
"msgtype" : "textcard",
"agentid" : AgentID,
"textcard" : {
"title" : title,
"description" : message,
"url" : "www.wzlinux.com",
"btntxt":"更多"
}
}
print requests.post(Purl,json.dumps(weixin_msg),headers=headers)
if __name__ == '__main__':
title = sys.argv[1] #获取第一个参数
message = sys.argv[2] #获取第二个参数
msg(title,message)chmod +x /usr/local/zabbix/alertscripts/wechat.py
测试脚本(1为标题 2为内容)
./wechat.py 1 2
如出现以下报错需安装独立模块
[root@localhost /]# ./wechat.py 1 2
Traceback (most recent call last):
File "./wechat.py", line 6, in <module>
import requests
ImportError: No module named requests
[root@localhost /]# wget https://bootstrap.pypa.io/get-pip.py
[root@localhost /]# python get-pip.py
[root@localhost /]# pip install requests
安装完后再次执行脚本
[root@localhost /]# ./wechat.py 1 2
<Response [200]>
次结果为正常,同时微信受到标题1内容2的消息
脚本转自 https://www.linuxidc.com/Linux/2017-11/148418.htm 增加了报错的处理
zabbix 微信脚本(转贴后增加)
原文:http://blog.51cto.com/317487/2136298