首页 > 代码库 > zabbix 微信报警脚本
zabbix 微信报警脚本
#! /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=http://www.mamicode.com/{}): 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=http://www.mamicode.com/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 = ‘\n‘.join(content) # content = ‘test1‘ Corpid = ‘wwdc52e74bcaa0dfc2‘ #此处对应修改 Secret = ‘Y1Xh5eQmlc7QSJ1RblNXEjppUVYfqwvWhiKJmBncOBk‘ #此处对应修改 url = ‘https://qyapi.weixin.qq.com‘ wechat = weChat(url,Corpid,Secret) wechat.send_message(userlist,content)
zabbix 微信报警脚本
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。