Zabbix實(shí)現(xiàn)微信報(bào)警功能
一、 申請(qǐng)企業(yè)微信賬號(hào),申請(qǐng)地址 https://qy.weixin.qq.com/
二、 登陸企業(yè)微信賬
圖一
圖二
2、添加微信賬號(hào)
圖一
圖二
完成以上步驟后 就完成了微信賬號(hào)的添加
三、新建應(yīng)用
圖一
圖二
圖三
圖四
以上四幅圖完成后就應(yīng)用創(chuàng)建完成
四、設(shè)置權(quán)限管理
圖一
圖二
圖三
完成以上三幅圖的操作,權(quán)限管理設(shè)置完成;到此微信設(shè)置已經(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 chenpeng import urllib2 import json import sys import time class WebChat(object): def __init__(self,CropID,Secret): self.CropID = CropID self.Secret = Secret def Get_Token(self,info): ''' :param info: 存儲(chǔ)執(zhí)行結(jié)果和執(zhí)行程序狀態(tài)碼code (0代表執(zhí)行成功,非零表示不成功) :return: ''' self.info = info gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s" % (self.CropID,self.Secret) try: #通過Get方式獲取token req = 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'] = 0 else: self.info['result'] = g_result self.info['code'] = 1 except Exception,e: self.info['code'] = 1 self.info['result'] = e def Send_Msg(self,touser,toparty,agentid,access_token,content,info,*args,**kwargs): ''' 發(fā)送信息到微信 :param touser: 部門成員id,zabbix中定義的微信接收者, 成員ID列表(消息接收者,多個(gè)接收者用‘|'分隔,最多支持1000個(gè))。 特殊情況:指定為@all,則向關(guān)注該企業(yè)應(yīng)用的全部成員發(fā)送 :param toparty: 部門id,定義了范圍,組內(nèi)成員都可接收到消息, 部門ID列表,多個(gè)接收者用‘|'分隔,最多支持100個(gè)。當(dāng)touser為@all時(shí)忽略本參數(shù) :param agentid: 企業(yè)應(yīng)用的id,整型。可在應(yīng)用的設(shè)置頁面查看 :param access_token: 根據(jù)CropID,Secret獲取的訪問token值 :param content: 濾出zabbix傳遞的第三個(gè)參數(shù), 表示發(fā)送微信消息的內(nèi)容消息內(nèi)容,最長不超過2048個(gè)字節(jié), 注意:主頁型應(yīng)用推送的文本消息在微信端最多只顯示20個(gè)字(包含中英文) :param info: 返回執(zhí)行結(jié)果信息{'result':None,'code':None};'code':0或者非零 ;0表示成功 非零表示失敗 :param args: :param kwargs: :return: ''' self.touser = touser self.toparty = toparty self.agentid = agentid self.conntent = content self.access_token = access_token self.info = info purl = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % (access_token) data = { "touser": "", "toparty": "", "totag": "", #標(biāo)簽ID列表,多個(gè)接收者用‘|'分隔,最多支持100個(gè)。當(dāng)touser為@all時(shí)忽略本參數(shù),非必須 "msgtype": "text", #必須 "agentid": "", #必須 "text": { "content": "" #必須 }, "safe": "0" # 表示是否是保密消息,0表示否,1表示是,默認(rèn)0 } data['touser'] = self.touser data['agentid'] = self.agentid data['toparty'] = self.toparty data['text']['content']=self.conntent data = 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'] = e self.info['code'] = 1 if __name__ == '__main__': reload(sys) sys.setdefaultencoding('utf-8') def log(date, touser, content,info): ''' 發(fā)送的日志打印日志 :param date: 時(shí)間 :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 = 1 touser = '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對(duì)應(yīng)的是第四步《設(shè)置權(quán)限管理》中圖三對(duì)應(yīng)的CropID 和 Secret
代碼63行中的data數(shù)據(jù),請(qǐng)參考微信接口文檔
地址: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實(shí)現(xiàn)微信報(bào)警功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
淺談Pycharm調(diào)用同級(jí)目錄下的py腳本bug
今天小編就為大家分享一篇淺談Pycharm調(diào)用同級(jí)目錄下的py腳本bug,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數(shù)據(jù)
大家好,本篇文章主要講的是python解析Chrome瀏覽器歷史瀏覽記錄和收藏夾數(shù)據(jù),感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下2022-02-02Python對(duì)口紅進(jìn)行數(shù)據(jù)分析來選定情人節(jié)禮物
情人節(jié)送小仙女什么禮物?讓我們來用Python對(duì)口紅進(jìn)行數(shù)據(jù)分析,那個(gè)女孩子會(huì)拒絕這樣精心挑選的禮物,感興趣的小伙伴快來看看吧2022-02-02CentOS 7下安裝Python3.6 及遇到的問題小結(jié)
這篇文章主要介紹了CentOS 7下安裝Python3.6 及遇到的問題小結(jié),需要的朋友可以參考下2018-11-11python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制
這篇文章主要介紹了python ImageDraw類實(shí)現(xiàn)幾何圖形的繪制與文字的繪制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02淺析Python中的getattr(),setattr(),delattr(),hasattr()
這篇文章主要介紹了Python中的getattr(),setattr(),delattr(),hasattr() 的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06