在CentOS 6.2上部署nagios-3.3.1
1nagios简介:         
Nagios 是一款用于系统和网络监控的应用程序并遵循 GPL 协定。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息
Nagios 通常由一个主程序 (Nagios) 、一个插件程序 (Nagios-plugins) 和四个可选的 ADDON(NRPE NSCA NSClient++ NDOUtils) 组成。 Nagios 的监控工作都是通过插件实现的,因此, Nagios Nagios-plugins 是服务器端工作所必须的组件。而四个 ADDON
1 NRPE :用来在监控的远程 Linux/Unix 主机上执行脚本插件以实现对这些主机资源的监控。
2 NSCA :用来让被监控的远程 Linux/Unix 主机主动将监控信息发送给 Nagios 服务器 ( 这在冗余监控模式中特别要用到 )
3 NSClient++ :用来监控 Windows 主机时安装在 Windows 主机上的组件。
4 NDOUtils :则用来将 Nagios 的配置信息和各 event 产生的数据存入数据库,以实现这些数据的快速检索和处理。
这四个 ADDON 中, NRPE NSClient++ 工作于客户端, NDOUtils 工作于服务器端,而 NSCA 则需要同时安装在服务器端和客户端。
2Nagios的特征包括:
1 、监控网络服务( SMTP POP3 HTTP NNTP PING 等);
2 、监控主机资源(处理器负荷、磁盘利用率等);
3 、简单地插件设计使得用户可以方便地扩展自己服务的检测方法;
4 、并行服务检查机制;
5 、具备定义网络分层结构的能力,用 "parent" 主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;  
6 、当服务或主机问题产生与解决时将告警发送给联系人(通过 EMail 、短信、用户定义方式);
7 、具备定义事件句柄功能,它可以在主机或服务的事件发生时获取更多问题定位;  
8 、自动的日志回滚;
9 、可以支持并实现对主机的冗余监控;  
10 、可选的 WEB 界面用于查看当前的网络状态、通知和故障历史、日志文件等。
3nagios的优缺点:
优点:
1 )良好的稳定的系统管理代码
2 )在服务事件和主机事件之间良好的相关性
3 )命令检查配置文件的验证
4 )命令重新加载配置文件无需打扰 Nagios 的运行
5 )良好的文档
缺点:
1 )没有自动发现
2 )很弱的事件控制台
3 )无 OOTB 采集或性能数据的阈值
4 )没有简单的方法来接收和解释的 SNMP TRAP
5 )没有 MIB 编译器或浏览器
一、安装前的准备工作
 (1) 、解决安装 Nagios 的依赖关系:
Nagios 基本组件的运行依赖于 httpd gcc gd 。可以通过以下命令来检查 nagios 所依赖的 rpm 包是否已经完全安装:
# yum -y install httpd gcc glibc glibc-common gd gd-devel php php-mysql mysql mysql-devel mysql-server
说明:以上软件包您也可以通过编译源代码的方式安装,只是后面许多要用到的相关文件的路径等需要按照您的源代码安装时的配置逐一修改。此外,您还得按需启动必要的服务,如 httpd 等。
 (2) 、添加 nagios 运行所需要的用户和组:
# groupadd nagcmd
# useradd -G nagcmd nagios
# passwd nagios
apache 加入到 nagcmd 组,以便于在通过 web Interface 操作 nagios 时能够具有足够的权限: # usermod -a -G nagcmd apache
二、编译安装nagios10.100.10.11
# tar zxf nagios-3.3.1.tar.gz
# cd nagios-3.3.1
#./configure --with-command-group=nagcmd \
--enable-event-broker \
--with-httpd-conf=/etc/httpd/conf/ \
--prefix=/usr/local/nagios
# make all
# make install
# make install-init
# make install-commandmode
# make install-config
(2) 、为 email 指定您想用来接收 nagios 警告信息的邮件地址,默认是本机的 nagios 用户 : # vi /usr/local/nagios/etc/objects/contacts.cfg
email        nagios@localhost       # 这个是默认设置
(3) 、在 httpd 的配置文件目录 (conf.d) 中创建 Nagios Web 程序配置文件:
nagios# make install-webconf
(4) 、创建一个登录 nagios web 程序的用户,这个用户帐号在以后通过 web 登录 nagios 认证时所用:
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin
以上过程配置结束以后需要重新启动 httpd
# service httpd restart
三、编译安装nagios-plugins
nagios 的所有监控工作都是通过插件完成的,因此,在启动 nagios 之前还需要为其安装官方提供的插件。
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make
# make install
四、配置并启动Nagios
(1) 、把 nagios 添加为系统服务并将之加入到自动启动服务队列:
# chkconfig --add nagios
# chkconfig nagios on
 (2) 、检查其主配置文件的语法是否正确:
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
(3) 、如果上面的语法检查没有问题,接下来就可以正式启动 nagios 服务了:
# service nagios start
(4) 、配置 selinux
如果您的系统开启了 selinux 服务,则默认为拒绝 nagios web cgi 程序的运行。您可以通过下面的命令来检查您的系统是否开启了 selinux
#getenforce
如果上面命令的结果显示开启了 selinux 服务,您可以通过下面的命令暂时性的将其关闭: #setenforce 0
如果您想在以后完全关闭 selinux ,可以通过编辑 /etc/sysconfig/selinux 文件,将其中的 selinux 后面的值“ force ”修改为“ disable ”即可。
当然,您也可以通过以下方式将 nagios CGI 程序运行于 SELinux/targeted 模式而不用关闭 selinux
# chcon -R -t httpd_sys_content_t /usr/local/nagios/sbin
# chcon -R -t httpd_sys_content_t /usr/local/nagios/share
(5) 、通过 web 界面查看 nagios
http://10.100.10.11/nagios
登录时需要指定前面设定的 web 认证帐号和密码。

此时,监控本机的服务页面如下:

五、基于NRPE监控远程Linux主机
1 NRPE 简介:
Nagios 监控远程主机的方法有多种,其方式包括 SNMP NRPE SSH NCSA 等。这里介绍其通过 NRPE 监控远程 Linux 主机的方式。
NRPE Nagios Remote Plugin Executor )是用于在远端服务器上运行检测命令的守护进程,
它用于让 Nagios 监控端基于安装的方式触发远端主机上的检测命令,并将检测结果输出至监控端。而其执行的开销远低于基于 SSH 的检测方式,而且检测过程并不需要远程主机上的系统帐号等信息,其安全性也高于 SSH 的检测方式。
2 、安装配置被监控端 10.100.10.10 10.100.10.12 ,分别在两个机器上进行配置。
1 )先添加 nagios 用户
# useradd -s /sbin/nologin nagios
2 NRPE 依赖于 nagios-plugins ,因此,需要先安装之
nagios-plugins-1.4.16.tar.gz
# tar zxf nagios-plugins-1.4.16.tar.gz
# cd nagios-plugins-1.4.16
# ./configure --with-nagios-user=nagios --with-nagios-group=nagios
# make all
# make install
3 )安装 NRPE
# tar -zxvf nrpe-2.13.tar.gz
# cd nrpe-2.13
# ./configure --with-nrpe-user=nagios \
     --with-nrpe-group=nagios \
     --with-nagios-user=nagios \
     --with-nagios-group=nagios \
     --enable-command-args \
     --enable-ssl
# make all
# make install-plugin
# make install-daemon
# make install-daemon-config
4 )配置NRPE
# vi /usr/local/nagios/etc/nrpe.cfg
 
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_address=127.0.0.1
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=10.100.10.11
command_timeout=60
connection_timeout=300
debug=0
 
上述配置指令可以做到见名知义,因此,配置过程中根据实际需要进行修改即可。其中,需要特定说明的是 allowed_hosts 指令用于定义本机所允许的监控端的 IP 地址。
5 )启动 NRPE
# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d
为了便于 NRPE 服务的启动,可以将如下内容定义为 /etc/init.d/nrped 脚本:
# vi /etc/init.d/nrped
 
