使用check_http和webinject監控你的web服務 分享

前,對web服務的監控,主要有nagios插件check_http,以及第三方軟件webinjectphp

原文地址: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>


注意reporttype是nagios。
這樣就表示webinject會將它的檢測結果報告給nagios,由nagios來展示它的檢查結果。
另外,這裏還指定了默認的testcase文件是 zjwtest.xml 。
即當你不指定testcase文件時,它會使用此文件來作檢測。固然,你也能夠指定真正的testcase文件。

B、設計testcase文件,根據用戶的動做來模擬出各個步驟
以P4P的一個testcase文件爲案例來講明。
$ 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>


說明:
id=1,2,3... -- 標識是第幾個步驟

在 case 1 中,模擬用戶去get:http://agent.p4p.cn.yahoo.com/ 。而後檢查返回的結果中是否包含「loginsys」這個字符串。
若是包含,則說明檢測是 Passed 的,也就是用戶正常打開了這個頁面;不然說明用戶打開頁面失敗。

在 case 2 中,模擬用戶提交登錄信息。而後檢查服務器處理後返回的結果中是否包含「frame.htm」這個字符串。
若是包含,說明用戶提交的登錄信息是正確的,即登錄成功,轉向登錄後的一個頁面。

在 case 3中,模擬用戶get:http://agent.p4p.cn.yahoo.com/tts/index.php?type=2 。檢查返回結果中是否包含「test for new mysql」。
驗證用戶登錄成功後,用戶已進入相應的頁面,該頁面包含「test for new mysql」這個字符串。

C、在nagios中定義監控任務

#定義host
define host {
    use cnb-host
    host_name agent.p4p.cn.yahoo.com
    alias agent.p4p.cn.yahoo.com
    address agent.p4p.cn.yahoo.com
}
#定義contact和contactgroup
define contact {
    use base-contact
    contact_name p4p-L1-mail
    alias p4p-L1-mail
    host_notification_options n
    service_notification_options w,u,c,r,f
    email hui.xiah@alibaba-inc.com yuan.liu@alibaba-inc.com
    register 1       ; 0 = disable, 1 = enable
}
define contactgroup {
    contactgroup_name p4p-L1
    alias p4p common
    members p4p-L1-mail
}
#定義command
define command {
    command_name    webinject
    command_line    /home/y/conf/nagios/webinject/webinject.pl -c $ARG1$ $ARG2$
}
#定義service
define service {
    use base-http-ycn
    service_description p4p_agent
    host_name agent.p4p.cn.yahoo.com
    servicegroups http
    contact_groups p4p-L1
    check_command webinject!config.xml!oldp4pagent.xml
    register 1   ; 0 = disable, 1 = enable
}

這樣,一個模擬用戶登錄的監控服務就配置完了。
對應地,命令行下的檢測命令爲:
$sudo ./webinject.pl -c config.xml oldp4pagent.xml


搜索結果返回頁的監控相對簡單。
$ 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>


平常的維護,主要是根據app ops的調整,修改url的相關參數,和verifypositive的關鍵字。 關鍵字選擇的原則: 一是有表明性,能說明頁面的性質是正常的仍是不正常的。 二是相對固定,也就是關鍵字最好相對固定,不常常改動。
相關文章
相關標籤/搜索