强曰为道
与天地相似,故不违。知周乎万物,而道济天下,故不过。旁行而不流,乐天知命,故不忧.
文档目录

Nagios 监控运维完整教程 / 第9章:NRPE 远程监控

第9章:NRPE 远程监控

NRPE(Nagios Remote Plugin Executor)是 Nagios 生态中最重要的远程监控组件。本章详细讲解 NRPE 的安装配置、安全管理、命令参数传递、被动检查以及 Windows 平台的 NSClient++ 集成。


一、NRPE 架构

1.1 工作原理

┌─────────────────────┐              ┌─────────────────────┐
│    监控服务器        │              │    被监控主机        │
│                     │              │                     │
│  ┌──────────────┐   │   NRPE 协议   │   ┌──────────────┐  │
│  │ check_nrpe   │───┼──────────────┼──→│  NRPE Daemon │  │
│  │ (客户端)     │   │   (5666端口)  │   │  (服务端)    │  │
│  └──────────────┘   │              │   └──────┬───────┘  │
│                     │              │          │          │
└─────────────────────┘              │   ┌──────▼───────┐  │
                                     │   │  本地插件    │  │
                                     │   │ check_disk   │  │
                                     │   │ check_load   │  │
                                     │   │ check_procs  │  │
                                     │   └──────────────┘  │
                                     │                     │
                                     └─────────────────────┘

1.2 NRPE 通信流程

1. 监控服务器调用 check_nrpe 插件
2. check_nrpe 连接被监控主机的 NRPE Daemon(端口 5666)
3. SSL 握手建立安全连接
4. check_nrpe 发送命令名称和参数
5. NRPE Daemon 查找本地配置中的命令定义
6. NRPE Daemon 执行对应的本地插件
7. 插件返回结果给 NRPE Daemon
8. NRPE Daemon 将结果返回给 check_nrpe
9. check_nrpe 将结果返回给 Nagios Core

1.3 NRPE 版本说明

版本 特性 推荐度
NRPE 2.x 老版本,基础功能 ❌ 不推荐
NRPE 3.x SSL/TLS 支持,命令参数 ✅ 推荐
NRPE 4.x 增强安全,性能改进 ✅ 最新

二、NRPE 安装

2.1 监控服务器端安装

# CentOS/RHEL
yum install -y nrpe nagios-plugins-nrpe

# 或编译安装
cd /tmp
wget https://github.com/NagiosEnterprises/nrpe/releases/download/nrpe-4.1.0/nrpe-4.1.0.tar.gz
tar xzf nrpe-4.1.0.tar.gz
cd nrpe-4.1.0

./configure \
    --with-nrpe-user=nagios \
    --with-nrpe-group=nagios \
    --with-nagios-user=nagios \
    --with-nagios-group=nagios \
    --enable-ssl \
    --enable-command-args

make check_nrpe
sudo cp src/check_nrpe /usr/local/nagios/libexec/
sudo chown nagios:nagios /usr/local/nagios/libexec/check_nrpe
sudo chmod 755 /usr/local/nagios/libexec/check_nrpe

2.2 被监控主机安装

# CentOS/RHEL
yum install -y nrpe nagios-plugins-all

# Ubuntu/Debian
apt-get install -y nagios-nrpe-server nagios-plugins

# 或编译安装
cd /tmp
tar xzf nrpe-4.1.0.tar.gz
cd nrpe-4.1.0

./configure \
    --with-nrpe-user=nagios \
    --with-nrpe-group=nagios \
    --with-nagios-user=nagios \
    --with-nagios-group=nagios \
    --enable-ssl \
    --enable-command-args \
    --libexecdir=/usr/local/nagios/libexec

make all
sudo make install
sudo make install-daemon
sudo make install-config
sudo make install-init

2.3 创建用户(被监控主机)

# 创建 nagios 用户
sudo useradd -r -m -s /bin/bash nagios

# 安装插件依赖
yum install -y fping bind-utils

# 设置插件权限
sudo chown -R nagios:nagios /usr/local/nagios/

三、NRPE 配置

3.1 服务端配置(被监控主机)

