前,對web服務的監控,主要有nagios插件check_http,以及第三方軟件webinject。php
原文地址:http://hancang2000.blog.sohu.com/56964734.html
check_http能夠完成常規的web服務檢查。
如:
A、頁面請求的響應時間是否符合要求(相關選項:-t ,-w,-c )
B、頁面請求響應是否正確(相關選項爲:-e 。經常使用的選項值爲200,301或者302)
C、URL的檢查(相關選項爲:-H,-I,-u)
D、頁面大小是否符合期待值(相關選項爲:-m)
它的具體使用方法,能夠查看它的幫助:
$ sudo ./check_http -h
Usage: check_http -H | -I [-u ] [-p ]
[-w ] [-c ] [-t ] [-L]
[-a auth] [-f ] [-e ]
[-s string] [-l] [-r | -R ]
[-P string] [-m :] [-4|-6] [-N]
[-M ] [-A string] [-k string]
NOTE: One or both of -H and -I must be specified
幫助裏有各個選項的詳細說明,這裏省略了。
在nagios裏配置service前,通常先在命令行下進行測試:
$ sudo ./check_http -H cn.yahoo.com -u / -w 10 -c 20 -t 30
HTTP OK HTTP/1.1 200 OK - 77530 bytes in 0.021 seconds |time=0.021011s;10.000000;20.000000;0.000000 size=77530B;;;0
補充說明:
-H cn.yahoo.com 和 -u / 表示檢查的URL是: http://cn.yahoo.com/ ;
-w 10 表示超過10s響應則發出warning報警;
-c 20 表示超過20s響應則發出critical報警;
-t 30 表示超過30s響應則發出timeout報警;
經過命令行獲得的測試結果,你知道了:該URL響應的HTTP信息是200,頁面大小爲77530 bytes 。
經常使用的HTTP響應信息以下:
200 OK:一切正常,對GET和POST請求的應答文檔跟在後面。
301 Moved Permanently:客戶請求的文檔在其餘地方,新的URL在Location頭中給出,瀏覽器應該自動地訪問新的URL。
302 Found:相似於301,但新的URL應該被視爲臨時性的替代,而不是永久性的。
404 Not Found:沒法找到指定位置的資源。
500 Internal Server Error:服務器遇到了意料不到的狀況,不能完成客戶的請求。
其餘HTTP信息表示的含義請查閱相關文檔。
命令行測試獲取到以上信息後,在nagios裏定義監控任務:
#定義host:
define host {
use colo-vhost-domains
host_name cn.yahoo.com
alias cn.yahoo.com
address cn.yahoo.com
}
#定義contact和contactgroup:
define contact {
use base-web-contact
contact_name web-L1-mail
alias web-L1-mail
host_notification_options n
service_notification_options w,u,c,r,f
email mymontest@yahoo.com.cn
register 1 ; 0 = disable, 1 = enable
}
define contactgroup {
contactgroup_name web-L1
alias web-L1
members web-L1-mail
}
#定義服務檢查的command(這裏的ARG[1,2,3...]表示命令的參數,在service的定義中用「!」順序隔開各個參數):
define command {
command_name check_http_vhost_url_200_OK
command_line $USER1$/check_http -H $ARG4$ -w $ARG1$ -c $ARG2$ -m $ARG3$ -I $HOSTADDRESS$ -t 30 -e 200 -u '$ARG5$'
}
#最後定義service:
define service {
use base-http-ycn
service_description cn_homepage
host_name cn.yahoo.com
servicegroups ycn_check_url_ok
contact_groups web-L1
check_command check_http_vhost_url_200_OK!10!20!70000!cn.yahoo.com!/
register 1
}
這裏定義的service,在命令行對應的檢查命令爲:
$sudo ./check_http -H cn.yahoo.com -w 10 -c 20 -m 70000 -I cn.yahoo.com -t 30 -e 200 -u /
即:http://cn.yahoo.com/ 頁面響應應爲200,頁面大小不小於70K;
超過10s才響應時爲warning,超過20s才響應時爲critical,超過30s則爲timeout;
報警時,通知 web-L1這個組的聯繫人。
整合webinject,加強web服務監控。
webinject是一個用perl語言寫的第三方工具,其官方網站爲 http://www.webinject.org/ 。
下載解壓後便可使用。若是報錯,請確認你的系統的perl環境是否知足webinject的要求。
webinject能夠模擬用戶的 交互行爲 來檢測你提供的web服務是否正常。
它的大概工做原理是模擬用戶 get/post 一個URL,服務器處理後返回一個頁面結果,webinject檢查這個頁面中
是否包含某個特定的字符串,來講明服務的響應是不是正確的。
注意:就目前來講,webinject不支持 中文字符串 做爲關鍵字來檢查!
目前,主要應用它來檢測(mail,mybox,myweb,photo)登錄服務和(web,music,news...)搜索服務是否正常。
在命令行下,webinject的使用,主要包含如下三個文件:
webinject.pl -- perl語言寫的檢查程序,系統的perl環境需知足它的要求
config.xml -- 主配置文件,定義的一些全局參數
testcase.xml -- 檢查步驟的xml文件(此文件名可任意,這裏假定爲testcase.xml)
其中,最關鍵的是 testcase.xml 這個文件。
它將用戶的動做設計成一個一個步驟,而後檢查服務器的響應是不是指望的結果。
所以,它的設計,將直接影響到檢測結果的有效性和準確性。
平常的維護,也是維護這個文件。
命令行下的使用,通常爲:
$sudo ./webinject.pl --config=config.xml testcase.xml
或者:$sudo ./webinject.pl -c config.xml testcase.xml
下面來講明webinject和nagios整合的通常使用過程:
A、首先,將主配置文件 config.xml 配置成跟nagios整合的方式。html
$ cat ./config.xml
<testcasefile>zjwtest.xml</testcasefile>
<globalhttplog>onfail</globalhttplog>
<useragent>WebInject Application Tester</useragent>
<timeout>10</timeout>
<globaltimeout>20</globaltimeout>
<reporttype>nagios</reporttype>
$ cat oldp4pagent.xml
<testcases repeat="1">
<case
id="1"
description1="old p4p agent"
description2="old p4p agent"
method="get"
url="http://agent.p4p.cn.yahoo.com/"
parseresponse='skey" value="|"'
parseresponse1='encrypted" value="|"'
verifypositive="loginsys"
errormessage="Fail to open old p4p agent page"
/>
<case
id="2"
description1="Old p4p agent"
description2="home page"
method="post"
url="https://bssauth.3721.yahoo.com/loginverify.php"
postbody="skey={PARSEDRESULT}&encrypted={PARSEDRESULT1}&loginsys=agent&username=abcde&encrypt=&password=p4pwjj&word=1234&ok=%B5%C7+%C2%BC"
verifypositive="frame.htm"
errormessage="Login fail"
/>
<case
id="3"
description1="Old p4p agent"
description2="Job Sheet"
method="get"
url="http://agent.p4p.cn.yahoo.com/tts/index.php?type=2"
verifypositive="test for new mysql"
errormessage="Job Sheet"
/>
</testcases>
$ cat ./search_myweb_cn.xml
<testcases repeat="1">
<case
id="1"
description1="search_myweb_cn"
description2="search_myweb_cn"
method="get"
url="http://myweb.cn.yahoo.com/search.html?showp=%E5%91%A8%E6%9D%B0%E4%BC%A6&p=%E5%91%A8%E6%9D%B0%E4%BC%A6"
verifypositive="addp.html\?method=save&ou="
/>
</testcases>