nagios 中安装和使用nrpe

来源:互联网 发布:mac双系统option无效 编辑:程序博客网 时间:2024/05/16 14:12

在被监控主机上安装Nagios-plugins插件程序:
1.
添加Nagio用户
[root@KCentOS5A ~]# useradd nagios

2.解压Nagios-plugins压缩包
[root@KCentOS5A ~]# tar -zxvf nagios-plugins-1.4.10.tar.gz 4.
进入Nagios-plugins插件包目录
[root@KCentOS5A ~]# cd nagios-plugins-1.4.10

5.
预配置Nagios-plugins的安装路径
[root@KCentOS5A nagios-plugins-1.4.10]# ./configure --prefix=/usr/local/nagios

6.
预配置完成后会反馈信息并生成Makefile
-------------------------------------------------------
config.status: creating po/Makefile
            --with-apt-get-command: 
              --with-ping6-command: /bin/ping6 -n -U -w %d -c %d %s
               --with-ping-command: /bin/ping -n -U -w %d -c %d %s
                       --with-ipv6: yes
                      --with-mysql: no
                    --with-openssl: yes
                     --with-gnutls: no
                       --with-perl: /usr/bin/perl
             --enable-perl-modules: no
                     --with-cgiurl: /nagios/cgi-bin
               --with-trusted-path: /bin:/sbin:/usr/bin:/usr/sbin
-------------------------------------------------------

7.
编译Nagios-plugins插件程序
[root@KCentOS5A nagios-plugins-1.4.10]# make

8.
安装Nagios-plugins插件程序
[root@KCentOS5A nagios-plugins-1.4.10]# make install

9.
查看Nagios-plugins插件程序的安装
[root@KCentOS5A nagios-plugins-1.4.10]# ll /usr/local/nagios/
total 8
drwxr-xr-x 2 root root 4096 Oct 7 01:02 libexec
drwxr-xr-x 3 root root 4096 Oct 7 01:02 share

10.
递归更改Nagios主路径的属主
[root@KCentOS5A ~]# chown -R nagios.nagios /usr/local/nagios/

11.
检查Nagios主路径的属性
[root@KCentOS5A ~]# ll /usr/local/|grep nagios
drwxr-xr-x 4 nagios nagios 4096 Oct 7 01:02 nagios

[root@KCentOS5A ~]# ll /usr/local/nagios/
total 8
drwxr-xr-x 2 nagios nagios 4096 Oct 7 01:02 libexec
drwxr-xr-x 3 nagios nagios 4096 Oct 7 01:02 share


.再在被监控主机上安装NRPE扩展插件程序:
1.
解压NRPE的压缩包
[root@KCentOS5A ~]# tar -zxvf nrpe-2.9.tar.gz 

2.
进入NRPE包目录
[root@KCentOS5A ~]# cd nrpe-2.9

3.
预配置NRPE安装程序
[root@KCentOS5A nrpe-2.9]# ./configure

4.
预配置完毕后会反馈提示信息以及建立Makefile
-------------------------------------------------------
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating subst
config.status: creating include/config.h

*** Configuration summary for nrpe 2.9 08-13-2007 ***:
General Options:
-------------------------
NRPE port:    5666
NRPE user:    nagios
NRPE group:   nagios
Nagios user: nagios
Nagios group: nagios

Review the options above for accuracy. If they look okay,
type 'make all' to compile the NRPE daemon and client.
最后提示继续使用“make all”命令来对NRPE守护程序以及客户端程序进行编译。
-------------------------------------------------------

5.
NRPE程序进行编译
[root@KCentOS5A nrpe-2.9]# make all

6.
编译成功后会反馈提示信息
-------------------------------------------------------
*** Compile finished ***
If the NRPE daemon and client compiled without any errors, you
can continue with the installation or upgrade process.
Read the PDF documentation (NRPE.pdf) for information on the next
steps you should take to complete the installation or upgrade.
NRPE的包目录中有一份NRPE.pdf的手册,接下来就可以参考那份手册进行后续的操作了。
-------------------------------------------------------
安装NRPE插件程序,守护程序以及模板守护程序配置文件。
Install the NRPE plugin (for testing), daemon, and sample daemon config file.

7.
安装NRPE插件程序
[root@KCentOS5A nrpe-2.9]# make install-plugin

8.
安装NRPE守护程序
[root@KCentOS5A nrpe-2.9]# make install-daemon

9.
安装NRPE守护程序配置文件
[root@KCentOS5A nrpe-2.9]# make install-daemon-config

