首先我们要有一个企业微信,这个可以去官网,个人可注册
企业微信:
应用管理->创建应用 ->这里有两个值一会用到
右上角我的企业->最下面企业id 一会也会用到
发微信的python脚本/usr/local/zabbix/alertscripts/zabbix_vx.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2
agentid = '10xxxx'#这里写agentid
corpid = 'wwaxxxxx'#这里写企业ID
corpsecret = 'xxxxxxx'#这里写secret
#get tocken
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']
#send wechart
touser=sys.argv[1] #many user: 'zhangsan|wangwu'
content=sys.argv[2] #content
post_content = {
"touser":touser,
"agentid":agentid,
"msgtype": "text",
"text":{
"content":content,
}
}
json_content = json.dumps(post_content)
url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)
print(response.read().decode('utf-8'))
测试脚本:
[root@linux-node1 alertscripts]# /usr/local/zabbix/alertscripts/zabbix_vx.py 'LiJinZhu' 'port is down'
{"errcode":0,"errmsg":"ok","invaliduser":""}
errmsg显示ok,说明发送成功。