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

Nagios基於NRPE 監控遠端Linux主機

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使用者

[[email protected] ~]# useradd -s /sbin/nologin nagios
[[email protected] ~]# id nagios
uid=500(nagios) gid=500(nagios) groups=500(nagios)

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

[[email protected] ~]# tar xf nagios-plugins-2.1.2.tar.gz
[[email protected]

~]# cd nagios-plugins-2.1.2
[[email protected] 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

[[email protected] ~]# tar xf nrpe-2.15.tar.gz
[[email protected] ~]# cd nrpe-2.15
[[email protected] 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
[[email protected] nrpe-2.15]# make all
[[email protected] nrpe-2.15]# make install-plugin
[[email protected] nrpe-2.15]# make install-daemon
[[email protected] nrpe-2.15]# make install-daemon-config
如果需要開啟5666埠,則需要下列命令(本案例預設關閉的防火牆)

3.1.4配置NRPE

 [[email protected] 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 檢視配置結果

[[email protected] ~]# 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

[email protected] ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg –d

1、 檢視啟動服務
2、 [[email protected] ~]# 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啟動服務指令碼
[[email protected] ~]# 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   

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

3.2 監控的NRPE 安裝與配置

3.2.1 安裝依賴包

[[email protected] ~]#yum -y install openssl openssl-devel

3.2.2 NRPE安裝

[[email protected] ~]# tar xf nrpe-2.15.tar.gz
[[email protected] ~]# cd nrpe-2.15
[[email protected] 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

[[email protected] nrpe-2.15]# make all
[[email protected] nrpe-2.15]# make install-plugin

安裝完成後,會在Nagios安裝目錄的libexec下生成check_nrpe的外掛,如下所示:
[[email protected] ~]# 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 測試

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

3.2.4定義命令

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

3.2.5 定義主機和服務

[[email protected] 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 啟動所定義的命令和服務

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

3.2.7 檢查配置檔案語法

[[email protected] etc]# service nagios configtest

3.2.8重新啟動nagios服務

[[email protected] etc]# service nagios restart

3.2.9登入Nagios web監控頁面檢視配置的監控是否生效

Nagios基於NRPE 監控遠端Linux主機