一、 申請企業(yè)微信賬號,申請地址 https://qy.weixin.qq.com/

二、 登陸企業(yè)微信賬

圖一

圖二
2、添加微信賬號

圖一

圖二
完成以上步驟后 就完成了微信賬號的添加
三、新建應用

圖一

圖二

圖三

圖四
以上四幅圖完成后就應用創(chuàng)建完成
四、設置權(quán)限管理

圖一

圖二

圖三
完成以上三幅圖的操作,權(quán)限管理設置完成;到此微信設置已經(jīng)完成!
五、Zabbix Server配置

圖一

圖二

圖三
完成以上三幅圖中的配置,則zabbix server的配置已經(jīng)完成。
七、weixin.py程序內(nèi)容
#!/usr/bin/env python# encoding: utf-8# Create time 2016-10-08#Auth chenpengimport urllib2import jsonimport sysimport timeclass WebChat(object):def __init__(self,CropID,Secret):self.CropID = CropIDself.Secret = Secretdef Get_Token(self,info):''':param info: 存儲執(zhí)行結(jié)果和執(zhí)行程序狀態(tài)碼code (0代表執(zhí)行成功,非零表示不成功):return:'''self.info = infogurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (self.CropID,self.Secret)try:#通過Get方式獲取tokenreq = urllib2.Request(gurl)response = urllib2.urlopen(req)g_result = json.loads(response.read(),"UTF-8")if g_result .has_key('access_token'):self.info['result']= g_result ['access_token']self.info['code'] = 0else:self.info['result'] = g_resultself.info['code'] = 1except Exception,e:self.info['code'] = 1self.info['result'] = edef Send_Msg(self,touser,toparty,agentid,access_token,content,info,*args,**kwargs):'''發(fā)送信息到微信:param touser: 部門成員id,zabbix中定義的微信接收者,成員ID列表(消息接收者,多個接收者用‘|'分隔,最多支持1000個)。特殊情況:指定為@all,則向關(guān)注該企業(yè)應用的全部成員發(fā)送:param toparty: 部門id,定義了范圍,組內(nèi)成員都可接收到消息,部門ID列表,多個接收者用‘|'分隔,最多支持100個。當touser為@all時忽略本參數(shù):param agentid: 企業(yè)應用的id,整型。可在應用的設置頁面查看:param access_token: 根據(jù)CropID,Secret獲取的訪問token值:param content: 濾出zabbix傳遞的第三個參數(shù),表示發(fā)送微信消息的內(nèi)容消息內(nèi)容,最長不超過2048個字節(jié),注意:主頁型應用推送的文本消息在微信端最多只顯示20個字(包含中英文):param info: 返回執(zhí)行結(jié)果信息{'result':None,'code':None};'code':0或者非零 ;0表示成功 非零表示失敗:param args::param kwargs::return:'''self.touser = touserself.toparty = topartyself.agentid = agentidself.conntent = contentself.access_token = access_tokenself.info = infopurl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % (access_token)data = {"touser": "","toparty": "","totag": "", #標簽ID列表,多個接收者用‘|'分隔,最多支持100個。當touser為@all時忽略本參數(shù),非必須"msgtype": "text", #必須"agentid": "", #必須"text": {"content": "" #必須},"safe": "0" # 表示是否是保密消息,0表示否,1表示是,默認0}data['touser'] = self.touserdata['agentid'] = self.agentiddata['toparty'] = self.topartydata['text']['content']=self.conntentdata = json.dumps(data,ensure_ascii=False)try:#通過PUT方式獲取發(fā)送數(shù)據(jù)req = urllib2.Request(purl, data)response = urllib2.urlopen(req)res = json.loads(response.read())self.info['code'] = res['errcode']self.info['result'] = res['errmsg']except Exception,e:self.info['result'] = eself.info['code'] = 1if __name__ == '__main__':reload(sys)sys.setdefaultencoding('utf-8')def log(date, touser, content,info):'''發(fā)送的日志打印日志:param date: 時間:param touser: 發(fā)送給誰:param content: 發(fā)送的信息內(nèi)容:param info: 發(fā)送執(zhí)行的結(jié)果:return:'''msg = '%s %s %s 發(fā)送結(jié)果 - %s/n' % (date, touser, content, info)with open('msg.log', 'a') as f:f.write(msg)agentid = sys.argv[1]#agentid = 1touser = 'xxxxxxx@qq.com'toparty = ''content = sys.argv[2:]content = '/n'.join(content)#content = '測試'CropID = 'xxxxxxxxxxxxxxxxxxx'Secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'info={'result':None,'code':None}date = time.strftime('%Y-%m-%d %H:%M:%S')res=WebChat(CropID,Secret)res.Get_Token(info)if info['code'] == 0:access_token = info['result']res.Send_Msg(touser=touser, toparty=toparty, agentid=agentid, access_token=access_token,content=content,info=info)if info['code'] == 0:content = eval(content)log(date, touser, content,info)else:log(date, touser, content, info)else:log(date,touser,content,info)其中代碼114、115行中的CropID 和 Secret對應的是第四步《設置權(quán)限管理》中圖三對應的CropID 和 Secret
代碼63行中的data數(shù)據(jù),請參考微信接口文檔
地址:http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E
以上所述是小編給大家介紹的Zabbix實現(xiàn)微信報警功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!
新聞熱點
疑難解答
圖片精選