1. 程式人生 > >Nagios基於NRPE 監控遠程Linux主機

Nagios基於NRPE 監控遠程Linux主機

sbin ces 防火 apt-get 遠程主機 tin ini 啟動 serve

1 安裝環境:

監控段IP: 192.168.4.34 主機名:nagios.com 操作系統:CentOS release 6.8 (Final)
軟件:nagios-4.2.0,nagios-plugins-2.1.2,nrpe-2.15

被監控端IP:192.168.4.111 主機名:client.com操作系統:CentOS release 6.8 (Final)
軟件:nagios-plugins-2.1.2,nrpe-2.15

2 NRPE簡介

2.1 NRPE 功能介紹

NRPE是Nagios的一個功能擴展,它可在遠程Linux/Unix主機上執行插件程序。通過在遠程服務器上安裝NRPE插件及Nagios插件程序來向Nagios監控平臺提供該服務器的本地情況,如CPU負載,內存使用,磁盤使用等。

2.2 NRPE 架構

Nagios監控端稱為Nagios服務器端,而將遠程被監控的主機稱為Nagios客戶端。

2.3 NRPE 工作原理

NRPE進程,運行於遠程主機(Linux/UNIX),也就是被監控端。 當nagios需要監控遠程主機(Linux/UNIX)的服務時,NRPE具體的工作流程如下:
Nagios會執行check_nrpe插件,並告訴它需要監控的服務項;
check_nrpe插件通過SSL方式與被監控端的nrpe進程連接;
nrpe進程運行對應的nagios插件來執行服務或資源的監測;
NRPE 進程將監測的結果返回給check_nrpe 插件,check_nrpe插件又將結果傳遞給nagios進程做後續處理。

註意:NRPE進程能夠進行服務與資源監控的前提是:遠程主機(Linux/UNIX)必須裝有nagios插件。

3 NRPE安裝與配置

3.1 遠程主機(被監控主機)安裝

3.1.1添加nagios用戶

[root@client ~]# useradd -s /sbin/nologin nagios
[root@client ~]# id nagios
uid=500(nagios) gid=500(nagios) groups=500(nagios)

3.1.2NRPE 依賴於nagios-plugins,因此,要先安裝。

[root@client ~]# tar xf nagios-plugins-2.1.2.tar.gz

[root@client ~]# cd nagios-plugins-2.1.2
[root@client nagios-plugins-2.1.2]# ./configure
--with-nagios-user=nagios --with-nagios-group=nagios

註意:要監控MySQL需要添加 –with-mysql
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
--enable-extra-opts: yes
--with-perl: /usr/bin/perl
--enable-perl-modules: no
--with-cgiurl: /nagios/cgi-bin
--with-trusted-path: /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
--enable-libtap: no

3.1.3安裝NRPE

[root@client ~]# tar xf nrpe-2.15.tar.gz
[root@client ~]# cd nrpe-2.15
[root@client nrpe-2.15]# ./configure
--with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl
[root@client nrpe-2.15]# make all
[root@client nrpe-2.15]# make install-plugin
[root@client nrpe-2.15]# make install-daemon
[root@client nrpe-2.15]# make install-daemon-config
如果需要打開5666端口,則需要下列命令(本案例默認關閉的防火墻)

3.1.4配置NRPE

 [root@kk nrpe-3.0.1]#vim /usr/local/nagios/etc/nrpe.cfg 

修改allowed_hosts=192.168.4.34,允許Nagios服務器端訪問;

在命令行測試如下的監測命令,這裏根據自己的監測需求對命令進行修改,並寫入nrpe.cfg文件:
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_users
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_load
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_sda1
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_total_procs
/usr/local/nagios/libexec/check_nrpe -H localhost -c check_zombie_procs

3.1.5 查看配置結果

[root@client ~]# grep -v ‘^#‘ /usr/local/nagios/etc/nrpe.cfg |sed ‘/^$/d‘
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=192.168.4.34 #允許Nagios服務器端訪問

dont_blame_nrpe=0
allow_bash_command_substitution=0
debug=0
command_timeout=60
connection_timeout=300
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_sda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda1
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

3.1.6啟動NRPE

root@client ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

