本文詳細記錄使用 NRPE(Nagios Remote Plugin Executor)實現 Nagios 遠程監控 MySQL 服務器的完整部署流程,涵蓋被監控端與監控端雙向配置、插件編譯、SSL 連線、服務註冊與圖形化驗證。
check_nrpe 插件,發起檢查請求nrpe daemon,接收請求並調用本地 Nagios Plugins 執行檢查useradd -s /sbin/nologin nagios
yum -y install gcc gcc-c++ make openssl openssl-devel
# 下載並編譯安裝 nagios-plugins(略,假設已安裝至 /usr/local/nagios/libexec/)
tar -xvf nrpe-2.15.tar.gz
cd 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
make all
make install-plugin
make install-daemon
make install-daemon-config
grep -v '^#' /usr/local/nagios/etc/nrpe.cfg | sed '/^$/d'
核心配置項:
| 參數 | 值 | 說明 |
|---|---|---|
| server_port | 5666 | NRPE 監聽埠 |
| allowed_hosts | 127.0.0.1,192.168.121.211 | 允許連接的監控端 IP(必須含監控端) |
| dont_blame_nrpe | 0 | 禁止命令參數傳遞(安全考量) |
| command_timeout | 60 | 命令執行超時秒數 |
command[check_mysql_status]=/usr/local/nagios/libexec/check_mysql \
-u nagios -P 3306 -S /usr/local/mysql/mysql.sock \
-H localhost --password='nagiosq@0625' -d test -w 60 -c 100
cat > /etc/init.d/nrped << 'EOF'
#!/bin/bash
# chkconfig: 2345 80 90
# description: auto_run
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
EOF
chmod +x /etc/init.d/nrped
chkconfig --add nrped
chkconfig nrped on
service nrped start
/usr/local/nagios/libexec/check_mysql -u nagios -P 3306 \
-S /usr/local/mysql/mysql.sock -H localhost \
--password='nagiosq@0625' -d test -w 60 -c 100
輸出示例:Uptime: 1823238 Threads: 6 Questions: 684495 ... | Connections=3116c;;; ...
find / -name libmysqlclient.so.18
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18
GRANT PROCESS, SUPER, REPLICATION CLIENT ON *.*
TO 'nagios'@'localhost' IDENTIFIED BY 'nagiosq@0625';
FLUSH PRIVILEGES;
tar xf nrpe-2.15.tar.gz
cd 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
make all
make install-plugin
生成 /usr/local/nagios/libexec/check_nrpe
./check_nrpe -H 192.168.121.210
# 若報錯 "Could not complete SSL handshake",檢查被監控端 allowed_hosts 是否包含監控端 IP
# check_nrpe 通用命令
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
# 主機存活檢查
define command{
command_name check_host_alive
command_line $USER1$/check_ping -H $HOSTADDRESS$ -w 3000.0,80% -c 5000.0,100% -p 5
}
define service{
host_name cactitest
service_description CheckMysql Status
check_command check_nrpe!check_mysql_status
max_check_attempts 5
normal_check_interval 3
retry_check_interval 2
check_period 24x7
notification_interval 10
notification_period 24x7
notification_options w,u,c,r
contact_groups dba
}
同樣定義:check_load, check_sda1, check_total_procs, check_users, check_zombie_procs, check_host_alive
cfg_file=/usr/local/nagios/etc/servers.cfg
# 重啟 Nagios 生效
service nagios restart
此架構可輕鬆擴展至監控數十台 MySQL 實例,只需在各被監控端重複部署 NRPE 並調整 commands 定義即可。