# /etc/nagios/nrpe.cfg 或 /usr/local/nagios/etc/nrpe.cfg

########################################
# 基本设置
########################################
server_port=5666
server_address=0.0.0.0  # 监听所有接口(或指定 IP)
nrpe_user=nagios
nrpe_group=nagios
pid_file=/var/run/nrpe/nrpe.pid

########################################
# 日志设置
########################################
log_facility=daemon
log_file=/var/log/nrpe/nrpe.log
debug=0

########################################
# 监控服务器允许列表(关键安全配置!)
########################################
allowed_hosts=127.0.0.1,192.168.1.10
# 逗号分隔的允许连接的 IP 地址

########################################
# SSL 配置
########################################
ssl_version=TLSv1.2+
ssl_cipher_list=ALL:!MD5:!DES:!RC4:!PSK
ssl_cacert_file=/etc/nagios/ssl/ca-cert.pem
ssl_cert_file=/etc/nagios/ssl/server-cert.pem
ssl_key_file=/etc/nagios/ssl/server-key.pem
ssl_client_certs=optional
ssl_logging=0x00

########################################
# 命令参数(重要!)
########################################
dont_blame_nrpe=1
# 0 = 不允许命令参数(更安全)
# 1 = 允许命令参数(更灵活)

########################################
# 超时设置
########################################
command_timeout=60
connection_timeout=300

########################################
# 命令定义
########################################

# 系统负载
command[check_load]=/usr/local/nagios/libexec/check_load -w $ARG1$ -c $ARG2$

# 磁盘使用率
command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$

# 用户数
command[check_users]=/usr/local/nagios/libexec/check_users -w $ARG1$ -c $ARG2$

# 进程数
command[check_procs]=/usr/local/nagios/libexec/check_procs -w $ARG1$ -c $ARG2$ -s $ARG3$

# 内存使用率
command[check_mem]=/usr/local/nagios/libexec/check_mem -w $ARG1$ -c $ARG2$ -f

# Swap 使用率
command[check_swap]=/usr/local/nagios/libexec/check_swap -w $ARG1$ -c $ARG2$

# 日志文件
command[check_log]=/usr/local/nagios/libexec/check_log -F $ARG1$ -O /tmp/log.offset -q $ARG2$

# Zombie 进程
command[check_zombie_procs]=/usr/local/nagios/libexec/check_procs -w $ARG1$ -c $ARG2$ -s Z

# 总进程数
command[check_total_procs]=/usr/local/nagios/libexec/check_procs -w $ARG1$ -c $ARG2$

# 当前用户
command[check_current_users]=/usr/local/nagios/libexec/check_users -w $ARG1$ -c $ARG2$

# 磁盘 I/O
command[check_iostat]=/usr/local/nagios/libexec/check_iostat -d $ARG1$ -w $ARG2$ -c $ARG3$

# 网络接口
command[check_ifoperstatus]=/usr/local/nagios/libexec/check_ifoperstatus -i $ARG1$

# 自定义脚本
command[check_custom]=/usr/local/nagios/libexec/custom/$ARG1$ $ARG2$

# 本地 HTTP 检查
command[check_local_http]=/usr/local/nagios/libexec/check_http -H localhost -p $ARG1$ -u $ARG2$

# MySQL 连接检查
command[check_local_mysql]=/usr/local/nagios/libexec/check_mysql -H localhost -u $ARG1$ -p $ARG2$

# 带固定参数的命令(不允许参数传递时使用)
command[check_load_1_5_15]=/usr/local/nagios/libexec/check_load -w 1,5,15 -c 10,8,6
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_ssh_local]=/usr/local/nagios/libexec/check_ssh localhost

3.2 监控服务器端配置

# 命令定义 /etc/nagios/objects/commands.cfg

define command {
    command_name    check_nrpe
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30
}

# 带参数的 NRPE 命令
define command {
    command_name    check_nrpe_args
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$
}

# 带 SSL 的 NRPE 命令
define command {
    command_name    check_nrpe_ssl
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -a $ARG2$ --ssl=TLSv1.2+ -t 30
}
# 服务定义示例

