在CentOS 6.2上部署nagios-3.3.1

 

在CentOS 6.2上部署nagios-3.3.1
1nagios简介:         
Nagios是一款用于系统和网络监控的应用程序并遵循GPL协定。它可以在你设定的条件下对主机和服务进行监控,在状态变差和变好的时候给出告警信息
Nagios通常由一个主程序(Nagios)、一个插件程序(Nagios-plugins)和四个可选的ADDON(NRPENSCA NSClient++NDOUtils)组成。Nagios的监控工作都是通过插件实现的,因此,NagiosNagios-plugins是服务器端工作所必须的组件。而四个ADDON
1NRPE:用来在监控的远程Linux/Unix主机上执行脚本插件以实现对这些主机资源的监控。
2NSCA:用来让被监控的远程Linux/Unix主机主动将监控信息发送给Nagios服务器(这在冗余监控模式中特别要用到)
3NSClient++:用来监控 Windows主机时安装在Windows主机上的组件。
4NDOUtils:则用来将Nagios的配置信息和各event产生的数据存入数据库,以实现这些数据的快速检索和处理。
这四个ADDON中,NRPENSClient++工作于客户端,NDOUtils工作于服务器端,而NSCA则需要同时安装在服务器端和客户端。
2Nagios的特征包括:
1、监控网络服务(SMTPPOP3HTTPNNTPPING 等);
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基本组件的运行依赖于httpdgccgd。可以通过以下命令来检查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)中创建NagiosWeb程序配置文件:
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”即可。
当然,您也可以通过以下方式将nagiosCGI程序运行于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主机
1NRPE简介:
Nagios监控远程主机的方法有多种,其方式包括SNMPNRPESSHNCSA等。这里介绍其通过NRPE监控远程Linux主机的方式。
NRPENagios Remote Plugin Executor)是用于在远端服务器上运行检测命令的守护进程,
它用于让Nagios监控端基于安装的方式触发远端主机上的检测命令,并将检测结果输出至监控端。而其执行的开销远低于基于SSH的检测方式,而且检测过程并不需要远程主机上的系统帐号等信息,其安全性也高于SSH的检测方式。
2、安装配置被监控端10.100.10.1010.100.10.12,分别在两个机器上进行配置。
1)先添加nagios用户
# useradd -s /sbin/nologin nagios
2NRPE依赖于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
检测结果如下: 

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