1、 查看啟動服務
2、 [root@client ~]# netstat -tnlp | grep 5666
3、 tcp 0 0 0.0.0.0:5666 0.0.0.0: LISTEN 32301/nrpe
tcp 0 0 :::5666 :::
LISTEN 32301/nrpe
有兩種方式用於管理nrpe服務,nrpe有兩種運行模式:
-i # Run as a service under inetd or xinetd
-d # Run as a standalone daemon 可以為nrpe編寫啟動腳本,使得nrpe以standard alone方式運行
4、 配置NRPE啟動服務腳本
[root@client ~]# vim /etc/init.d/nrped

#!/bin/bash   
# chkconfig: 2345 88 12   
# description: NRPE DAEMON   

NRPE=/usr/local/nagios/bin/nrpe  
NRPECONF=/usr/local/nagios/etc/nrpe.cfg   

case "$1" in 
    start)   
        echo -n "Starting NRPE daemon..." 
        $NRPE -c $NRPECONF -d   
        echo " done." 
        ;;   
    stop)   
        echo -n "Stopping NRPE daemon..." 
        pkill -u nagios nrpe   
        echo " done." 
    ;;   
    restart)   
        $0 stop   
        sleep 2   
        $0 start   
        ;;   
    *)   
        echo "Usage: $0 start|stop|restart" 
        ;;   
    esac  
exit 0   

[root@client ~]# vim /etc/init.d/nrped
[root@client ~]# chmod +x /etc/init.d/nrped
[root@client ~]# chkconfig --add nrped
[root@client ~]# chkconfig nrped on
[root@client ~]# service nrped restart
Stopping NRPE daemon... done.
Starting NRPE daemon... done.

3.2 監控的NRPE 安裝與配置

3.2.1 安裝依賴包

[root@client ~]#yum -y install openssl openssl-devel

3.2.2 NRPE安裝

[root@nagios ~]# tar xf nrpe-2.15.tar.gz
[root@nagios ~]# cd nrpe-2.15
[root@nagios nrpe-2.15]# ./configure --with-nrpe-user=nagios --with-nagios-group=nagios \

--with-nagios-user=nagios --with-nagios-group=nagios --enable-command-args --enable-ssl

[root@nagios nrpe-2.15]# make all
[root@nagios nrpe-2.15]# make install-plugin

安裝完成後,會在Nagios安裝目錄的libexec下生成check_nrpe的插件,如下所示:
[root@nagios ~]# ls -l /usr/local/nagios/libexec/check_nrpe
-rwxrwxr-x. 1 nagios nagios 76777 Nov 21 19:06 /usr/local/nagios/libexec/check_nrpe

3.2.3 NRPE 測試

[root@nagios ~]# cd /usr/local/nagios/libexec/
[root@nagios libexec]# ./check_nrpe -H 192.168.4.111
NRPE v2.15

3.2.4定義命令

[root@nagios nagios]# cd etc/objects/
[root@nagios objects]# vim commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H "$HOSTADDRESS$" -c "$ARG1$"
}

3.2.5 定義主機和服務

[root@nagios objects]# more linuxserver.cfg
define host {
use linux-server
host_name linuxhost
alias My Linux Host
address 192.168.4.111
}

define host {
use linux-server
host_name linuxhost
alias My Linux Host
address 192.168.4.111
}

define service{
use generic-service
host_name linuxhost
service_description CHECK USERS
check_command check_nrpe!check_users
}

define service{
use generic-service
host_name linuxhost
service_description CHECK Load
check_command check_nrpe!check_load
}

define service{
use generic-service
host_name linuxhost
service_description CHECK sda1
check_command check_nrpe!check_sda1
}

define service{
use generic-service
host_name linuxhost
service_description CHECK Zombie
check_command check_nrpe!check_procs
}

define service{
use generic-service
host_name linuxhost
service_description CHECK procs
check_command check_nrpe!check_total_procs
}

3.2.6 啟動所定義的命令和服務

[root@nagios etc]# vim nagios.cfg
#增加一行
cfg_file=/usr/local/nagios/etc/objects/linuxserver.cfg

3.2.7 檢查配置文件語法

[root@nagios etc]# service nagios configtest

3.2.8重新啟動nagios服務

[root@nagios etc]# service nagios restart

3.2.9登錄Nagios web監控頁面查看配置的監控是否生效

技術分享圖片

Nagios基於NRPE 監控遠程Linux主機