您的位置:首页 > 运维架构 > Linux

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

2017-11-10 22:28 363 查看
作为Nagios的一个扩展功能插件,NRPE可在远程的linux主机上执行的插件程序。远程的liux服务器通过安装NRPE及Nagios的相关插件程序可以向Nagios服务端监控平台提供自身的情况例如:CPU负载,内存使用以及磁盘使用等情况。这里依旧将node1.cn主机作为Nagios监控端,node2.cn作为被监控端。

一、NRPE简介
Nrpe作为Nagios的一个扩展功能,其可以在远程Linux主机上执行插件程序,通过远程服务器自身安装的NRPE插件和Nagios插件程序向Nagios监控平台提供自身负载:CPU负载,内存使用以及磁盘使用等等。
NRPE作为Nagios远程主机监控插件,它被用于让Nagios监控端基于安装的方式触发远端主机检测指令,并发送给Nagios监控端。且执行开销低于SS和的检查方式,并由检测过程不需要远程主机上的系统账号信息,因此安全性方面优于SSH的检测方式。
1.1 NRPE工作原理
NRPE构成:
check_nrpe:位于监控主机

nrpe daemon:位于远程主机(npre daemon需要Nagios-plugins插件的支持,否则daemon在远程主机无效)

Nagios监控某个远程linux主机的服务或资源的过程如下:
第一:Nagios运行check_nrpe插件,使nrpe知道需要做什么检查;

第二:check_nrpe插件会链接到远程的daemon,所用的方式是SSL;

第三:NRPE daemon运行相应Nagios插件执行相关检查;

最后:NRPE daemon将检查结果返回check_nrpe插件,进而check_nrpe将结果递交给Nagios进行处理。
二、远程主机Nagios-plugins插件和NRPE的安装
2.1添加运行用户nagios
[root@node2 src]# useradd -s /sbin/nologin nagios
2.2安装nagios-plugins插件
[root@node2 src]# yum -y install gcc gcc-c++ make openssl openssl-devel
[root@node2 src]#cd /usr/local/src/
[root@node2 src]#tar xvf nagios-plugins-2.2.1.tar.gz
[root@node2 src]#cd nagios-plugins-2.2.1
[root@node2 nagios-plugins-2.2.1]#  ./configure --with-nagios-user=nagios --with-nagios-group=nagios
[root@node2 nagios-plugins-2.2.1]# make && make install
2.3安装nrpe
[root@node2 src]# tar xvf nrpe-2.15.tar.gz
[root@node2 src]# cd nrpe-2.15
[root@node2 nrpe-2.15]# ./configure --with-nrpe-user=nagios --with-nrpe-group=nagios --with-nagios-user=nagios --enable-command-args --enable-ssl

*** Configuration summary for nrpe 2.15 09-06-2013 ***:
General Options:
-------------------------
NRPE port:    5666
NRPE user:    nagios
NRPE group:   nagios
Nagios user:  nagios
Nagios group: nagios
Review the options above for accuracy.  If they look okay,
type 'make all' to compile the NRPE daemon and client.
[root@node2 nrpe-2.15]# make all
[root@node2 nrpe-2.15]# make install-plugin
cd ./src/ && make install-plugin
[root@node2 nrpe-2.15]# make install-daemon
cd ./src/ && make install-daemon
[root@node2 nrpe-2.15]# make install-daemon-config
2.4 配置NRPE
[root@node2 ~]# grep -E -v "^#|^$" /usr/local/nagios/etc/nrpe.cfg
log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,192.168.31.101            #允许的地址,在127.0.0.1后添加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_hda1]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/hda1
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


2.5 NRPE守护进程启动
[root@node2 ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@node2 ~]# netstat -tulpn | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      26018/nrpe
tcp        0      0 :::5666                     :::*                        LISTEN      26018/nrpe
NRPE的运行模式:
-i          =    Run as a service under inetd or xinetd
-d          =    Run as a standalone daemon
-d -s       =    Run as a subsystem under AIX
2.6 nrpe启动脚本的编辑