10.
检查NRPE程序的安装
[root@KCentOS5A nrpe-2.9]# ll /usr/local/nagios/
-------------------------------------------------------
total 16
drwxrwxr-x 2 nagios nagios 4096 Oct 7 01:16 bin
drwxrwxr-x 2 nagios nagios 4096 Oct 7 01:16 etc
drwxr-xr-x 2 nagios nagios 4096 Oct 7 01:15 libexec
drwxr-xr-x 3 nagios nagios 4096 Oct 7 01:02 share
-------------------------------------------------------


.在被监测主机上配置并运行NRPE
1.
配置NRPE的主配置文件
[root@KCentOS5A ~]# vi /usr/local/nagios/etc/nrpe.cfg 
这里主要交待一些关键重要的设定。
-------------------------------------------------------
pid_file=/var/run/nrpe.pid
设定NRPE运行的PID文件,这个默认就设定好了,不用更改。

server_port=5666
这个是NRPE守护进程占用的系统端口。

#server_address=0.0.0.0
server_address=192.168.1.9 #
这是被控端IP地址
设定系统监听NRPE的网络接口。一般设定具体的IP地址,如果要多个网卡的话就用逗号“,”来分隔多个IP地址。如果要监听系统全部的网络接口的话,可以使用“0.0.0.0”这个表示全部网络接口的特殊地址,但是不可以用通配符“*”

nrpe_user=nagios
nrpe_group=nagios
设定NRPE的宿主用户。

allowed_hosts=192.168.1.12
 #这是充许监控服务器IP地址
这里是设定允许与本机进行NRPE交互的主机的IP地址,也就是Nagios监测服务器的地址,如果要指定多个Nagios服务器的话,那么用逗号“,”来分隔多个IP地址。

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 
以上是NRPE默认就有的扩展插件命令,这是至关重要的地方了。当被监测主机运行NRPE守护进程的时候,同时就运行起来了这些在NRPE配置文件中定义的NRPE扩展命令了。可以看出这些扩展命令都已经指定了好了命令名、插件路径以及选项参数了。格式如下:

         command[NRPE
命令名]=插件路径/插件 选项参数选项参数2 ...

注意,如果要指定该被监测主机需要被监控的服务的话,就必须在这里都配置好NRPE命令并运行在NRPE守护进程里。这里没有配置的NRPE命令则Nagios监测服务器是无法监控到的。
-------------------------------------------------------

2.
找到NRPE运行的执行程序
[root@KCentOS5A ~]# ll /usr/local/nagios/bin/nrpe 
-rwxrwxr-x 1 nagios nagios 93420 Oct 7 01:16 /usr/local/nagios/bin/nrpe

3.
了解NRPE的执行程序的使用方式
[root@KCentOS5A ~]# /usr/local/nagios/bin/nrpe -h
-------------------------------------------------------
Usage: nrpe [-n] -c <config_file><mode>
运行方法:nrpe -c NRPE配置文件路径 运行模式

Options:
可用选项

   -n          = Do not use SSL
   
不使用SSL方式,一般都使用SSL

<config_file> = Name of config file to use
   
指定NRPE配置文件路径,这个NRPE主配置文件就是nrpe.cfg

<mode>        = One of the following two operating modes:
   
指定NRPE的运行方式,一共有2种可用的NRPE运行方式:
   
   -i          =    Run as a service under inetd or xinetd
   
以超级守护进程inetdxinetd方式运行NRPE,要通过这种方式运行的话还要安装和配置xinetd,一般不用。

   -d          =    Run as a standalone daemon
   
以独立守护进程方式运行NRPE,一般常用这种运行方式。
-------------------------------------------------------