# 检查远程磁盘
define service {
    use                     generic-service
    host_name               remote-server-01
    service_description     Root Partition
    check_command           check_nrpe_args!check_disk!-w 20% -c 10% -p /
}

# 检查远程负载
define service {
    use                     generic-service
    host_name               remote-server-01
    service_description     CPU Load
    check_command           check_nrpe_args!check_load!5,4,3!10,8,6
}

# 检查远程内存
define service {
    use                     generic-service
    host_name               remote-server-01
    service_description     Memory Usage
    check_command           check_nrpe_args!check_mem!80%!95%
}

# 检查远程进程
define service {
    use                     generic-service
    host_name               remote-server-01
    service_description     Apache Processes
    check_command           check_nrpe_args!check_procs!5!10!httpd
}

# 无参数的固定检查
define service {
    use                     generic-service
    host_name               remote-server-01
    service_description     SSH
    check_command           check_nrpe!check_ssh_local
}

四、NRPE 安全加固

4.1 SSL/TLS 配置

# 生成 CA 证书
openssl req -x509 -nodes -days 3650 -newkey rsa:2048 \
    -keyout /etc/nagios/ssl/ca-key.pem \
    -out /etc/nagios/ssl/ca-cert.pem \
    -subj "/CN=Nagios CA"

# 生成服务器证书签名请求
openssl req -new -nodes \
    -keyout /etc/nagios/ssl/server-key.pem \
    -out /etc/nagios/ssl/server-csr.pem \
    -subj "/CN=nrpe-server"

# 使用 CA 签发服务器证书
openssl x509 -req \
    -in /etc/nagios/ssl/server-csr.pem \
    -CA /etc/nagios/ssl/ca-cert.pem \
    -CAkey /etc/nagios/ssl/ca-key.pem \
    -CAcreateserial \
    -out /etc/nagios/ssl/server-cert.pem \
    -days 3650

