zabbix 微信报警
1 # cat /usr/lib/zabbix/alertscripts/wechat.py2 #!/usr/bin/python
3 #_*_coding:utf-8 _*_
4
5
6 import urllib,urllib2
7 import json
8 import sys
9 import simplejson
10
11>
12 sys.setdefaultencoding('utf-8')
13
14
15 def gettoken(corpid,corpsecret):
16 gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
17 printgettoken_url
18 try:
19 token_file = urllib2.urlopen(gettoken_url)
20 except urllib2.HTTPError as e:
21 print e.code
22 print e.read().decode("utf8")
23 sys.exit()
24 token_data = token_file.read().decode('utf-8')
25 token_json = json.loads(token_data)
26 token_json.keys()
27 token = token_json['access_token']
28 return token
29
30
31
32 def senddata(access_token,user,subject,content):
33
34 send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
35 send_values = {
36 "touser":“user”, #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
37 "toparty":"2", #企业号中的部门id。
38 "msgtype":"text", #消息类型。
39 "agentid":"1000002", #企业号中的应用id。
40 "text":{
41 "content":subject + '\n' + content
42 },
43 "safe":"0"
44 }
45 # send_data = json.dumps(send_values, ensure_ascii=False)
46 send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8')
47 send_request = urllib2.Request(send_url, send_data)
48 response = json.loads(urllib2.urlopen(send_request).read())
49 print str(response)
50
51
52 if __name__ == '__main__':
53 user = str(sys.argv) #zabbix传过来的第一个参数
54 subject = str(sys.argv)#zabbix传过来的第二个参数
55 content = str(sys.argv)#zabbix传过来的第三个参数
56
57 corpid ='11111111111111' #CorpID是企业号的标识
58 corpsecret = '222222222222222222'#corpsecretSecret是管理组凭证密钥
59 accesstoken = gettoken(corpid,corpsecret)
60 senddata(accesstoken,user,subject,content)
页:
[1]