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

简介:

作为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

1
  [root@node2 src] # useradd -s /sbin/nologin nagios

  2.2安装nagios-plugins插件

1
2
3
4
5
6
  [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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  [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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[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守护进程启动

1
2
3
4
[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的运行模式:

1
2
3
    -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启动脚本的编辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  [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服务开机启动启动

1
2
3
4
5
6
7
[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

1
2
3
4
5
6
[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的一些用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
[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.

相关语法格式:

1
2
3
[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配置文件内容

1
2
[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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
     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


本文转自 CARYFLASH 51CTO博客,原文链接:http://blog.51cto.com/maoxiaoxiong/1980787


目录
打赏
0
0
0
0
344
分享
相关文章
Prometheus+Grafana监控Linux主机
通过本文的步骤,我们成功地在 Linux 主机上使用 Prometheus 和 Grafana 进行了监控配置。具体包括安装 Prometheus 和 Node Exporter,配置 Grafana 数据源,并导入预设的仪表盘来展示监控数据。通过这种方式,可以轻松实现对 Linux 主机的系统指标监控,帮助及时发现和处理潜在问题。
301 7
Prometheus+Grafana+NodeExporter:构建出色的Linux监控解决方案,让你的运维更轻松
本文介绍如何使用 Prometheus + Grafana + Node Exporter 搭建 Linux 主机监控系统。Prometheus 负责收集和存储指标数据,Grafana 用于可视化展示,Node Exporter 则采集主机的性能数据。通过 Docker 容器化部署,简化安装配置过程。完成安装后,配置 Prometheus 抓取节点数据,并在 Grafana 中添加数据源及导入仪表盘模板,实现对 Linux 主机的全面监控。整个过程简单易行,帮助运维人员轻松掌握系统状态。
441 3
在Linux中,zabbix如何监控脑裂?
在Linux中,zabbix如何监控脑裂?
在Linux中,如何查看当前主机的主机名,如何修改主机名?要想重启后依旧生效,需要修改哪个配置文件?
在Linux中,如何查看当前主机的主机名,如何修改主机名?要想重启后依旧生效,需要修改哪个配置文件?
Linux虚拟机与主机和Xshell的连接问题解决
Linux虚拟机与主机和Xshell的连接问题解决
220 1
使用NRPE和Nagios监控Linux系统资源的方法
通过遵循以上步骤,可以有效地使用NRPE和Nagios监控Linux系统资源,确保系统运行稳定,并及时响应任何潜在的问题。这种方法提供了高度的可定制性和灵活性,适用于从小型环境到大型分布式系统的各种监控需求。
118 2
监控Linux服务器
详细介绍了如何监控Linux服务器,包括监控CPU、内存、磁盘存储和带宽的使用情况,以及使用各种系统监控工具如vmstat、iostat、sar、top和dstat来分析系统性能,并推荐了一些开源监控系统。
98 0
监控Linux服务器
Linux修改主机名的两种方法
Linux修改主机名的两种方法
120 0
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。
在Linux中,如何将本地80端口的请求转发到8080端口,当前主机IP为192.168.16.1,其中本地网卡eth0。