二、 登陸企業微信賬
圖一
圖二
2、添加微信賬號
圖一
圖二
完成以上步驟后 就完成了微信賬號的添加
三、新建應用
圖一
圖二
圖三
圖四
以上四幅圖完成后就應用創建完成
四、設置權限管理
圖一
圖二
圖三
完成以上三幅圖的操作,權限管理設置完成;到此微信設置已經完成!
五、Zabbix Server配置
圖一
圖二
圖三
完成以上三幅圖中的配置,則zabbix server的配置已經完成。
七、weixin.py程序內容
#!/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: 存儲執行結果和執行程序狀態碼code (0代表執行成功,非零表示不成功) :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): ''' 發送信息到微信 :param touser: 部門成員id,zabbix中定義的微信接收者, 成員ID列表(消息接收者,多個接收者用‘|'分隔,最多支持1000個)。 特殊情況:指定為@all,則向關注該企業應用的全部成員發送 :param toparty: 部門id,定義了范圍,組內成員都可接收到消息, 部門ID列表,多個接收者用‘|'分隔,最多支持100個。當touser為@all時忽略本參數 :param agentid: 企業應用的id,整型。可在應用的設置頁面查看 :param access_token: 根據CropID,Secret獲取的訪問token值 :param content: 濾出zabbix傳遞的第三個參數, 表示發送微信消息的內容消息內容,最長不超過2048個字節, 注意:主頁型應用推送的文本消息在微信端最多只顯示20個字(包含中英文) :param info: 返回執行結果信息{'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": "", #標簽ID列表,多個接收者用‘|'分隔,最多支持100個。當touser為@all時忽略本參數,非必須 "msgtype": "text", #必須 "agentid": "", #必須 "text": { "content": "" #必須 }, "safe": "0" # 表示是否是保密消息,0表示否,1表示是,默認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方式獲取發送數據 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): ''' 發送的日志打印日志 :param date: 時間 :param touser: 發送給誰 :param content: 發送的信息內容 :param info: 發送執行的結果 :return: ''' msg = '%s %s %s 發送結果 - %s ' % (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 = ' '.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對應的是第四步《設置權限管理》中圖三對應的CropID 和 Secret
代碼63行中的data數據,請參考微信接口文檔
地址: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實現微信報警功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com