公司已經(jīng)搭建號(hào)nagios,通過(guò)nagiosql界面管理,下面通過(guò)微信企業(yè)公眾號(hào)實(shí)現(xiàn)微信告警!
創(chuàng)新互聯(lián)專注于網(wǎng)站建設(shè),為客戶提供成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)、網(wǎng)頁(yè)設(shè)計(jì)開(kāi)發(fā)服務(wù),多年建網(wǎng)站服務(wù)經(jīng)驗(yàn),各類網(wǎng)站都可以開(kāi)發(fā),成都品牌網(wǎng)站建設(shè),公司官網(wǎng),公司展示網(wǎng)站,網(wǎng)站設(shè)計(jì),建網(wǎng)站費(fèi)用,建網(wǎng)站多少錢,價(jià)格優(yōu)惠,收費(fèi)合理。
首先注冊(cè)個(gè)微信公共平臺(tái)賬號(hào)
申請(qǐng)企業(yè)公眾號(hào)地址:https://qy.weixin.qq.com/
帳號(hào)類型選擇企業(yè)號(hào),注冊(cè)步驟略過(guò),申請(qǐng)好后登陸公眾號(hào)后臺(tái)創(chuàng)建應(yīng)用。
通訊錄-創(chuàng)建組織架構(gòu)、標(biāo)簽
添加通訊錄人員信息,可以手動(dòng)一個(gè)個(gè)添加,也可以批量導(dǎo)入。
我們要提前把成員信息添加進(jìn)組織部門,必填項(xiàng)+手機(jī)號(hào)或者微信號(hào),這樣別人掃描二維碼的時(shí)候才能成功關(guān)注企業(yè)號(hào)。
注意:這里有兩個(gè)我們要用到信息,一個(gè)組織部門的ID,一個(gè)部門成員的賬號(hào)(賬號(hào)是自己手動(dòng)指定的,不同于微信號(hào),最好是字母加數(shù)字)
應(yīng)用中心-新建應(yīng)用-消息型應(yīng)用-設(shè)置頭像、名稱、應(yīng)用范圍等
點(diǎn)擊應(yīng)用中心-告警測(cè)試應(yīng)用
可以查看該應(yīng)用的應(yīng)用ID(這個(gè)ID后面會(huì)用到)
設(shè)置-權(quán)限管理-新建-配置管理組
這里的CorpID、Secret后面會(huì)用到。接受告警信息的用戶關(guān)注該企業(yè)號(hào),完成身份認(rèn)證。
通過(guò)腳本發(fā)送微信告警信息
第一步 獲取access_token
正常情況下AccessToken有效期為7200秒,之后需要重新獲取,參考文檔http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8
Https請(qǐng)求方式:
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=id&corpsecret=secrect
id、Secret對(duì)應(yīng)上面應(yīng)用中的信息。替換上續(xù)信息后直接瀏覽器里打開(kāi)或者linux cur http://...就可以獲取access_token信息,
微信企業(yè)號(hào)接口調(diào)試工具方式:
地址:http://qydev.weixin.qq.com/debug
第二步 發(fā)送消息
通過(guò)微信企業(yè)號(hào)接口調(diào)試工具來(lái)發(fā)送消息
查看公眾號(hào)里信息:
其中的body部分可以這樣定義
{
"toparty": "2", //2 通訊錄中對(duì)應(yīng)的部門ID,
"msgtype": "text", //text 消息類型
"agentid": "6", //6 企業(yè)應(yīng)用的id,這里對(duì)應(yīng)的是上面創(chuàng)建的測(cè)試應(yīng)用ID
"text": {
"content": "Hello Justin!" //消息內(nèi)容
},
"safe":"0" //表示是否是保密消息,0表示否,1表示是,默認(rèn)0
}
具體參數(shù)可以參考官方文檔:
http://qydev.weixin.qq.com/wiki/index.php?title=消息類型及數(shù)據(jù)格式#text.E6.B6.88.E6.81.AF
通過(guò)curl方式
[root@localhost sh]# curl https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=DJZi3l051NuXz7rmWZOrugc9D6GlKewiqW3VewnCkAgvBFNQngImvYfPtdfHcQWncJ -d "{ \ > \"toparty\": \"2\", \ > \"msgtype\": \"text\", \ > \"agentid\": 6, \ > \"text\": { \ > \"content\": \"Hello Justin_peng! \" \ > }, \ > \"safe\":\"0\" \ > }" {"errcode":0,"errmsg":"ok"}[root@localhost sh]#
部門ID可以在通訊錄中選擇修改部門查看對(duì)應(yīng)ID
至此微信端配置完成,下面在nagios上配置
安裝Python-3.4
安裝openssl與openssl-devel包,否則使用python發(fā)送微信連接時(shí)會(huì)報(bào)urllib2.URLError: <urlopen error unknown url type: https>這個(gè)錯(cuò)誤,是因?yàn)閜ython沒(méi)有SSL模塊,需重新編譯安裝python
[root@localhost libexec]# yum -y install openssl openssl-devel [root@localhost libexec]# wget [root@localhost libexec]# tar zxvf Python-3.4.3.tgz [root@localhost libexec]# cd Python-3.4.3/Modules/ [root@localhost Modules]# vim Setup.dist # Socket module helper for SSL support; you must comment out the other # socket line above, and possibly edit the SSL variable: SSL=/usr/local/ssl _ssl _ssl.c \ -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \ -L$(SSL)/lib -lssl -lcrypto # The crypt module is now disabled by default because it breaks builds [root@localhost Modules]#
修改Setup.dist,去掉以上幾行的注釋,使其支持發(fā)送https請(qǐng)求。
[root@localhost Modules]# cd .. [root@localhost Python-3.4.3]# ./configure --prefix=/usr/local/python-3.4 [root@localhost Python-3.4.3]# make && make install [root@localhost Python-3.4.3]# vim /etc/profile export PATH="/usr/local/python-3.4/bin:$PATH" #文末添加 [root@localhost Python-3.4.3]# source /etc/profile [root@localhost Python-3.4.3]# cd /usr/local/nagios/python/ [root@localhost python]# vim Notify-host-by-weixin-party.py import urllib.request import json import sys #以上是導(dǎo)入模塊 #創(chuàng)建獲取AccessToken的方法 def gettoken(corp_id,corp_secret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret try: token_file = urllib.request.urlopen(gettoken_url) except urllib.error.HTTPError as e: print(e.code) print(e.read().decode("utf8")) token_data = token_file.read().decode('utf-8') token_json = json.loads(token_data) token_json.keys() token = token_json['access_token'] return token #這里是發(fā)送消息的方法 def senddata(access_token,notify_str): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token #我傳入的參數(shù)是一段字符串每個(gè)信息用separator連起來(lái),只要再用字符串的split("separator")方法分開(kāi)信息就可以了。 notifydata = notify_str.split("separator") party = notifydata[0] cationtype = notifydata[1] name = notifydata[2] state = notifydata[3] address = notifydata[4] output = notifydata[5] datatime = notifydata[6] # content = '[擦汗]Host Notification[擦汗]\n\n類型: ' + cationtype + '\n主機(jī)名: ' + name + '\n狀態(tài): ' + state + '\nIP地址: ' + address + '\n摘要: ' + output + '\n時(shí)間: ' + datatime + '\n' if cationtype == "RECOVERY": content = '[噓]' + address + ' is ' + state + '[噓]\n\nIP地址: ' + address + '\n主要用途: ' + name + '\n當(dāng)前狀態(tài): ' + state + '\n\n日志摘要: ' + output + '\n檢測(cè)時(shí)間: ' + datatime + '\n' else: content = '[擦汗]' + address + ' is ' + state + '[擦汗]\n\nIP地址: ' + address + '\n主要用途: ' + name + '\n當(dāng)前狀態(tài): ' + state + '\n\n日志摘要: ' + output + '\n檢測(cè)時(shí)間: ' + datatime + '\n' send_values = { "toparty":party, "totag":"2", "msgtype":"text", "agentid":"15", "text":{ "content":content }, "safe":"0" } send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8') #設(shè)置為非ascii解析,使其支持中文 send_request = urllib.request.Request(send_url, send_data) response = urllib.request.urlopen(send_request) #這個(gè)是返回微信公共平臺(tái)的信息,調(diào)試時(shí)比較有用 msg = response.read() return msg default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) #我編輯的腳本是要獲取nagios傳入的一段參數(shù)的(字符串),下面這條代碼是獲取執(zhí)行腳本后獲取的第一個(gè)參數(shù)(經(jīng)測(cè)試nagios只能傳入一個(gè)參進(jìn)python,所以把所有包括用戶名跟報(bào)警主機(jī)報(bào)警信息放進(jìn)一個(gè)字符串里) notifystr = str(sys.argv[1]) corpid = 'wxb6162862801114c9da602' corpsecret = '2nCsNcHxepBCV4U9Lcf-23By1RGzU1Zs422tdJpKTQzqjQ1b26IFxP76ydG2rKkchGN6E' accesstoken = gettoken(corpid,corpsecret) msg = senddata(accesstoken,notifystr) print(msg) [root@localhost python]# vim Notify-service-by-weixin-party.py import urllib.request import json import sys def gettoken(corp_id,corp_secret): gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corp_id + '&corpsecret=' + corp_secret try: token_file = urllib.request.urlopen(gettoken_url) except urllib.error.HTTPError as e: print(e.code) print(e.read().decode("utf8")) 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,notify_str): send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token notifydata = notify_str.split("separator") party = notifydata[0] cationtype = notifydata[1] desc = notifydata[2] alias = notifydata[3] address = notifydata[4] state = notifydata[5] datatime = notifydata[6] output = notifydata[7] # content ='[擦汗]Service Notification [擦汗]\n\n類型: ' + cationtype + '\n\n服務(wù)名: ' + desc + '\n主機(jī)名: ' + alias + '\nIP址: ' + address + '\n狀態(tài): ' + state + '\n時(shí)間: ' + datatime + '\n摘要:\n' + output + '\n' if cationtype == "RECOVERY": content ='[鼓掌]' + desc + ' is ' + state + '[鼓掌]\n\nIP地址: ' + address + '\n主要用途: ' + alias + '\n服務(wù)狀態(tài): ' + desc + ' is ' + state + '\n檢測(cè)時(shí)間: ' + datatime + '\n日志摘要: \n' + output + '\n' else: content ='[擦汗]' + desc + ' is ' + state + '[擦汗]\n\nIP地址: ' + address + '\n主要用途: ' + alias + '\n服務(wù)狀態(tài): ' + desc + ' is ' + state + '\n檢測(cè)時(shí)間: ' + datatime + '\n日志摘要: \n' + output + '\n' send_values = { "toparty":party, "totag":"2", "msgtype":"text", "agentid":"15", "text":{ "content":content }, "safe":"0" } send_data = json.dumps(send_values, ensure_ascii=False).encode(encoding='UTF8') send_request = urllib.request.Request(send_url, send_data) response = urllib.request.urlopen(send_request) msg = response.read() return msg default_encoding = 'utf-8' if sys.getdefaultencoding() != default_encoding: reload(sys) sys.setdefaultencoding(default_encoding) notifystr = str(sys.argv[1]) corpid = 'wxb616286d28ds01114c9da602' corpsecret = '2nCsNcHxepBCdtgV4U9Lcf-23By1RGzUgh2Zs422tdJpKTQzqjQ1b26IFxP76ydG2rKkchGN6E' accesstoken = gettoken(corpid,corpsecret) msg = senddata(accesstoken,notifystr) print(msg) [root@localhost python]# chmod +x Notify-host-by-weixin-party.py [root@localhost python]# chmod +x Notify-service-by-weixin-party.py [root@localhost python]# ll Notify-host-by-weixin-party.py Notify-service-by-weixin-party.py -rwxr-xr-x. 1 root root 3040 Aug 22 14:32 Notify-host-by-weixin-party.py -rwxr-xr-x. 1 root root 2498 Aug 19 14:55 Notify-service-by-weixin-party.py [root@localhost python]#
上面腳本需要修改以下幾處成自己的信息:
send_values = {
"toparty":party,
"totag":"2",
"msgtype":"text",
"agentid":"15",
"text":{
"content":content
},
"safe":"0"
}
corpid = 'wxb6162862sf80113e14c9da602'
corpsecret = '2nCsNcHxepBCV4U9Lcf-23By1RGzU1Zs422tdJdpKTQzqjQ1gjyb26IFxP76ydG2rKkchGN6E'
通過(guò)命令行測(cè)試腳本
[root@localhost python]# /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/Notify-host-by-weixin-party.py "微信通訊錄組id號(hào)separator時(shí)間標(biāo)題separator主機(jī)名separator主機(jī)狀態(tài)separator主機(jī)地址separator主機(jī)輸出信息separator時(shí)間"
成功后會(huì)有 b'{"errcode":0,"errmsg":"ok"}' 的提示。
[root@localhost python]# /usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/notify-host-by-weixin-test.py "2separator20160923separatortest_serverseparatorupseparator10.10.2.132separatortestseparator17:30" b'{"errcode":0,"errmsg":"ok"}' [root@localhost python]#
上面第一條命令報(bào)錯(cuò),提示權(quán)限問(wèn)題,檢測(cè)腳本里CorpID、Secret對(duì)應(yīng)的管理組是否給了權(quán)限:設(shè)置-權(quán)限管理-對(duì)應(yīng)的管理組-應(yīng)用權(quán)限。我這里是沒(méi)把告警測(cè)試應(yīng)用加到腳本里CorpID、Secret對(duì)應(yīng)的管理組的應(yīng)用權(quán)限里。
接下來(lái)需要對(duì)接nagios報(bào)警了
定義發(fā)送微信的命令,修改commands.cfg文件,定義主機(jī)報(bào)警命令notify-host-by-weixin,及服務(wù)報(bào)警命令notify-service-by-weixin。然后修改templates.cfg文件,添加微信報(bào)警。我這里通過(guò)nagiosql管理nagios的。
在命令欄里定義報(bào)警命令:
notify-host-by-weixin-Party-Address4
/usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/Notify-host-by-weixin-party.py"$CONTACTADDRESS4$separator$NOTIFICATIONTYPE$separator$HOSTALIAS$separator$HOSTSTATE$separator$HOSTADDRESS$separator$HOSTOUTPUT$separator$LONGDATETIME$"
notify-service-by-weixin-Party-Address4
/usr/local/python-3.4/bin/python3.4 /usr/local/nagios/python/Notify-service-by-weixin-party.py "$CONTACTADDRESS4$separator$NOTIFICATIONTYPE$separator$SERVICEDESC$separator$HOSTALIAS$separator$HOSTADDRESS$separator$SERVICESTATE$separator$LONGDATETIME$separator$SERVICEOUTPUT$"
我這里是發(fā)送消息到組,
告警欄里添加微信告警人信息,上面命令中$CONTACTADDRESS4$調(diào)取的是附加地址4,我們把組ID寫到該欄,然后選擇主機(jī)命令、服務(wù)命令
至此,nagios通過(guò)微信告警完成!
名稱欄目:Nagios調(diào)用Python程序控制微信公眾平臺(tái)發(fā)布報(bào)警信息
當(dāng)前URL:http://aaarwkj.com/article24/jjhhje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、標(biāo)簽優(yōu)化、面包屑導(dǎo)航、全網(wǎng)營(yíng)銷推廣、靜態(tài)網(wǎng)站、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
移動(dòng)網(wǎng)站建設(shè)知識(shí)