使用Nagios監控網頁

使用Nagios監控網頁

1. 問題的由來

    對於一個網站來說,外部用戶能夠看到就是該網站的頁面。網站頁面能否被正常訪問,以及顯示是否正常勢必會成爲網站整體水平最直接的外在表現。

    那麼,如何才能在第一時間檢測到網頁是否正常,並且給相應的技術人員發出報警來及時解決問題,而不是等接到用戶抱怨的電話後纔在慌忙中倉促的解決問題呢?解決這個問題的關鍵就是要在第一時間發現問題,發現那些不能顯示的網頁或是顯示不正常的網頁,並及時發出報警。當然我們可以通過人工的方法去監測,但對於一些大型的、複雜的網站來說就不是很合適了,我們可以使用監控軟件來解決這個問題。我所使用的就是Nagios軟件,它提供的插件(Plugins)中有相應的命令可以完成對網頁的監控。

   

2. 如何通過Nagios解決此類問題

    對於NagiosNRPE以及Nagios Plugins的安裝配置網站的資料很多,在我的Blog中也有相應的文章可以參考,在這裏就不再過多的說明了。

    想使用Nagios監控網頁狀況,Nagios插件中的一個命令不得不被提及,那就是check_http,我沒可以使用它來檢查網頁是否正常、可用。該命令的具體說明和用法如下。   

Usage: check_http -H <vhost> | -I <IP-address> [-u <uri>] [-p <port>]

       [-w <warn time>] [-c <critical time>] [-t <timeout>] [-L]

       [-a auth] [-f <ok | warn | critcal | follow>] [-e <expect>]

       [-s string] [-l] [-r <regex> | -R <case-insensitive regex>] [-P string]

       [-m <min_pg_size>:<max_pg_size>] [-4|-6] [-N] [-M <age>] [-A string]

       [-k string] [-S] [-C <age>] [-T <content-type>]

NOTE: .e 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 . 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=<ok|warning|critical|follow>

    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)

   

    通過check_http的幫助我們可以清楚的瞭解到該命令可以爲我們做什麼,以及如何去做。選項雖然很多,其實常被用到的卻只有幾項,而且很多選項是有默認值的一般無需設置。下面就我們需要用到的幾個選項做一個簡要的說明。

   

 

 

-H, --hostname=ADDRESS

主機名或域名

-I, --IP-address=ADDRESS

serverIP地址,用於在不能DNS的情況下

-p, --port=INTEGER

端口號,默認80

-u, --url=PATH

url,默認是/

-w, --warning=DOUBLE

warning狀態的響應時間,單位是秒

-c, --critical=DOUBLE

critical狀態的響應時間,單位是秒

-t, --timeout=INTEGER

連接超時時間,默認10

 

    舉一個簡單的實例,一個網站的域名是[url]www.testhost.test[/url],端口是默認80,需要監測它的主頁/index.htmlwarning時間和critical時間、 timeout時間使用默認值,不設置。具體命令書寫如下。   

$ ./check_http -H [url]www.testhost.test[/url] -u /index.html

HTTP OK HTTP/1.1 200 OK - 115357 bytes in 1.717 seconds |time=1.716934s;;;0.000000 size=115357B;;;0

   

    可以看出該網頁是正常的,如果網頁地址不對或顯示有錯誤也會有相應的反饋信息。

 

3. 具體配置Nagios來監控網頁

    通過Nagios監控網頁一般有兩種方式,一種是直接通過Nagios監控主機配置監控服務監視網頁;另一種是在某一安裝了NRPE的客戶端主機上配置監控命令,通過NRPE監控網頁情況,再將結果傳回給Nagios監控主機。通過一個裝有NRPE的客戶端作爲橋樑的方式可以使主機更加安全,減輕Nagios主機的負擔,同時可以避免在Nagios主機上配置DNS等不必要的麻煩。

    方式一、直接通過Nagios主機監控網頁。   


3.1

 

    方式二、通過NRPE監控網頁。   


3.2

 

    方式一的配置十分簡單,只需要在Nagios的配置文件裏添加一個服務即可。

    配置內容如下  

    修改./etc/objects/commands.cfg,增加如下內容。

#'check_http' check web page
define command{
        command_name    check_webpage
        command_line    $USER1$/check_http $ARG1$
        }
    修改./etc/objects/localhost.cfg,增加如下內容。

define host{
        use                     linux-server           
        host_name               web_pages
        alias                   web_pages
        address                 127.0.0.1
        }

 

 

#the check web pages . the remote host.
define service{
        use                     generic-service
        host_name                web_pages      ;主機名,爲了便於顯示可以定義一個虛擬的host
        service_de.ion     web page1
        check_command           check_webpage!-H [url]www.testhost.test[/url] -u /index.html
        }
   

    方式二的配置方法略複雜一些,需要修改兩臺主機的配置文件。

    修改NRPE的配置文件,增加如下內容。

#check webpage
command[check_webpage]=/usr/local/nagios//libexec/check_http -H [url]www.testhost.test[/url] -u /index.html
   

    修改Nagios配置文件,增加如下內容。

#the check_apache . the remote host.
define service{
        use                     generic-service
        host_name               hostname
        service_de.ion     web page
        check_command           check_nrpe! check_webpage
        }
   

    以上僅僅是舉個簡單的例子來說明,當然實際環境要更復雜、頁面要更多,可以通過增加服務(service)的方式將其一一納入監控範圍。

 

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章