一 使用目的
作为系统运维人员,对系统的监控是不可或缺的一项重要任务,当系统出现故障时能在第一时间得到通知并解决故障是每个运维人员职责。时下监控软件有很多比如:zabbix,cacti,nagios等,本文主要介绍nagios的一些基本功能。
二
Nagios工作原理
Nagios的功能是监控服务和主机,但是他自身并不包括这部分功能,所有的监控、检测功能都是通过各种插件来完成的。
启动Nagios后,它会周期性的自动调用插件去检测服务器状态,同时Nagios会维持一个队列,所有插件返回来的状态信息都进入队列,Nagios每次都从队首开始读取信息,并进行处理后,把状态结果通过web显示出来。
Nagios提供了许多插件,利用这些插件可以方便的监控很多服务状态。安装完成后,在nagios主目录下的/libexec里放有
nagios自带的可以使用的所有插件,如,check_disk是检查磁盘空间的插件,check_load是检查CPU负载的,等等。每一个插件可以
通过运行./check_xxx –h 来查看其使用方法和功能。
Nagios可以识别4种状态返回信息,即
0(OK)表示状态正常/绿色、1(WARNING)表示出现警告/黄色、2(CRITICAL)表示出现非常严重的错误/红色、3(UNKNOWN)表
示未知错误/深黄色。Nagios根据插件返回来的值,来判断监控对象的状态,并通过web显示出来,以供管理员及时发现故障
四种状态

