標籤:監控 nagios
監控思路:通過url訪問某一介面檔案的具體返回內容,正則匹配某一介面存活必定含有的字元,若有則證明介面存活,若無則介面有問題。
廢話不多說,上外掛程式:
#!/usr/bin/env python# -*- coding: utf-8 -*-import reimport urllibimport sysimport getoptdef usage(): print """Usage: check_api [-h|--help] [-u|--url url] [-S|--Str String]"Url: the url that you want to check;String: the string that you want to match;"For example,#/usr/local/nagios/libexec/check_api -u ‘http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577‘ -S AdList""" sys.exit(3)def getHtml(url): page = urllib.urlopen(url) html = page.read() return htmldef getString(Str, html): matchObj = re.search(Str, html, re.M) if matchObj: print("GET API OK:get %s successfully!" % Str) sys.exit(0) elif not matchObj: print("GET API CRITICAL:the API was error!") sys.exit(2) else: print("GET API UNKNOWN:the API was unknow!") sys.exit(3)try: options, args = getopt.getopt(sys.argv[1:], "hu:S:", "--help --url= --Str=", )except getopt.GetoptError: usage() sys.exit(3)for name, value in options: if name in ("-h", "--help"): usage() if name in ("-u", "--url"): url = value if name in ("-S", "--Str"): Str = valuehtml = getHtml(url)getString(Str, html)
返回結果:
[[email protected] ~]# /usr/local/nagios/libexec/check_api -u ‘http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577‘ -S AdList GET API OK:get AdList successfully!
一、上述外掛程式在客戶機上部署,將此外掛程式放入/usr/local/nagios/libexec/ 賦予可執行許可權和屬主組,修改/usr/local/nagios/etc/nrpe.cfg設定檔,添加
command[check_api]=/usr/local/nagios/libexec/check_api -u ‘http://xxx.com/ch1.ashx?jsoncallback=jQuery111104933076921983046_1479864376576&classid=12&systemtype=2&dataType=json&cityid=129&videoid=36065812&pageid=1&_=1479864376577‘ -S AdList
重啟nagios用戶端。
二、修改監控主機/usr/local/nagios/etc/objects/commands.cfg檔案,添加
define command{ command_name check_api command_line $USER1$/check_api -u $ARG1$ -S $ARG2$ }
修改監控主機/usr/local/nagios/etc/objects/services.cfg檔案,添加
##############################M-WEB-065 check_api#####################################define service{ host_name M-WEB-065 service_description check_api check_command check_nrpe!check_api max_check_attempts 5 normal_check_interval 5 retry_check_interval 2 check_period 24x7 notification_interval 10 notification_period 24x7 notification_options u,c,r contact_groups yunwei}
最後重啟nagios,查看監控結果
650) this.width=650;" src="http://s4.51cto.com/wyfs02/M02/8A/A0/wKiom1g1YHHQfSKQAAAngyVqCUQ707.png" title="QQ20161123172445.png" alt="wKiom1g1YHHQfSKQAAAngyVqCUQ707.png" />
本文出自 “echo xiayun” 部落格,謝絕轉載!
nagios自訂監控API外掛程式