一,实验环境以及软件版本:

1,CentOS release 6.4(IP:192.168.1.110)[服务器监控端]

2,CentOS release 6.4(IP:192.168.1.129)

3,nagios-3.5.1;nagios-plugins-2.0.3.tar;nrpe-2.15.tar;check_mk-1.2.6.tar

二,Nagios 可以监控的功能有:

1、监控网络服务(SMTP、POP3、HTTP、NNTP、PING等);

2、监控主机资源(处理器负荷、磁盘利用率等);

3、简单地插件设计使得用户可以方便地扩展自己服务的检测方法;

4、并行服务检查机制;

5、具备定义网络分层结构的能力,用"parent"主机定义来表达网络主机间的关系,这种关系可被用来发现和明晰主机宕机或不可达状态;

6、当服务或主机问题产生与解决时将告警发送给联系人(通过EMail、短信、用户定义方式);

7、可以定义一些处理程序,使之能够在服务或者主机发生故障时起到预防作用;

8、自动的日志滚动功能;

9、可以支持并实现对主机冗余监控;

10、可选的WEB界面用于查看当前的网络状态、通知和故障历史、日志文件等;

三,Nagios 服务器端安装:

1,安装前准备

During portions of the installation you'll need to have root access to your machine.

Make sure you've installed the following packages on your linux installation before continuing.

  • Apache

  • PHP

  • GCC compiler

  • GD development libraries

2,Create a new nagios user account,Create a new nagcmd group for allowing external commands to be submitted through the web interface. Add both the nagios user and the apache user to the group.

useradd -s /sbin/nologin nagios

groupadd nagcmd 

usermod -G nagcmd nagios

usermod -G nagcmd apache [根据httpd运行时的属主]

3,编译安装nagios

tar xf nagios-3.5.1.gz

cd nagios

[root@Conan_ nagios]# ./configure --with-command-group=nagcmd --prefix=/usr/local/nagios

[root@Conan_ nagios]# make all

[root@Conan_ nagios]# make install

4,Install init script, sample config files and set permissions on the external command directory

[root@Conan_ nagios]# make install-init  

[root@Conan_ nagios]# make install-config 

[root@Conan_ nagios]# make install-commandmode

5,在Apache中使用基本认证的方式创建一个nagiosadmin的用户用于Nagios的WEB界面登录,用户登录口令和账号信息会存储到/usr/local/nagios/etc/passwd.users文件中。相关apache配置文件放到/etc/http/conf.d目录下,文件名是nagios.conf。此文件默认,不需要修改。

[root@Conan_ nagios]# make install-webconf

[root@Conan_ nagios]# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin

New password: 

Re-type new password: 

Adding password for user nagiosadmin

注意:Nagios主程序到此已经安装完成,当这只是一个控制中心,而能够起到服务监测和系统监测等功能的是众多Nagios的插件,因此在安装了Nagios平台之后还需要安装插件。

6,nagios插件安装

[root@Conan_ ~]# tar xf nagios-plugins-2.0.3.tar.gz 

[root@Conan_ ~]# cd nagios-plugins-2.0.3

[root@Conan_ nagios-plugins-2.0.3]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --prefix=/usr/local/nagios

[root@Conan_ nagios-plugins-2.0.3]# make && make install

7,修改命令执行环境,增加nagios至服务列表,启动nagios,httpd运行。

[root@Conan_ nagios]# vim /etc/profile.d/nagios.sh

增加:export PATH=$PATH:/usr/local/nagios/bin

[root@Conan_ nagios]# . /etc/profile.d/nagios.sh

[root@Conan_ nagios]# chkconfig --add nagios

可以在浏览器上访问链接http://192.168.1.110/nagios,输入账号和密码,如果能够正常看到页面,证明主程序和插件都安装和配置成功。

四,Nagios 被监控端插件安装:

      这里采用NRPE外部构件监测远程主机。NRPE外部构件可以在远程的Linux/Unix主机上执行插件程序。在被监测主机上部署NRPE,相当于一个守护进程负责监听。而在监测主机使用check_nrpe并通过SSL连接访问这个daemon,然后调用被监测方相应安装目录下的check_disk,check_load等脚本获取信息并将结果传递到监测主机。同时这些脚本也有能力监测到其他主机的相关信息。下面是官网上的一张图片:

nagios之通过nrpe实现监控编译安装_nagios

1,在192.168.1.129上安装,新建nagios账号

[root@conan3 ~]# useradd nagios

2,安装nagios-plugin

[root@conan3 ~]# cd nagios-plugins-2.0.3

[root@conan3 nagios-plugins-2.0.3]# ./configure --prefix=/usr/local/nagios

[root@conan3 nagios-plugins-2.0.3]# make && make install

3,对目录设置权限

[root@conan3 nagios-plugins-2.0.3]# chown nagios.nagios /usr/local/nagios

[root@conan3 nagios-plugins-2.0.3]# chown -R nagios.nagios /usr/local/nagios/libexec

4,安装nrpe-2.15

[root@conan3 ~]# cd nrpe-2.15

[root@conan3 nrpe-2.15]# ./configure

[root@conan3 nrpe-2.15]# make all

[root@conan3 nrpe-2.15]# make install-plugin

[root@conan3 nrpe-2.15]# make install-daemon

[root@conan3 nrpe-2.15]# make install-daemon-config