Nagios 系统提供了一个插件NRPE。Nagios
通过周期性的运行它来获得远端服务器的各种状态信息。它们之间的关系如下图所示:
Nagios 通过NRPE
来远端管理服务
1. Nagios
执行安装在它里面的check_nrpe 插件,并告诉check_nrpe 去检测哪些服务。
2. 通过SSL,check_nrpe 连接远端机器上的NRPE
daemon
3. NRPE
运行本地的各种插件去检测本地的服务和状态(check_disk,..etc)
4. 最后,NRPE
把检测的结果传给主机端的check_nrpe,check_nrpe 再把结果送到Nagios状态队列中。
5. Nagios 依次读取队列中的信息,再把结果显示出来。
三 实验环境
nagios-server | 192.168.151.90 | Red Hat Enterprise Linux Server
release 5.7 |apache,php,nagios,nagios-plugin,nrpe
nagios-client | 192.168.151.69 | Red Hat Enterprise Linux Server
release 5.7 |nagios-plugin,nrpe
四 服务端安装软件
在nagios-server上装如下软件
httpd-2.4.6.tar.gz
php-5.4.10.tar.gz
nagios-3.4.3.tar.gz
nagios-plugins-1.5.tar.gz
nrpe-2.13.tar.gz
1 nagios安装
tar zxf nagios-3.4.3.tar.gz
cd nagios-3.4.3
./configure --prefix=/usr/local/nagios
make all
make install
make install-init
make install-commandmode
make install-config
2 nagios-plugins安装
tar zxf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --prefix=/usr/local/nagios
make
make install
3 nrpe安装,只需要安装check_nrpe,不需要配置文件
tar zxf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
make install-plugin
4 apache,php安装
tar zxf httpd-2.4.6.tar.gz
cd httpd-2.4.6
./configure --prefix=/usr/local/apache
make
make install
tar zxf php-5.4.10.tar.gz
cd php-5.4.10
./configure --prefix=/usr/local/php
--with-apxs2=/usr/local/apache/bin/apxs
make && make install
a apache整合php,红色字体为添加的部分
DirectoryIndex index.html index.php
AddType application/x-httpd-php
.php
b nagios web页面授权
ScriptAlias /nagios/cgi-bin
/usr/local/nagios/sbin
AuthType Basic
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
Alias /nagios /usr/local/nagios/share
AuthType Basic
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthUserFile /usr/local/nagios/etc/htpasswd
Require valid-user
c 创建验证文件
/usr/local/apache/bin/htpasswd -c /usr/local/nagios/etc/htpasswd
nagios
根据提示输入密码
在/usr/local/nagios/etc下面就生成了一个密码文件
启动apache和nagios
/usr/local/apache/bin/apachectl start
/etc/init.d/nagios start
现在可以在浏览器中访问nagios页面http://192.168.151.90/nagios
五 客户端软件安装
在客户端要装如下软件
nagios-plugins-1.5.tar.gz
nrpe-2.13.tar.gz
创建nagios用户
useradd nagios
软件安装
cd /usr/local/src
wget https://www.nagios-plugins.org/download/nagios-plugins-1.5.tar.gz
tar zxf nagios-plugins-1.5.tar.gz
cd nagios-plugins-1.5
./configure --prefix=/usr/local/nagios
make
make install
cd /usr/local/src
wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
tar zxf nrpe-2.13.tar.gz
cd nrpe-2.13
./configure
make all
cd ./src && make install-plugin
cd ./src && make install-daemon
make install-daemon-config
如果安装成功,在/usr/local/nagios下会有下面几个目录
[root@localhost ~]# ll /usr/local/nagios/
total 24
drwxrwxr-x 2 nagios nagios 4096 Nov 13 23:42 bin
drwxrwxr-x 2 nagios nagios 4096 Nov 13 23:42 etc
drwxrwxr-x 2 nagios nagios 4096 Nov 13 23:41 libexec
drwxr-xr-x 2 root root 4096 Nov 13 23:28 share
在libexec下会有一个check_nrpe文件
六 nagios配置
客户端配置
以xinetd方式启动nrpe,配置如下
cat /etc/xinetd.d/nrpe
service nrpe
{
flags
=
REUSE
socket_type =
stream
port
= 5666
wait
= no
user
=
nagios
group
=
nagios
server
=
/usr/local/nagios/bin/nrpe
server_args =
-c /usr/local/nagios/etc/nrpe.cfg --inetd
log_on_failure
+= USERID
disable =
no
only_from =
192.168.151.90
}
/etc/services中添加如下行
nrpe
5666/tcp
nrpe.cfg是nrpe的配置文件,对用户,磁盘,负载,进程及swap资源的监控配置如下:
cat /usr/local/nagios/etc/nrpe.cfg |grep -v \#|sed '/^$/d'
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
server_address=127.0.0.1
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1
dont_blame_nrpe=0
debug=0
command_timeout=60
connection_timeout=300
command[check_users]=/usr/local/nagios/libexec/check_users -w 5 -c
10
command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5
-c 30,25,20
command[check_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c
10% -p /dev/hda1
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs
-w 5 -c 10 -s Z
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w
150 -c 200
command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c
10%
-w=warnning -c=critical
服务端配置
文件目录含义如下:
|
文件名或目录名
|
用途
|
|
cgi.cfg
|
控制CGI访问的配置文件
|
|
nagios.cfg
|
Nagios 主配置文件
|
|
resource.cfg
|
变量定义文件,又称为资源文件,在些文件中定义变量,以便由其他配置文件引用,如$USER1$
|
|
objects
|
objects
是一个目录,在此目录下有很多配置文件模板,用于定义Nagios 对象
|
|
objects/commands.cfg
|
命令定义配置文件,其中定义的命令可以被其他配置文件引用
|
|
objects/contacts.cfg
|
定义联系人和联系人组的配置文件
|
|
objects/localhost.cfg
|
定义监控本地主机的配置文件
|
|
objects/printer.cfg
|
定义监控打印机的一个配置文件模板,默认没有启用此文件
|
|
objects/switch.cfg
|
定义监控路由器的一个配置文件模板,默认没有启用此文件
|
|
objects/templates.cfg
|
定义主机和服务的一个模板配置文件,可以在其他配置文件中引用
|
|
objects/timeperiods.cfg
|
定义Nagios 监控时间段的配置文件
|
|
objects/windows.cfg
|
监控Windows 主机的一个配置文件模板,默认没有启用此文件
|
本文配置示例如下:
nagios.cfg文件
cat nagios.cfg |grep cfg|grep -v \#
cfg_file=/usr/local/nagios/etc/objects/commands.cfg
cfg_file=/usr/local/nagios/etc/objects/contacts.cfg
cfg_file=/usr/local/nagios/etc/objects/timeperiods.cfg
cfg_file=/usr/local/nagios/etc/objects/templates.cfg
resource_file=/usr/local/nagios/etc/resource.cfg
objects下的文件
[root@master objects]# ll /usr/local/nagios/etc/objects/
total 68
-rw-rw-r-- 1 nagios nagios 8214 Oct 25 10:54
commands.cfg
-rw-rw-r-- 1 nagios nagios 388
Oct 25 10:14 contacts.cfg
-rw-r--r-- 1 root
root 2166 Oct
25 10:12 contacts.cfg.original
-rw-rw-r-- 1 nagios nagios 5403 Oct 24 11:43
localhost.cfg
-rw-rw-r-- 1 nagios nagios 3124 Oct 24 11:43
printer.cfg
-rw-rw-r-- 1 nagios nagios 3293 Oct 24 11:43
switch.cfg
-rw-rw-r-- 1 nagios nagios 11986 Oct 24 16:05 templates.cfg
-rw-rw-r-- 1 nagios nagios 3208 Oct 24 11:43
timeperiods.cfg
-rw-rw-r-- 1 nagios nagios 4019 Oct 24 11:43
windows.cfg
templates.cfg文件
[root@master objects]# tail -28 templates.cfg
define service {
name
linux-services
use
generic-service
max_check_attempts
3
normal_check_interval
3
retry_check_interval
2
check_period
24x7
notification_period
24x7
notification_interval
0
notification_options
w,u,c,r,f
contact_groups
admins
register
0
}
define host {
name
linux-host
use
generic-host
max_check_attempts
3
normal_check_interval
3
retry_check_interval
2
check_period
24x7
notification_period
24x7
notification_interval
0
notification_options
d,u,r,f
check_command
check-host-alive
contact_groups
admins
register
0
}
[root@master objects]#
到此,nagios安装结束,本文会继续更新
遇到的问题:
cgi页面不能解析,ie显示为乱码,firefox显示为下载cgi页面
解决:
apache 开启LoadModule cgid_module modules/mod_cgid.so
页面提示It appears as though you do not have permission to view
information for any of the services you requested...
解决:
打开cgi.cfg配置文件,里面有个参数,设置为1:
use_authentication=1
/usr/local/nagios/libexec/check_nrpe -H 192.168.151.69出现错误提示
CHECK_NRPE: Error - Could not complete SSL handshake
客户端,服务端版本是否一致
nrpe是否有daemon方式启动的进程,关闭,以xinetd方式启动