NRPEを使った監視を実装する

今回はNRPEを使ったリモート監視の実装方法のメモ。

NRPEとは


NRPE(Nagios Remote Plugin Executer)とは、Nagiosからリモートホストを監視するためのプラグインです。
リソース監視など監視対象ホストで任意のコマンドを実行する必要がある時に使います。

インストール作業(監視対象ホスト)


まずは監視対象ホストから。


ユーザーを作成します。必要なければログインシェルは剥奪してしまっても問題ないです。

# useradd -m nagios
# passwd nagios


nagios-plugins」をインストールします。

# cd /usr/local/src
#  wget http://prdownloads.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.15.tar.gz
#  tar -xzf nagios-plugins-1.4.15.tar.gz 
#  cd nagios-plugins-1.4.15
#  ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-ssl
#  make all
#  make install


nrpeをインストールします。

# cd /usr/local/src/
# wget http://prdownloads.sourceforge.net/sourceforge/nagios/nrpe-2.13.tar.gz
# tar -xzf nrpe-2.13.tar.gz 
# cd nrpe-2.13
# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --enable-ssl --enable-command-args
# make all
# make install
# cp -p /usr/local/src/nrpe-2.13/sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
# cp /usr/local/src/nrpe-2.13/init-script /etc/init.d/nrpe
# chmod 755 /etc/init.d/nrpe 
# chkconfig --add nrpe


nrpe.cfgを設定します。以下を修正していきます。


監視サーバーからアクセスを許可する為に、「allowed_hosts」にIPアドレスを追加します。

allowed_hosts=127.0.0.1,xxx.xxx.xxx.xxx


nrpeが引数を受け付けるように、「dont_blame_nrpe」を有効にします。

dont_blame_nrpe=1


次にコマンドを定義します。今回はプロセスとリソース監視を定義してみます。

command[check_users]=/usr/local/nagios/libexec/check_users -w $ARG1$ -c $ARG2$
command[check_load]=/usr/local/nagios/libexec/check_load -w $ARG1$ -c $ARG2$
command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$
command[check_procs]=/usr/local/nagios/libexec/check_procs -w $ARG1$ -c $ARG2$ -u $ARG3$ -C $ARG4$
command[check_cpu]=/usr/local/nagios/libexec/check_cpu.sh -w $ARG1$ -c $ARG2$
command[check_mem]=/usr/local/nagios/libexec/check_mem.pl -u -C -w $ARG1$ -c $ARG2$
command[check_swap]=/usr/local/nagios/libexec/check_swap -w $ARG1$ -c $ARG2$


終わった所でnrpeを再起動します。

# /etc/init.d/nrpe restart
Shutting down nrpe: [  OK  ]
Starting nrpe: [  OK  ]
#

クライアント側の動作確認


ローカルホスト上で、「check_nrpe」経由でプラグインを実行してみます。
意図した通りに応答すれば、コマンド定義含めて問題無しと判断します。

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_users -a 5 10
USERS OK - 1 users currently logged in |users=1;5;10;0

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_load -a 5,5,5 10,10,10
OK - load average: 0.03, 0.01, 0.04|load1=0.030;5.000;10.000;0; load5=0.010;5.000;10.000;0; load15=0.040;5.000;10.000;0; 

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_procs -a 1:1 1:1 root auditd
PROCS OK: 1 process with UID = 0 (root), command name 'auditd'

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_cpu -a 80 90
OK - user: 8.83, nice: 0.50, sys: 16.40, iowait: 0.50, irq: 0.50, softirq: 0.50 idle: 76.25 | 'user'=8.83 'nice'=0.50 'sys'=16.40 'softirq'=0.50 'iowait'=0.50 'irq'=0.50 'idle'=76.25

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_mem -a 80 90
OK - 16.0% (80268 kB) used.|TOTAL=502720KB;;;; USED=80268KB;;;; FREE=422452KB;;;; CACHES=46952KB;;;;

# /usr/local/nagios/libexec/check_nrpe -H 127.0.0.1 -c check_swap -a 20% 10%
SWAP OK - 100% free (991 MB out of 991 MB) |swap=991MB;198;99;0;991

サーバー側の設定


サーバー側でも「check_nrpe」を使うので、クライアント側同様にnrpeをインストールします。


コマンドを「command.cfg」に定義します。

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


「service.cfg」に監視設定を定義します。

define service {
    use                 generic-service
    host_name           myhost
    service_description USER COUNT
    check_command       check_nrpe_arg!check_users!5 10
}

define service {
    use                 generic-service
    host_name           myhost
    service_description CPU USAGE
    check_command       check_nrpe_arg!check_cpu!80 90
}

define service {
    use                 generic-service
    host_name           myhost
    service_description LOADAVERAGE
    check_command       check_nrpe_arg!check_load!5,5,5 10,10,10
}

define service {
    use                 generic-service
    host_name           myhost
    service_description MEMORY USAGE
    check_command       check_nrpe_arg!check_mem!80 90
}

define service {
    use                 generic-service
    host_name           myhost
    service_description SWAP
    check_command       check_nrpe_arg!check_swap!20% 10%
}

define service {
    use                 generic-service
    host_name           myhost
    service_description DISK: /
    check_command       check_nrpe_arg!check_disk!80 90 /
}

define service {
    use                 generic-service
    host_name           myhost
    service_description DISK: /boot
    check_command       check_nrpe_arg!check_disk!80 90 /boot
}

define service {
    use                 generic-service
    host_name           myhost
    service_description PROCS: crond
    check_command       check_nrpe_arg!check_procs!1:1 1:1 root crond
}

define service {
    use                 generic-service
    host_name           myhost
    service_description PROCS: rsyslogd
    check_command       check_nrpe_arg!check_procs!1:1 1:1 root rsyslogd
}

define service {
    use                 generic-service
    host_name           myhost
    service_description PROCS: sshd
    check_command       check_nrpe_arg!check_procs!1: 1: root sshd
}


今日はこんなところで。