使用Nagios监控网页
使用Nagios监控网页1. 问题的由来
对于一个网站来说,外部用户能够看到就是该网站的页面。网站页面能否被正常访问,以及显示是否正常势必会成为网站整体水平最直接的外在表现。
那么,如何才能在第一时间检测到网页是否正常,并且给相应的技术人员发出报警来及时解决问题,而不是等接到用户抱怨的电话后才在慌忙中仓促的解决问题呢?解决这个问题的关键就是要在第一时间发现问题,发现那些不能显示的网页或是显示不正常的网页,并及时发出报警。当然我们可以通过人工的方法去监测,但对于一些大型的、复杂的网站来说就不是很合适了,我们可以使用监控软件来解决这个问题。我所使用的就是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: .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=
How to handle redirected pages
-m, --pagesize=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
server的IP地址,用于在不能DNS的情况下
-p, --port=INTEGER
端口号,默认80
-u, --url=PATH
url,默认是/
-w, --warning=DOUBLE
warning状态的响应时间,单位是秒
-c, --critical=DOUBLE
critical状态的响应时间,单位是秒
-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主机监控网页。
http://blog.运维网.com/attachment/200902/200902191235028098597.jpg
图3.1
方式二、通过NRPE监控网页。
http://blog.运维网.com/attachment/200902/200902191235028110753.jpg
图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 www.testhost.test -u /index.html
}
方式二的配置方法略复杂一些,需要修改两台主机的配置文件。
修改NRPE的配置文件,增加如下内容。
#check webpage
command=/usr/local/nagios//libexec/check_http -H www.testhost.test -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)的方式将其一一纳入监控范围。
附件:http://down.运维网.com/data/2351823
页:
[1]