#!/bin/bash
# chkconfig: 2345 88 12
# description: NRPE DAEMON
NRPE=/usr/local/nagios/bin/nrpe
NRPECONF=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
         start)
                   echo -n "Starting NRPE daemon..."
                   $NRPE -c $NRPECONF -d
                   echo " done."
                   ;;
         stop)
                   echo -n "Stopping NRPE daemon..."
                   pkill -u nagios nrpe
                   echo " done."
         ;;
         restart)
                   $0 stop
                   sleep 2
                   $0 start
                   ;;
         *)
                   echo "Usage: $0 start|stop|restart"
                   ;;
         esac
exit 0
或者,也可以在 /etc/xinetd.d 目录中创建 nrpe 文件,使其成为一个基于非独立守护进程的服务,文件内容如下:
service nrpe
{
         flags = REUSE
         socket_type = stream
         wait = no
         user = nagios
         group = nagios
         server = /opt/nagios/bin/nrpe
         server_args = -c /etc/nagios/nrpe.cfg -i
         log_on_failure += USERID
         disable = no
}
此种情况下启动 NRPE 进程需要通过重启 xinetd 来实现。
6 )配置允许远程主机监控的对象
在被监控端,可以通过 NRPE 监控的服务或资源需要通过 nrpe.cfg 文件使用命令进行定义,定义命令的语法格式为: command[<command_name>]=<command_to_execute>
比如:
command[check_rootdisk]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p
command[check_swap]=/usr/local/nagios/libexec/check_disk -w 40% -c 20%
command[check_sensors]=/usr/local/nagios/libexec/check_sensors
command[check_users]=/usr/local/nagios/libexec/check_users -w 10 -c 20
command[check_load]=/usr/local/nagios/libexec/check_load -w 10,8,5 -c 20,18,15
command[check_zombies]=/usr/local/nagios/libexec/check_procs -w 5 -c 10 -s Z
command[check_all_procs]=/usr/local/nagios/libexec/check_procs -w 150 -c 200
六、配置监控端所要监控的服务和资源
手动添加一些监控的配置文件, 在监控端( 10.100.10.11 )的 /usr/local/nagios/etc/objects/ 添加:
首先是添加主机和服务:
   vi /usr/local/nagios/etc/objects/host10.cfg
define host {
        host_name idc010vm010
        alias idc010vm010
        address 10.100.10.10
        check_command check-host-alive
        max_check_attempts 5
        notification_interval 10
        notification_period 24x7
        notification_options d,u,r
        }
define service {
        host_name idc010vm010
        service_description check-host-alive
#       check_command check_nrpe! check_load
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive
        }
define service {
        host_name idc010vm010
        service_description check-disk
        check_command check_nrpe! check_df
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
#       check_command check-host-alive
        }
define service{
host_name idc010vm010
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm010
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm010
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
   Vi /usr/local/nagios/etc/objects/host12.cfg
define host {
        host_name idc010vm012
        alias idc010vm012
        address 10.100.10.12
        check_command check-host-alive
        max_check_attempts 5
        notification_interval 10
        notification_period 24x7
        notification_options d,u,r
 
        }
define service {
        host_name idc010vm012
        service_description check-host-alive
#       check_command check_nrpe! check_load
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
        check_command check-host-alive
 
        }
define service {
        host_name idc010vm012
        service_description check-disk
        check_command check_nrpe! check_df
        check_period 24x7
        max_check_attempts 4
        normal_check_interval 3
        retry_check_interval 2
        contact_groups admins
        notification_interval 10
        notification_period 24x7
        notification_options w,u,c,r
#       check_command check-host-alive
 
        }
define service{
host_name idc010vm012
service_description total_procs
check_command check_nrpe!check_total_procs
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm012
service_description ip_connets
check_command check_nrpe! check_ip_connets
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
define service{
host_name idc010vm012
service_description check-load
check_command check_nrpe!check_load
max_check_attempts 4
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
#contact_groups admins
}
然后是在 /usr/local/nagios/etc/nagios.cfg 里添加两行内容,如下所示:
# vi /usr/local/nagios/etc/nagios.cfg
 
cfg_file=/usr/local/nagios/etc/objects/host10.cfg
cfg_file=/usr/local/nagios/etc/objects/host12.cfg
 
     文件保存后,先测试一下 nagios 是否有语法错误,若没有,则重新启动 nagios
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
#service nagios restart
检测结果如下: