Nagios基于NRPE 监控远程Linux主机

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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主机的主要内容,如果未能解决你的问题,请参考以下文章

监控之--使用NPRE监控Linux主机

Nagios监控

Nagios

Nagios4.3.1监控Centos6.7

监控之--Nagios监控远程windows主机及邮件告警功能的配置

搭建基于Nagios的监控系统——之监控远程Linux服务器