神甫 发表于 2015-9-8 08:21:02

在nagios中使用nrpe自定义脚本

  nrpe的安装
    tar xvfz nrpe-2.13.tar.gz
    cd nrpe-2.13
    ./configure
    make all
    make install-plugin
    make install-daemon
    make install-daemon-config
    yum install xinetd
    make install-xinetd

# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.5.10

# vim /etc/xinetd.d/nrpe
# 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.5.10   # 这个IP可以不变,为本机
}

# /usr/local/nagios/libexec/check_nrpe -H localhost
NRPE v2.12
# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.10    #192.168.5.10为nagios服务器的ip
NRPE v2.12
# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.110   #192.168.5.110还未开机
Connection refused or timed out
# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.110   #192.168.5.110开机
CHECK_NRPE: Error - Could not complete SSL handshake.

# service xinetd restart#重启nrpe服务
=========================================================================================================================
在192.168.5.110被监控端安装nrpe
1.安装nrpe依赖包
yum -y install gcc glibc glibc-common openssl openssl-devel
2.安装nagios-plugin
useradd nagios
wget http://sourceforge.net/projects/nagiosplug/files/nagiosplug/1.4.15/nagios-plugins-1.4.15.tar.gz/download
tar zxf nagios-plugins-1.4.15.tar.gz && cd nagios-plugins-1.4.15
./configure --with-nagios-user=nagios --with-nagios-group=nagios
make
make install
chown -R nagios.nagios /usr/local/nagios
3.安装nrpe
wget http://nchc.dl.sourceforge.net/project/nagios/nrpe-2.x/nrpe-2.12/nrpe-2.12.tar.gz
tar zxf nrpe-2.12.tar.gz && cd nrpe-2.12
./configure
make all
make install-plugin
make install-daemon
make install-daemon-config
4.启动nrpe并设置开机自启动
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.d/rc.local


# vim /usr/local/nagios/etc/nrpe.cfg
allowed_hosts=127.0.0.1,192.168.5.10   #将nagios的服务端IP加上

# vim /etc/xinetd.d/nrpe
# 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.5.10   #将nagios的服务端IP加上

}
==============在nagios服务端测试 ====================
#
NRPE v2.12

=================================================================

要在被监控端192.168.5.110加入自定义的脚本
# vim /usr/local/nagios/libexec/nh_check_users
#!/bin/bash
# for nrpe check user
U=`who | wc -l`
if [ $U -le 3 ];then
echo "OK,current user is.$U"
exit 0
elif [ $U -gt 6 ];then
echo "CRITICAL,current user is.$U"
exit 2
else
echo "WARNING,current user is.$U"
exit 1
fi

  定义该脚本为nagios用户和nagios组
  # chown nagios.nagios /usr/local/nagios/libexec/nh_check_91

  在NRPE上加入该自定义命令
  # vim /usr/local/nagios/etc/nrpe.cfg
  command=/usr/local/nagios/libexec/nh_check_users
在服务端测试192.168.5.10,直接使用命令行
# /usr/local/nagios/libexec/check_nrpe -H 192.168.5.110 -c nh_check_users
OK,current user is.1

=========================================================================
在服务端测试192.168.5.10的nagios中加入自定义脚本
# cd /usr/local/nagios/etc/objects
# vim hosts_192.168.5.110.cfg
define host{
      use                     linux-server
      host_name               192.168.5.110
      alias                   192.168.5.110
      address               192.168.5.110
      }
define hostgroup{
      hostgroup_name          nh_linuxs
      alias                   nh_linuxs
      members               192.168.5.110
      }
define service{
      use                     local-service
      host_name               192.168.5.110
      service_description   check-host-alive
      check_command         check-host-alive
      max_check_attempts      5
      normal_check_interval   3
      retry_check_interval    2
      check_period            24x7
      notification_interval   10
      notification_period   24x7
   }
define service{
      use                     local-service
      host_name               192.168.5.110
      service_description   SSH
      check_command         check_ssh
      max_check_attempts      5
      normal_check_interval   3
      retry_check_interval    2
      check_period            24x7
      notification_interval   10
      notification_period   24x7
      }
define service{
      use                     local-service
      host_name               192.168.5.110
      service_description   check_nrpe_check_users
      check_command         check_nrpe!nh_check_users
      max_check_attempts      5
      normal_check_interval   3
      retry_check_interval    2
      check_period            24x7
      notification_interval   10
      notification_period   24x7
      }
过一下,在nagios中就能观察到check_nrpe_check_users自定义服务的状态了,"OK,current user is.1".
# service nagios reload

Host Sort by host name (ascending)Sort by host name (descending)    Service Sort by service name (ascending)Sort by service name (descending)   
Status Sort by service status (ascending)Sort by service status (descending)    Last Check Sort by last check time (ascending)Sort by last check time
(descending)    Duration Sort by state duration (ascending)Sort by state duration time (descending)    Attempt Sort by current attempt (ascending)Sort by
current attempt (descending)    Status Information
192.168.5.110
   
   
SSH
   
    OK   04-04-2014 20:22:01   0d 0h 1m 56s   1/5   SSH OK - OpenSSH_5.3 (protocol 2.0)
   
check-host-alive
   
    OK   04-04-2014 20:22:50   0d 0h 2m 56s   1/5   PING OK - Packet loss = 0%, RTA = 0.61 ms
   
check_nrpe_check_users
   
    OK   04-04-2014 20:23:38   0d 0h 0m 19s   1/5   OK,current user is.1
页: [1]
查看完整版本: 在nagios中使用nrpe自定义脚本