Linux下監控網頁-Nagios(圖)
使用Nagios監控網頁
1. Nagios監控問題的由來
對于一個網站來說,外部用戶能夠看到就是該網站的頁面。網站頁面能否被正常訪問,以及顯示是否正常勢必會成為網站整體水平最直接的外在表現。
那么,如何才能在***時間檢測到網頁是否正常,并且給相應的技術人員發出報警來及時解決問題,而不是等接到用戶抱怨的電話后才在慌忙中倉促的解決問題呢?解決這個問題的關鍵就是要在***時間發現問題,發現那些不能顯示的網頁或是顯示不正常的網頁,并及時發出報警。當然我們可以通過人工的方法去監測,但對于一些大型的、復雜的網站來說就不是很合適了,我們可以使用監控軟件來解決這個問題。我所使用的就是Nagios軟件,它提供的插件(Plugins)中有相應的命令可以完成對網頁的監控。
2. 如何通過Nagios解決此類問題
對于Nagios、NRPE以及Nagios Plugins的安裝配置網站的資料很多,在我的Blog中也有相應的文章可以參考,在這里就不再過多的說明了。
想使用Nagios監控網頁狀況,Nagios插件中的一個命令不得不被提及,那就是check_http,我沒可以使用它來檢查網頁是否正常、可用。該命令的具體說明和用法如下。
- 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)
通過check_http的幫助我們可以清楚的了解到該命令可以為我們做什么,以及如何去做。選項雖然很多,其實常被用到的卻只有幾項,而且很多選項是有默認值的一般無需設置。下面就我們需要用到的幾個選項做一個簡要的說明。
選項說明:
-H, --hostname=ADDRESS主機名或域名
-I, --IP-address=ADDRESSserver的IP地址,用于在不能DNS的情況下
-p, --port=INTEGER端口號,默認80
-u, --url=PATHurl,默認是/
-w, --warning=DOUBLEwarning狀態的響應時間,單位是秒
-c, --critical=DOUBLEcritical狀態的響應時間,單位是秒
-t, --timeout=INTEGER連接超時時間,默認10秒
舉一個簡單的實例,一個網站的域名是www.testhost.test,端口是默認80,需要監測它的主頁/index.html,warning時間和critical時間、 timeout時間使用默認值,不設置。具體命令書寫如下。
- $ ./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
可以看出該網頁是正常的,如果網頁地址不對或顯示有錯誤也會有相應的反饋信息。
3. 具體配置Nagios來監控網頁
通過Nagios監控網頁一般有兩種方式,一種是直接通過Nagios監控主機配置監控服務監視網頁;另一種是在某一安裝了NRPE的客戶端主機上配置監控命令,通過NRPE監控網頁情況,再將結果傳回給Nagios監控主機。通過一個裝有NRPE的客戶端作為橋梁的方式可以使主機更加安全,減輕Nagios主機的負擔,同時可以避免在Nagios主機上配置DNS等不必要的麻煩。 方式一、直接通過Nagios主機監控網頁。
Linux下監控網頁-Nagios就介紹到這里了,下一節:Linux下監控網頁-NRPE
【編輯推薦】