1、nrpe实现监控远程主机原理 NRPE是nagios的一个功能扩展,它可在远程Linux/Unix主机上执行插件程序,通过在远程服务器上安装nrpe构件以及nagios插件程序,来向nagios监控平台提供该服务器的一些本地的情况。例如,cpu负载、内存使用、硬盘使用等等。
2、 配置nagios客户端 (1)安装nagios插件在nagios客户端主机上需要安装nrpe和nagios插件,Nrpe插件可以从nagios官方网站下载到,从http://www.nagios.org/download/addons下载最新稳定版本nrpe-2.12.tar.gz,然后开始安装和配置,基本操作如下:
[iyunv@test2 ~]# tar zxvf nagios-plugins-1.4.9.tar.gz
[iyunv@test2 ~]# cd nagios-plugins-1.4.9
[iyunv@test2 nagios-plugins-1.4.9]# ./configure [iyunv@test2 nagios-plugins-1.4.9]# make && make install [iyunv@test2 ~]# chown nagios.nagios /usr/local/nagios/
[iyunv@test2 ~]# chown -R nagios.nagios /usr/local/nagios/libexec (2)在客户端安装nrpe插件过程要比在服务端安装复杂,因为nrpe在客户端是作为一个守护进程在运行的,操作如下: [iyunv@test2 ~]# yum install -y openssl openssl-devel
[iyunv@test2 ~]# wget http://sourceforge.net/projects/ ... e_mirror=hivelocity [iyunv@test2 ~]# tar zxvf nrpe-2.14.tar.gz
[iyunv@test2 nrpe-2.14]# useradd nagios
[iyunv@test2 nrpe-2.14]# ./configure --enable-ssl --with-ssl-lib
[iyunv@test2 nrpe-2.14]# make all
[iyunv@test2 nrpe-2.14]# make install-plugin
[iyunv@test2 nrpe-2.14]# make install-daemon [iyunv@test2 nrpe-2.14]# make install-daemon-config
(3)配置nrpe nrpe的配置文件为/usr/local/nagios/etc/nrpe.cnf。 首先找到“server_address=127.0.0.1”将后面的地址改为客户端主机的ip地址,然后找到:“allowed_hosts=127.0.0.1”一行,将其改为:allowed_hosts=127.0.0.1, Nagios监控服务器的地址或域名修改这个配置的作用是声明合法的nrpe服务对象,没有在这里指定的地址是无法从本机的NRPE获得服务信息的。 “Nagios监控服务器的地址或域名”可以是ip地址,也可以是域名。可以根据自己的情况设定。
(4)启动nrpe守护进程 启动NRPE很简单,只需执行如下操作: /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d 建议将此命令加入到/etc/rc.local文件中,这样就可以开机自动运行NRPE守护进程了。 Nrpe守护进程默认的端口为5666,通过如下命令可以检测端口是否启动:
[iyunv@test2 ~]# netstat -antl|grep 5666 tcp 00 0.0.0.0:5666 0.0.0.0:*LISTEN
可以看到,nrpe守护进程端口5666已经启动了。
(5)测试nrpe功能 首先在nagios客户端本机上测试,执行如下命令:
[iyunv@test2 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.87.135
CHECK_NRPE: Error - Could not complete SSL handshake. 如果正常,应该出现如下信息:
[iyunv@nagios-client ~]# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1
NRPE v2.12
正常的返回值为被监控服务器上安装的NRPE的版本信息,如果能看到这些,表示NRPE已经正常工作了。 /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 CHECK_NRPE: Error - Could not complete SSL handshake. 此时出现此错误表明客户端没有安装openssl 不能完成ssl握手
(6)定义监控服务器内容 要监控一个远程服务器下的某些信息,首先要在远程服务器中定义监控的内容,例如,如果要监控一台远程服务器上的当前用户数、cpu负载、磁盘利用率、交换空间使用情况时,则需要在nrpe.conf中定义监控内容: 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_sda5]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda5 command[check_zombie_procs]=/usr/local/nagios/libex /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
其中,command后面中括号里面的内容就是定义的变量,变量名可以随意指定。
3、在服务端安装nrpe和配置nagios服务 (1)安装nrpe插件nrpe在服务端安装很简单,操作如下: [iyunv@test1 ~]# wget http://sourceforge.net/projects/nagios/files/nrpe-2.x/nrpe-2.14/nrpe-2.14.tar.gz/download?use_mirror=hivelocity&r=&use_mirror=hivelocity [iyunv@test1 ~]# tar zxvf nrpe-2.14.tar.gz
[iyunv@test1 ~]# cd nrpe-2.14
[iyunv@test1 nrpe-2.14]# yum install -y openssl openssl-devel [iyunv@test1 nrpe-2.14]# ./configure --enable-ssl --with-ssl-lib
[iyunv@test1 nrpe-2.14]#make all && make install-plugin && make install-daemon && make install-daemon-config [iyunv@test1 nrpe-2.14]# cp /usr/local/nagios/etc/nrpe.cfg /usr/local/nagios/bin
通过“make install-plugin”命令将check_nrpe插件默认安装到了/usr/local/nagios/libexec目录下。
(2)测试插件与客户端是否能正常通信在nagios服务端(即nagios监控平台)服务器上执行如下指令: /usr/local/nagios/libexec/check_nrpe -H 客户端主机地址
例如:
[iyunv@test1 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.87.135
NRPE v2.14
如果能显示如上的输出信息,表明nrpe可以与客户端正常通信。
(3)定义一个check_nrpe监控命令 修改/usr/local/nagios/etc/commands.cfg文件,添加如下内容: define command {
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ } (4)添加远程主机监控修改/usr/local/nagios/etc/objects/service.cfg,添加如下监控内容: define service{
use local-service host_name test2.test.com
service_description users check_command check_nrpe!check_users_1
}
define service{
use local-service
host_name mysql
service_description load
check_command check_nrpe!check_load_1 } define service{
use local-service host_name mysql service_description disk
check_command check_nrpe!check_sda5_1 }
define servicegroup{ #定义一个服务组 servicegroup_name servergroup #服务组名称,可以随意指定 alias server-group #服务组别名membersweb,PING,web,SSH,web,SSHD,web,http,mysql,users,mysql,load,mysql,disk,mysql,swap#服务组成员,格式为“主机名,主机对应的服务描述” }
(5)测试和启动nagios服务
排错 nrpe Connection refused by host [iyunv@test2 ~]# vim /usr/local/nagios/etc/nrpe.cfg
server_address=0.0.0.0 allowed_hosts=127.0.0.1,192.168.87.129,192.168.87.135 [iyunv@test2 ~]# netstat -antup|grep 5666
tcp 0 0 0.0.0.0:5666 0.0.0.0:* LISTEN 1093/nrpe
|