# 设置权限
chmod 600 /etc/nagios/ssl/*-key.pem
chmod 644 /etc/nagios/ssl/*-cert.pem
chown -R nagios:nagios /etc/nagios/ssl/

4.2 防火墙配置

# 仅允许监控服务器 IP 访问 NRPE 端口
# CentOS/RHEL firewalld
firewall-cmd --permanent --add-rich-rule='
    rule family="ipv4"
    source address="192.168.1.10"
    port protocol="tcp" port="5666"
    accept'
firewall-cmd --reload

# iptables
iptables -A INPUT -p tcp -s 192.168.1.10 --dport 5666 -j ACCEPT
iptables -A INPUT -p tcp --dport 5666 -j DROP

4.3 xinetd 方式运行(可选)

# /etc/xinetd.d/nrpe
service nrpe
{
    flags           = REUSE
    socket_type     = stream
    port            = 5666
    wait            = no
    user            = nagios
    group           = nagios
    server          = /usr/local/nagios/bin/nrpe
    server_args     = -c /usr/local/nagios/etc/nrpe.cfg --inetd
    log_on_failure  += USERID
    disable         = no
    only_from       = 127.0.0.1 192.168.1.10
}

4.4 安全最佳实践

安全措施 说明 配置方式
IP 白名单 仅允许监控服务器连接 allowed_hosts
SSL 加密 通信加密 ssl_version=TLSv1.2+
禁用参数 不允许动态参数 dont_blame_nrpe=0
非 root 运行 使用低权限用户 nrpe_user=nagios
防火墙 限制端口访问 iptables/firewalld
日志审计 记录所有连接 log_facility=daemon

五、NRPE 命令参数

5.1 参数传递模式

# 模式一:固定参数(安全,推荐)
# 在被监控主机 nrpe.cfg 中定义
command[check_disk_root]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /

# 监控服务器调用(无参数)
check_nrpe -H remote-host -c check_disk_root

# 模式二:动态参数(灵活,需要 dont_blame_nrpe=1)
# 在被监控主机 nrpe.cfg 中定义
command[check_disk]=/usr/local/nagios/libexec/check_disk -w $ARG1$ -c $ARG2$ -p $ARG3$

# 监控服务器调用(带参数)
check_nrpe -H remote-host -c check_disk -a '-w 20% -c 10% -p /'

5.2 参数安全建议

# 方式一:固定参数(最安全,管理复杂)
# 为每个检查定义独立的命令
command[check_disk_root_20_10]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /
command[check_disk_var_15_10]=/usr/local/nagios/libexec/check_disk -w 15% -c 10% -p /var
command[check_disk_home_20_10]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /home

# 方式二:使用参数但限制输入(推荐)
# 使用 bash 脚本包装,验证输入
command[check_disk]=/usr/local/nagios/libexec/check_disk_wrapper.sh $ARG1$

# check_disk_wrapper.sh
#!/bin/bash
VALID_PATHS="/ /var /home /tmp /opt"
if echo "$VALID_PATHS" | grep -qw "$1"; then
    /usr/local/nagios/libexec/check_disk -w 20% -c 10% -p "$1"
else
    echo "UNKNOWN: Invalid path '$1'"
    exit 3
fi

六、被动检查与 NSCA

6.1 被动检查架构

┌─────────────────────┐              ┌─────────────────────┐
│    被监控主机        │              │    监控服务器        │
│                     │              │                     │
│  ┌──────────────┐   │   NSCA 协议   │   ┌──────────────┐  │
│  │ 外部检查脚本 │───┼──────────────┼──→│  NSCA Daemon │  │
│  │ (定时执行)   │   │   (5667端口)  │   │              │  │
│  └──────────────┘   │              │   └──────┬───────┘  │
│                     │              │          │          │
│  ┌──────────────┐   │              │   ┌──────▼───────┐  │
│  │ send_nsca    │   │              │   │ Nagios Core  │  │
│  │ (发送工具)   │   │              │   │ (被动检查)   │  │
│  └──────────────┘   │              │   └──────────────┘  │
└─────────────────────┘              └─────────────────────┘

6.2 NSCA 安装与配置

# 监控服务器安装 NSCA
yum install -y nsca
# 或编译安装
wget https://github.com/NagiosEnterprises/nsca/releases/download/nsca-2.10.0/nsca-2.10.0.tar.gz
tar xzf nsca-2.10.0.tar.gz
cd nsca-2.10.0
./configure
make all
sudo cp src/nsca /usr/local/nagios/bin/
sudo cp sample-config/nsca.cfg /usr/local/nagios/etc/

# NSCA 配置 /usr/local/nagios/etc/nsca.cfg
server_port=5667
server_address=0.0.0.0
password=your_encryption_password
decryption_method=1
debug=0
aggregate_writes=1
# 被监控主机安装 send_nsca
yum install -y send_nsca
# 或编译安装
cd nsca-2.10.0
make send_nsca
sudo cp src/send_nsca /usr/local/nagios/bin/

# send_nsca 配置 /usr/local/nagios/etc/send_nsca.cfg
password=your_encryption_password
encryption_method=1

6.3 提交被动检查

# 格式: 主机名\t服务描述\t状态码\t输出信息
echo -e "remote-server-01\tDisk Check\t0\tOK: Disk usage 45%" | \
    /usr/local/nagios/bin/send_nsca -H nagios-server -c /usr/local/nagios/etc/send_nsca.cfg

# 状态码: 0=OK, 1=WARNING, 2=CRITICAL, 3=UNKNOWN

# 批量提交脚本
#!/bin/bash
NSCA_HOST="nagios-server"
NSCA_CFG="/usr/local/nagios/etc/send_nsca.cfg"
HOSTNAME=$(hostname)

# 检查磁盘
for PART in / /var /home; do
    USAGE=$(df -h $PART | tail -1 | awk '{print $5}' | tr -d '%')
    if [ $USAGE -gt 90 ]; then
        echo -e "${HOSTNAME}\tDisk ${PART}\t2\tCRITICAL: ${USAGE}% used" | send_nsca -H $NSCA_HOST -c $NSCA_CFG
    elif [ $USAGE -gt 80 ]; then
        echo -e "${HOSTNAME}\tDisk ${PART}\t1\tWARNING: ${USAGE}% used" | send_nsca -H $NSCA_HOST -c $NSCA_CFG
    else
        echo -e "${HOSTNAME}\tDisk ${PART}\t0\tOK: ${USAGE}% used" | send_nsca -H $NSCA_HOST -c $NSCA_CFG
    fi
done

七、NSClient++(Windows 监控)

7.1 NSClient++ 简介

NSClient++ 是 Windows 平台上最常用的 Nagios 监控代理,支持 NRPE 和 NSCA 协议。

7.2 安装配置

# 下载 NSClient++
# https://nsclient.org/download/

# 安装(管理员权限运行 PowerShell)
msiexec /i NSCP-0.5.2.41-x64.msi /quiet ADDLOCAL=ALL

# 配置文件 C:\Program Files\NSClient++\nsclient.ini
[/settings/default]
; 允许的主机
allowed hosts = 192.168.1.10

; 启用 NRPE 服务器
[/settings/NRPE/server]
port = 5666
ssl options = tls
ssl = true

; 命令定义
[/settings/external scripts/scripts]
check_cpu = check_cpu warn=80 crit=95 time=5m time=1m time=30s
check_memory = check_memory warn=80 crit=95
check_disk = check_drive "warn=free<20%" "crit=free<10%" drive=C:
check_uptime = check_uptime "warn=uptime<1d" "crit=uptime<1h"
check_service = check_service "filter=name like 'sql%' and state='started'"
check_eventlog = check_eventlog "filter=level='critical' and age<1h" "warn=count>0" "crit=count>5"

; 启动服务
net start nscp

7.3 监控服务器配置

# NRPE 检查 Windows 主机
define command {
    command_name    check_nrpe_win
    command_line    $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$ -t 30
}

define service {
    use                     generic-service
    host_name               windows-server-01
    service_description     CPU Usage
    check_command           check_nrpe_win!check_cpu
}

define service {
    use                     generic-service
    host_name               windows-server-01
    service_description     Memory Usage
    check_command           check_nrpe_win!check_memory
}

define service {
    use                     generic-service
    host_name               windows-server-01
    service_description     C: Drive
    check_command           check_nrpe_win!check_disk
}

八、故障排查

8.1 常见问题

问题 可能原因 解决方案
Connection refused NRPE 未启动 systemctl start nrpe
Connection timeout 防火墙阻断 检查 5666 端口是否开放
SSL handshake failed SSL 配置不匹配 检查 SSL 版本和证书
Return code 255 命令不存在 检查 nrpe.cfg 中的命令定义
Return code 126 插件无执行权限 chmod +x plugin
Return code 127 插件路径错误 检查插件路径

8.2 诊断命令

# 测试 NRPE 连接
/usr/local/nagios/libexec/check_nrpe -H remote-host

# 测试特定命令
/usr/local/nagios/libexec/check_nrpe -H remote-host -c check_disk -a '-w 20% -c 10% -p /'

# 测试 SSL
/usr/local/nagios/libexec/check_nrpe -H remote-host --ssl=TLSv1.2+ -c check_load

# 查看 NRPE 版本
/usr/local/nagios/libexec/check_nrpe -H remote-host -c check_nrpe_version

# 被监控主机调试
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
# 或前台运行查看输出
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg --no-ssl -n

九、注意事项

注意事项 说明
参数安全 生产环境建议 dont_blame_nrpe=0
SSL 必须 始终启用 SSL/TLS 加密
IP 白名单 严格限制 allowed_hosts
插件版本 监控服务器和被监控主机插件版本一致
超时设置 合理设置 command_timeout 避免卡死
定期更新 关注 NRPE 安全公告

十、本章小结

  1. NRPE 是远程执行 Nagios 插件的标准协议
  2. 安全配置至关重要:IP 白名单、SSL 加密、权限控制
  3. 命令参数模式需要在安全和灵活性之间平衡
  4. 被动检查通过 NSCA 实现外部结果提交
  5. NSClient++ 是 Windows 监控的标准代理

下一章第10章:通知升级与依赖 - 学习高级通知策略和依赖管理。