系統(tǒng)管理:使用Nagios監(jiān)控網(wǎng)頁(yè)

字號(hào):

1. 問(wèn)題的由來(lái)
    對(duì)于一個(gè)網(wǎng)站來(lái)說(shuō),外部用戶能夠看到就是該網(wǎng)站的頁(yè)面。網(wǎng)站頁(yè)面能否被正常訪問(wèn),以及顯示是否正常勢(shì)必會(huì)成為網(wǎng)站整體水平最直接的外在表現(xiàn)。
    那么,如何才能在第一時(shí)間檢測(cè)到網(wǎng)頁(yè)是否正常,并且給相應(yīng)的技術(shù)人員發(fā)出報(bào)警來(lái)及時(shí)解決問(wèn)題,而不是等接到用戶抱怨的電話后才在慌忙中倉(cāng)促的解決問(wèn)題呢?解決這個(gè)問(wèn)題的關(guān)鍵就是要在第一時(shí)間發(fā)現(xiàn)問(wèn)題,發(fā)現(xiàn)那些不能顯示的網(wǎng)頁(yè)或是顯示不正常的網(wǎng)頁(yè),并及時(shí)發(fā)出報(bào)警。當(dāng)然我們可以通過(guò)人工的方法去監(jiān)測(cè),但對(duì)于一些大型的、復(fù)雜的網(wǎng)站來(lái)說(shuō)就不是很合適了,我們可以使用監(jiān)控軟件來(lái)解決這個(gè)問(wèn)題。我所使用的就是Nagios軟件,它提供的插件(Plugins)中有相應(yīng)的命令可以完成對(duì)網(wǎng)頁(yè)的監(jiān)控。
    2. 如何通過(guò)Nagios解決此類(lèi)問(wèn)題
    對(duì)于Nagios、NRPE以及Nagios Plugins的安裝配置網(wǎng)站的資料很多,在我的Blog中也有相應(yīng)的文章可以參考,在這里就不再過(guò)多的說(shuō)明了。
    想使用Nagios監(jiān)控網(wǎng)頁(yè)狀況,Nagios插件中的一個(gè)命令不得不被提及,那就是check_http,我沒(méi)可以使用它來(lái)檢查網(wǎng)頁(yè)是否正常、可用。該命令的具體說(shuō)明和用法如下。
    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] [-S] [-C ] [-T ]
    NOTE: One or both of -H and -I must be specified
    Options:
    -h, --help
    Print detailed help screen
    -V, --version
    Print version information
    -H, --hostname=ADDRESS
    Host name argument for servers using host headers (virtual host)
    Append a port to include it in the header (eg: example.com:5000)
    -I, --IP-address=ADDRESS
    IP address or name (use numeric address if possible to bypass DNS lookup).
    -p, --port=INTEGER
    Port number (default: 80)
    -4, --use-ipv4
    Use IPv4 connection
    -6, --use-ipv6
    Use IPv6 connection
    -S, --ssl
    Connect via SSL. Port defaults to 443
    -C, --certificate=INTEGER
    Minimum number of days a certificate has to be valid. Port defaults to 443
    (when this option is used the url is not checked.)
    -e, --expect=STRING
    String to expect in first (status) line of server response (default:
    HTTP/1.)
    If specified skips all other status line logic (ex: 3xx, 4xx, 5xx processing)
    -s, --string=STRING
    String to expect in the content
    -u, --url=PATH
    URL to GET or POST (default: /)
    -P, --post=STRING
    URL encoded http POST data
    -N, --no-body
    Don’t wait for document body: stop reading after headers.
    (Note that this still does an HTTP GET or POST, not a HEAD.)
    -M, --max-age=SECONDS
    Warn if document is more than SECONDS old. the number can also be of
    the form "10m" for minutes, "10h" for hours, or "10d" for days.
    -T, --content-type=STRING
    specify Content-Type header media type when POSTing
    -l, --linespan
    Allow regex to span newlines (must precede -r or -R)
    -r, --regex, --ereg=STRING
    Search page for regex STRING
    -R, --eregi=STRING
    Search page for case-insensitive regex STRING
    --invert-regex
    Return CRITICAL if found, OK if not
    -a, --authorization=AUTH_PAIR
    Username:password on sites with basic authentication
    -A, --useragent=STRING
    String to be sent in http header as "User Agent"
    -k, --header=STRING
     Any other tags to be sent in http header. Use multiple times for additional headers
    -L, --link
    Wrap output in HTML link (obsoleted by urlize)
    -f, --onredirect=
    How to handle redirected pages
    -m, --pagesize=INTEGER<:INTEGER>
    Minimum page size required (bytes) : Maximum page size required (bytes)
    -w, --warning=DOUBLE
    Response time to result in warning status (seconds)
    -c, --critical=DOUBLE
    Response time to result in critical status (seconds)
    -t, --timeout=INTEGER
    Seconds before connection times out (default: 10)
    -v, --verbose
    Show details for command-line debugging (Nagios may truncate output)
    通過(guò)check_http的幫助我們可以清楚的了解到該命令可以為我們做什么,以及如何去做。選項(xiàng)雖然很多,其實(shí)常被用到的卻只有幾項(xiàng),而且很多選項(xiàng)是有默認(rèn)值的一般無(wú)需設(shè)置。下面就我們需要用到的幾個(gè)選項(xiàng)做一個(gè)簡(jiǎn)要的說(shuō)明。 選 項(xiàng) 說(shuō) 明
    -H, --hostname=ADDRESS 主機(jī)名或域名
    -I, --IP-address=ADDRESS server的IP地址,用于在不能DNS的情況下
    -p, --port=INTEGER 端口號(hào),默認(rèn)80
    -u, --url=PATH url,默認(rèn)是/
    -w, --warning=DOUBLE warning狀態(tài)的響應(yīng)時(shí)間,單位是秒
    -c, --critical=DOUBLE critical狀態(tài)的響應(yīng)時(shí)間,單位是秒
    -t, --timeout=INTEGER 連接超時(shí)時(shí)間,默認(rèn)10秒
     舉一個(gè)簡(jiǎn)單的實(shí)例,一個(gè)網(wǎng)站的域名是www.testhost.test,端口是默認(rèn)80,需要監(jiān)測(cè)它的主頁(yè)/index.html,warning時(shí)間和critical時(shí)間、 timeout時(shí)間使用默認(rèn)值,不設(shè)置。具體命令書(shū)寫(xiě)如下。
    $ ./check_http -H www.testhost.test -u /index.html
    HTTP OK HTTP/1.1 200 OK - 115357 bytes in 1.717 seconds |time=1.716934s;;;0.000000 size=115357B;;;0
    可以看出該網(wǎng)頁(yè)是正常的,如果網(wǎng)頁(yè)地址不對(duì)或顯示有錯(cuò)誤也會(huì)有相應(yīng)的反饋信息。
    3. 具體配置Nagios來(lái)監(jiān)控網(wǎng)頁(yè)
    通過(guò)Nagios監(jiān)控網(wǎng)頁(yè)一般有兩種方式,一種是直接通過(guò)Nagios監(jiān)控主機(jī)配置監(jiān)控服務(wù)監(jiān)視網(wǎng)頁(yè);另一種是在某一安裝了NRPE的客戶端主機(jī)上配置監(jiān)控命令,通過(guò)NRPE監(jiān)控網(wǎng)頁(yè)情況,再將結(jié)果傳回給Nagios監(jiān)控主機(jī)。通過(guò)一個(gè)裝有NRPE的客戶端作為橋梁的方式可以使主機(jī)更加安全,減輕Nagios主機(jī)的負(fù)擔(dān),同時(shí)可以避免在Nagios主機(jī)上配置DNS等不必要的麻煩。
    方式一、直接通過(guò)Nagios主機(jī)監(jiān)控網(wǎng)頁(yè)。
    
    方式二、通過(guò)NRPE監(jiān)控網(wǎng)頁(yè)。
    
    方式一的配置十分簡(jiǎn)單,只需要在Nagios的配置文件里添加一個(gè)服務(wù)即可。
    配置內(nèi)容如下
    修改./etc/objects/commands.cfg,增加如下內(nèi)容。
    #’check_http’ check web page
    define command{
    command_name check_webpage
    command_line $USER1$/check_http $ARG1$
    }
    修改./etc/objects/localhost.cfg,增加如下內(nèi)容。
    define host{
    uselinux-server
    host_nameweb_pages
    alias web_pages
    address 127.0.0.1
    }
    #the check web pages on the remote host.
    define service{
    usegeneric-service
    host_name web_pages;主機(jī)名,為了便于顯示可以定義一個(gè)虛擬的host
    service_description web page1
    check_command check_webpage!-H www.testhost.test -u /index.html
    }
    方式二的配置方法略復(fù)雜一些,需要修改兩臺(tái)主機(jī)的配置文件。
    修改NRPE的配置文件,增加如下內(nèi)容。
    #check webpage
    command[check_webpage]=/usr/local/nagios//libexec/check_http -H www.testhost.test -u /index.html
    修改Nagios配置文件,增加如下內(nèi)容。
    #the check_apache on the remote host.
    define service{
    usegeneric-service
    host_namehostname
    service_description web page
    check_command check_nrpe! check_webpage
    }
    以上僅僅是舉個(gè)簡(jiǎn)單的例子來(lái)說(shuō)明,當(dāng)然實(shí)際環(huán)境要更復(fù)雜、頁(yè)面要更多,可以通過(guò)增加服務(wù)(service)的方式將其一一納入監(jiān)控范圍。