微信个人告警脚本
微信个人告警:使用个人微信,发送到微信群组,微信好友
两个脚本执行:
1)能连接网络
2)先执行server.py,扫描登录微信,登录之后没有报错,打开新终端查看端口是否起来了
3)在zabbix上面添加报警媒介,将personsend.py 脚本添加进去
4)pip install wxpy wechat_sender -i https://pypi.douban.com/simple
-----------------------------------------------server.py
#-* coding:utf-8 from wechat_sender import * from wxpy import * import logging import sys defaultencoding = 'utf-8' if sys.getdefaultencoding() != defaultencoding: reload(sys) sys.setdefaultencoding(defaultencoding) bot = Bot(console_qr=True) group = bot.groups().search(unicode('微信测试'))[0] #group2 = bot.groups().search(unicode('ITstone'))[0] logging.basicConfig(level=logging.DEBUG) #group.send("接入成功!") #group2.send("接入成功!") print("微信登陆成功!进行监控报警功能!") print(group) listen(bot, token='test', receivers=[group],port=12222) #listen(bot, token='test2', receivers=[group2],port=15556)
-----------------------------------------------personsend.py
#!/usr/local/python/bin/python # -*- coding:UTF-8 -*- from wechat_sender import * import time import sys defaultencoding = 'utf-8' if sys.getdefaultencoding() != defaultencoding: reload(sys) sys.setdefaultencoding(defaultencoding) import ssl context = ssl._create_unverified_context() #sender = Sender(token='test2', receivers=unicode('ITstone'),port=15556) sender2 = Sender(token='test', receivers=unicode('微信测试'),port=15555) #设置微信群和监听端口 #while True: # time.sleep(10) # sender.send(message=time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) # sender.send(message='测试数据'+time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) messeges=sys.argv[2]+sys.argv[3] #sender.send(message=messeges) sender2.send(message=messeges)
微信企业号告警脚本
下面的这个微信脚本从网上拉取下来的,后面由于微信更新,修改了倒数第三行的值
import json import sys import simplejson reload(sys) sys.setdefaultencoding('utf-8') def gettoken(corpid,corpsecret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret print gettoken_url try: token_file = urllib2.urlopen(gettoken_url) except urllib2.HTTPError as e: print e.code print 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,subject,content,itemid): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token send_values = { "touser":user, #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。 "toparty":"2", #企业号中的部门id。 "msgtype":"text", #消息类型。 "agentid":"1000002", #企业号中的应用id。 "text":{ "content":subject + ' ' + content }, "safe":"0" } send_data = simplejson.dumps(send_values, ensure_ascii=False).encode('utf-8') send_request = urllib2.Request(send_url, send_data) response = json.loads(urllib2.urlopen(send_request).read()) print str(response) if __name__ == '__main__': user = str(sys.argv[1]) #zabbix传过来的第一个参数 subject = str(sys.argv[2]) #zabbix传过来的第二个参数 content = str(sys.argv[3]) #zabbix传过来的第三个参数 itemid=1 corpid = 'ww22655492448f1e3b' #CorpID是企业号的标识 # corpsecret = 'e62ikstjInYq8vAp7mjDmQylO6DlA2a-CH86YpRxd38' #corpsecretSecret是管理组凭证密钥 corpsecret='deH66xl3FZVFhcmKhGfnAHHIlXAOS_Nh76ODn7ZsmU4' #'这里填写应用对应的secret' accesstoken = gettoken(corpid,corpsecret) senddata(accesstoken,user,subject,content,itemid)
参考: