不知道是什么原因直接用Python脚本zabbix无法执行脚本,需要一个shell来启动
#! /bin/bash userid=$1 content=$2 python /data/zabbix/alertscripts/weixin.py $userid $content
#!/usr/bin/env python # _*_ coding:utf8 _*_ import urllib2 import simplejson as json import sys class weChat: def __init__(self,url,Corpid,Secret): url = '%s/cgi-bin/gettoken?corpid=%s&corpsecret=%s' % (url,Corpid,Secret) res = self.url_req(url) self.token = res['access_token'] def url_req(self,url,method='get',data={}): if method == 'get': req = urllib2.Request(url) res = json.loads(urllib2.urlopen(req).read()) elif method == 'post': req = urllib2.Request(url,data) res = json.loads(urllib2.urlopen(req).read()) else: print 'error request method...exit' sys.exit() return res def send_message(self,userlist,content,agentid=1000002): self.userlist = userlist self.content = content url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s' % self.token data = { "touser": "", "toparty": "", "totag": "", "msgtype": "text", "agentid": "0", "text": { "content": "" }, "safe":"0" } data['touser'] = userlist data['agentid'] = agentid data['text']['content'] = content data = json.dumps(data,ensure_ascii=False) # print data res = self.url_req(url,method='post',data=data) if res['errmsg'] == 'ok': print 'send sucessed!!!' else: print 'send failed!!' print res if __name__ == '__main__': userlist = sys.argv[1] # userlist = 'ZhangSen' content = sys.argv[2:] content = ' '.join(content) # content = 'test1' Corpid = 'xxx' #此处对应修改 Secret = 'xxx' #此处对应修改 url = 'https://qyapi.weixin.qq.com' wechat = weChat(url,Corpid,Secret) wechat.send_message(userlist,content)
上面这脚本有个问题不能发中文消息,下面这个可以发中文
#!/usr/bin/python # _*_coding:utf-8 _*_ import urllib2 import json import sys reload(sys) sys.setdefaultencoding('utf-8') def gettoken(corpid, corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: e.code e.read().decode("utf8") sys.exit() token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token def senddata(access_token, user, party, agent, subject, content): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token send_values = "{"touser":"" + user + "","toparty":"" + party + "","totag":"","msgtype":"text","agentid":"" + agent + "","text":{"content":"" + subject + content + ""},"safe":"0"}" send_request = urllib2.Request(send_url, send_values) response = json.loads(urllib2.urlopen(send_request).read()) print "send sucessed!!!" str(response) if __name__ == '__main__': user = str(sys.argv[1]) # 参数1:发送给用户的账号,必须关注企业号,并对企业号有发消息权限 party = str('2') # 参数2:发送给组的id号,必须对企业号有权限 agent = str('1000002') # 参数3:企业号中的应用id subject = str('') # 参数4:标题【消息内容的一部分】 content = str(sys.argv[2]) # 参数5:文本具体内容 corpid = 'xxx' # CorpID是企业号的标识 corpsecret = 'xxx' # corpsecretSecret是管理组凭证密钥 try: accesstoken = gettoken(corpid, corpsecret) senddata(accesstoken, user, party, agent, subject, content) except Exception, e: print "error" str(e) + "Error Please Check "corpid" or "corpsecret" Config"