[root@node2 ~]# vim /etc/init.d/nrped
#!/bin/bash
#date: 2017-11-05
# chkconfig: - 64 36
# description:  NRPE server.
NPRE=/usr/local/nagios/bin/nrpe
NRPECFG=/usr/local/nagios/etc/nrpe.cfg
case "$1" in
start)
echo -n "Starting NPRE daemon..."
${NPRE} -c ${NRPECFG} -d
echo "Start NPRE daemon done..."
;;
stop)
echo -n "stopping NRPE daemon..."
pkill -u nagios nrpe
echo "Stop NPRE daemon done..."
;;
restart)
$0 stop
sleep 3
$0 start
;;
*)
echo "Usage:$0 start|stop|restart"
;;
esac
exit 0
2.7 nrped脚本测试和设置nrped服务开机启动启动
[root@node2 ~]# chkconfig nrped on
[root@node2 ~]# service nrped restart
stopping NRPE daemon...Stop NPRE daemon done...
Starting NPRE daemon...Start NPRE daemon done...
[root@node2 ~]# netstat -tnlp | grep nrpe
tcp        0      0 0.0.0.0:5666                0.0.0.0:*                   LISTEN      26085/nrpe
tcp        0      0 :::5666                     :::*                        LISTEN      26085/nrpe
3 Nagios服务端安装NRPE
[root@node1 src]# tar xvf nrpe-2.15.tar.gz
[root@node1 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@node1 nrpe-2.15]# make all
[root@node1 nrpe-2.15]# make install-plugin
[root@node1 nrpe-2.15]# ls /usr/local/nagios/libexec/check_nrpe
/usr/local/nagios/libexec/check_nrpe
3.1 关于check_nrpe的一些用法
[root@node1 libexec]# /usr/local/nagios/libexec/check_nrpe -h
NRPE Plugin for Nagios
Copyright (c) 1999-2008 Ethan Galstad (nagios@nagios.org)
Version: 2.15
Last Modified: 09-06-2013
License: GPL v2 with exemptions (-l for more info)
SSL/TLS Available: Anonymous DH Mode, OpenSSL 0.9.6 or higher required
Usage: check_nrpe -H <host> [ -b <bindaddr> ] [-4] [-6] [-n] [-u] [-p <port>] [-t <timeout>] [-c <command>] [-a <arglist...>]
Options:
-n         = Do no use SSL
-u         = Make socket timeouts return an UNKNOWN state instead of CRITICAL
<host>     = The address of the host running the NRPE daemon
<bindaddr> = bind to local address
-4         = user ipv4 only
-6         = user ipv6 only
[port]     = The port on which the daemon is running (default=5666)
[timeout]  = Number of seconds before connection times out (default=10)
[command]  = The name of the command that the remote daemon should run
[arglist]  = Optional arguments that should be passed to the command.  Multiple
arguments should be separated by a space.  If provided, this must be
the last option supplied on the command line.
Note:
This plugin requires that you have the NRPE daemon running on the remote host.
You must also have configured the daemon to associate a specific plugin command
with the [command] option you are specifying here.  Upon receipt of the
[command] argument, the NRPE daemon will run the appropriate plugin command and
send the plugin output and return code back to *this* plugin.  This allows you
to execute plugins on remote hosts and 'fake' the results to make Nagios think
the plugin is being run locally.
相关语法格式:

[root@node1 libexec]# ./check_nrpe -H <host> [-n] [-u] [-p <port>] [-t <timeout>] [-a <arglist...>]
[root@node1 libexec]# ./check_nrpe -H 192.168.31.102
NRPE v2.15
3.2 定义相关命令
[root@node1 libexec]# cd /usr/local/nagios/etc/objects/
添加内容到commands.cfg末尾

[root@node1 objects]# vim commands.cfg
define command{
command_name check_nrpe
command_line $USER1$/check_npre -H "$HOSTADDRESS$" -c "$ARG1$"
}
3.3 添加nagios.cfg配置文件内容
[root@node1 objects]# vim /usr/local/nagios/etc/nagios.cfg
cfg_file=/usr/local/nagios/etc/objects/linehost.cfg
3.4 定义主机linehost.cfg和服务service.cfg
[root@node1 objects]# grep -E -v "^#|^$" linehost.cfg
define host{
use                     linux-server            ; Name of host template to use
; This host definition will inherit all variables that are defined
; in (or inherited by) the linux-server host template definition.
host_name               linehost
alias                   linehost
address                 192.168.31.102
}
define hostgroup{
hostgroup_name  my-linux-servers ; The name of the hostgroup
alias           My Linux Servers ; Long name of the group
members         linehost     ; Comma separated list of hosts that belong to this group
}
[root@node1 objects]# vim services.cfg
define service{
use                     local-service
host_name               linehost
service_groups          MysqlGroup
service_description     MySqlSev
check_command           check_mysql
}
define service{
use                             local-service         ; Name of service template to use
host_name                       linehost
service_description             PING
check_commandcheck_ping!100.0,20%!500.0,60%
}
define service{
use                             local-service         ; Name of service template to use
host_name                       linehost
service_description             Root Partition
check_commandcheck_local_disk!20%!10%!/
}
define service{
use                             local-service         ; Name of service template to use
host_name                       linehost
service_description             Current Users
check_commandcheck_local_users!20!50
}
define service{
use                             local-service         ; Name of service template to use
host_name                       linehost
service_description             Total Processes
check_commandcheck_local_procs!250!400!RSZDT
}
检查配置文件
[root@node1 objects]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
无错误信息重启服务
[root@node1 objects]# service nagios restart
web端进行查看验证

Nagios通过NRPE对远程linux服务器监控就介绍到这里,下一章将介绍Nagios对windowns主机的监控以及的邮件报警功能的配置

博文出自:http://os.51cto.com/art/201409/452605.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  nagios 远程监控 NRPE