5,nrpe安装为依赖xinetd超级进程的非独立服务,所以前提是必须安装xinetd。一般系统都会自动安装该服务。执行下面的命令将NRPE安装为xinetd超级进程所管理的进程之一。

[root@conan3 nrpe-2.15]# make install-xinetd

/usr/bin/install -c -m 644 sample-config/nrpe.xinetd /etc/xinetd.d/nrpe 

系统提示创建了/etc/xinetd.d/nrpe文件,vim打开此文件,并且在最后添加允许实施监测的主机IP地址,这里是192.168.1.10,那么整个配置文件全文如下:

# default: on
# description: NRPE (Nagios Remote Plugin Executor)
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       = 127.0.0.1 192.168.1.110
}

6,修改/etc/services,执行命令对check_nrpe插件进行测试:

最后一行增加:nrpe            5666/tcp                # nrpe

[root@conan3 nrpe-2.15]# service xinetd restart

[root@conan3 nrpe-2.15]# netstat -tln | grep 5666

tcp        0      0 :::5666                     :::*           LISTEN

[root@conan3 nrpe-2.15]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1

NRPE v2.15

7,在监控主机上安装check_nrpe插件:

[root@Conan_ ~]# tar xf nrpe-2.15.tar.gz 

[root@Conan_ ~]# cd nrpe-2.15

[root@Conan_ nrpe-2.15]# ./configure

错误:checking for SSL headers... configure: error: Cannot find ssl headers

[root@Conan_ nrpe-2.15]# yum -y install openssl-devel

[root@Conan_ nrpe-2.15]# make all

[root@Conan_ nrpe-2.15]# make install-plugin 

此处只需要make install-plugin,然后测试与被监控端的通信

[root@Conan_ nrpe-2.15]# /usr/local/nagios/libexec/check_nrpe -H 192.168.1.129

NRPE v2.15

8,为使用check_nrpe插件建立Nagios命令定义以及服务定义,在commands.cfg中增加对check_nrpe的定义

[root@Conan_ nrpe-2.15]# vi /usr/local/nagios/etc/objects/commands.cfg

define command{
       command_name check_nrpe
       command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
       }

9,创建hosts.cfg主要用来指定被监控的主机地址以及相关属性信息

[root@Conan_ etc]# vim objects/hosts.cfg

define host{   
        use                  linux-server          #引用主机linux-server的属性信息,linux-server主机在templates.cfg文件中进行了定义。
        host_name              Nagios-Conan3          #主机名
        alias                 Nagios Conan3          #主机别名
        address                192.168.1.129         #被监控的主机地址,这个地址可以是ip,也可以是域名。
        }   
#定义一个主机组   
define hostgroup{      
        hostgroup_name           linux-servers        #主机组名称,可以随意指定。
        alias                 linux servers        #主机组别名
        members                Nagios-Conan3        #主机组成员,其中“Nagios-Linux”就是上面定义的主机。     
        }

10,创建services.cfg,并中定义对Nagios-Conan3的监控了

[root@Conan_ etc]# vim objects/services.cfg

      services.cfg文件主要用于定义监控的服务和主机资源,例如监控http服务、ftp服务、主机磁盘空间、主机系统负载等等。Nagios-Server 和Nagios-Windows 相关服务已在相应的配置文件中定义,这边只需要定义Nagios-Conan3 相关服务即可,这里只定义一个检测是否存活的服务来验证配置文件的正确性。

define service{

        use                local-service          #引用local-service服务的属性值,local-service在templates.cfg文件中进行了定义。

        host_name            Nagios-Conan3          #指定要监控哪个主机上的服务,“Nagios-Conan3”在hosts.cfg文件中进行了定义。

        service_description     check-host-alive         #对监控服务内容的描述,以供维护人员参考。

        check_command         check-host-alive         #指定检查的命令。

        }

11,在nagios.cfg中增加

cfg_file=/usr/local/nagios/etc/objects/hosts.cfg

cfg_file=/usr/local/nagios/etc/objects/services.cfg


出现的错误:

[root@Conan_ nagios]# service nagios restart

Running configuration check... CONFIG ERROR!  Restart aborted.  Check your Nagios configuration.

nagios -v /usr/local/nagios/etc/nagios.cfg 排查错误

[root@Conan_ nagios]# nagios -v /usr/local/nagios/etc/nagios.cfg 

Warning: Duplicate definition found for hostgroup 'linux-servers' (config file '/usr/local/nagios/etc/objects/hosts.cfg', starting on line 6)
Error: Could not add object property in file '/usr/local/nagios/etc/objects/hosts.cfg' on line 7.
   Error processing object config files!

原因分析:

在services.cfg和localhost.cfg中定义hostgroup_name时名字重复了,需要修改一下。


[root@Conan_ etc]# service nagios restart

Running configuration check...done.

Stopping nagios: done.

Starting nagios:This account is currently not available.

 done.

出现:This account is currently not available.

解决办法:

[root@Conan_ etc]# chsh -s /bin/bash nagios

Changing shell for nagios.

Shell changed.

[root@Conan_ etc]# service nagios restart

Running configuration check...done.

Stopping nagios: done.

Starting nagios: done.


测试成功:

nagios之通过nrpe实现监控编译安装_nrpe_02

nagios之通过nrpe实现监控编译安装_监控_03




参考资料:

   1,http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html

      2,Nagios官方网站:http://www.nagios.org/