4.
运行NRPE守护进程
[root@KCentOS5A ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d

5.
查看系统日志检查NRPE运行情况:
[root@KCentOS5A ~]# tail /var/log/messages
-------------------------------------------------------
Oct 7 01:45:10 KCentOS5A nrpe[21659]: Starting up daemon
说明已经顺利启动了NRPE守护进程。

Oct 7 01:45:10 KCentOS5A nrpe[21659]: Listening for connections on port 5666 
说明已经启动了5666端口为NRPE的监听端口。

Oct 7 01:45:10 KCentOS5A nrpe[21659]: Allowing connections from: 192.168.1.12 
说明已经打开对指定的Nagios监测服务器的访问允许。
-------------------------------------------------------
如果启动失败,提示已绑定,是因为nrpe服务已经启动过了,所以需要我们先把nrpe服务杀死,重新启动。
 检查NRPE的网络连接信息
-------------------------------------------------------
[root@KCentOS5A ~]# netstat -nap | grep nrpe
tcp        0      0 192.168.1.9:5666            0.0.0.0:*                   LISTEN      21659/nrpe          
unix 2      [ ]         DGRAM                    23374 21659/nrpe      
-------------------------------------------------------
 
kill   21659 
再重新启动。就可以了。
6.
检查NRPE的进程信息
[root@KCentOS5A ~]# ps aux | grep nrpe
-------------------------------------------------------
nagios 21659 0.0 0.3 4396 868 ? Ss 01:45 0:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
-------------------------------------------------------

7.
检查NRPE的网络连接信息
-------------------------------------------------------
[root@KCentOS5A ~]# netstat -nap | grep nrpe
tcp        0      0 192.168.1.9:5666            0.0.0.0:*                   LISTEN      21659/nrpe          
unix 2      [ ]         DGRAM                    23374 21659/nrpe      
-------------------------------------------------------

8.
NRPE运行添加加入系统启动脚本中
[root@KCentOS5A ~]# echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local 
到此,被监控主机要做的工作已经完成了。

Nagios监测服务器上安装NRPE核心扩展插件程序
相对被检测主机来说,Nagios监测服务器上做的NRPE安装就比较简单了,而且它主要监测别人而不是被别人监测,所以它不需要将NRPE作为守护进程运行起来,只要拥有NRPE的扩展插件功能就可以了。
1.
解压NRPE压缩包
[root@KCentOS5C ~]# tar -zxvf nrpe-2.9.tar.gz 

2.
进入NRPE包目录
[root@KCentOS5C ~]# cd nrpe-2.9

3.
预配置NRPE安装程序
[root@KCentOS5C nrpe-2.9]# ./configure 

4.
编译NRPE程序
[root@KCentOS5C nrpe-2.9]# make all

5.
安装NRPE扩展插件程序
[root@KCentOS5C nrpe-2.9]# make install-plugin
Nagios 
只要拥有NRPE的扩展插件功能就可以了,所以在Nagios监测服务器安装NRPE工作到这步就可以了。接下来要做的就是在Nagios监测服务器上配置NRPE的扩展插件命令,使得Nagios监测服务器能够定义nrpe命令,从而可以定义出远程系统监测服务


.在监控主机上配置NRPE的服务
1.
查看新扩展插件check_nrpe的使用方法
[root@KCentOS5C ~]# /usr/local/nagios/libexec/check_nrpe -h|less
-------------------------------------------------------
Usage: check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>]
使用方式:check_nrpe -H 主机名 -p NRPE端口 -c NRPE命令名 

Options:
选项:

<host>     = The address of the host running the NRPE daemon
主机,运行着NRPE守护进程的远程被监测主机名,并且该主机名必须在host里定义过。

[port]     = The port on which the daemon is running (default=5666)
端口,被监测的远程主机上运行NRPE的端口,默认是5666,如果是默认就不用指定。

[command] = The name of the command that the remote daemon should run
命令,这些命令名必须是被监测主机上NRPE守护进程运行着的。
-------------------------------------------------------

2.
command.cfg命令定义文件中添加NRPE命令。
[root@KCentOS5C ~]# vi /usr/local/nagios/etc/commands.cfg
-------------------------------------------------------
# NRPE Command
添加NRPE功能命令。

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

这里要说明几点:
1)这里定义的命令名就叫作nrpe
2$USER1$/check_nrpe会通过引用resource.cfg获得/usr/local/nagios/libexec/check_nrpe这个绝对路径。
3-H $HOSTADDRESS$ 用来获得指定被监测主机的IP地址,$HOSTADDRESS$变量会通过定义主机名查找到host段中的IP地址。
4-c $ARG1$ 用来指定被监测主机上NRPE守护进程运行着的NRPE命令名。 

6.
Nagios监控服务器上然后按照NRPE命令定义来添加NRPE远程监控服务:

service.cfg里面添加服务。


# Define a service to check SSH on the local machine.
# Disable notifications for this service by default, as not all users may have SSH enabled.

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       node2
        service_description             SSH
        check_command                   check_nrpe!check_ssh
        notifications_enabled           1
        normal_check_interval           10
        retry_check_interval            1
        }

# Define a service to check HTTP on the local machine.
# Disable notifications for this service by default, as not all users may have HTTP enabled.

define service{
        use                             generic-service         ; Name of service template to use
        host_name                       node2
        service_description             HTTP
        check_command                   check_nrpe!check_http
        notifications_enabled           0
        normal_check_interval           10
        retry_check_interval            1
        }

nrpe做代理收到服务器端信息之后,发给被监控的机器,被监控的机器在去查询nrpe.cfg里面的相关命令定义:command[check_http]=/usr/local/nagios/libexec/check_http -l 192.168.132.133
command[check_ssh]=/usr/local/nagios/libexec/check_ssh -4 -p 22 192.168.132.133
,提取自身服务器的响应之后再回送给服务器端。

0 0