首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

Nagios 監控 MySQL 服務器完整實作指南(NRPE 方案)

     編輯:MySQL 綜合教學

Nagios 監控 MySQL 服務器完整實作指南(NRPE 方案)

本文詳細記錄使用 NRPE(Nagios Remote Plugin Executor)實現 Nagios 遠程監控 MySQL 服務器的完整部署流程,涵蓋被監控端與監控端雙向配置、插件編譯、SSL 連線、服務註冊與圖形化驗證。

架構原理

  • 監控端:運行 check_nrpe 插件,發起檢查請求
  • 被監控端:運行 nrpe daemon,接收請求並調用本地 Nagios Plugins 執行檢查
  • 通訊方式:SSL 加密傳輸,埠口 5666
  • 優勢:開銷低於 SSH,無需系統帳號,安全性高

被監控端部署(MySQL 服務器)

1. 建立 nagios 用戶

useradd -s /sbin/nologin nagios

2. 安裝依賴與 Nagios Plugins

yum -y install gcc gcc-c++ make openssl openssl-devel
# 下載並編譯安裝 nagios-plugins(略,假設已安裝至 /usr/local/nagios/libexec/)

3. 編譯安裝 NRPE

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

4. 配置 nrpe.cfg 關鍵參數

grep -v '^#' /usr/local/nagios/etc/nrpe.cfg | sed '/^$/d'

核心配置項:

參數值說明
server_port5666NRPE 監聽埠
allowed_hosts127.0.0.1,192.168.121.211允許連接的監控端 IP(必須含監控端)
dont_blame_nrpe0禁止命令參數傳遞(安全考量)
command_timeout60命令執行超時秒數

5. 定義 MySQL 監控命令

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

6. 建立啟動腳本並註冊服務

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

7. 驗證本地監控

/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;;; ...

8. 解決共享庫依賴問題

find / -name libmysqlclient.so.18
ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/libmysqlclient.so.18

9. 建立監控專用 MySQL 帳號

GRANT PROCESS, SUPER, REPLICATION CLIENT ON *.* 
TO 'nagios'@'localhost' IDENTIFIED BY 'nagiosq@0625';
FLUSH PRIVILEGES;

監控端部署

1. 僅安裝 check_nrpe 插件

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

2. SSL 連線測試與允許名單

./check_nrpe -H 192.168.121.210
# 若報錯 "Could not complete SSL handshake",檢查被監控端 allowed_hosts 是否包含監控端 IP

Nagios 核心配置整合

1. commands.cfg 定義命令

# 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
}

2. servers.cfg 定義服務監控項

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

3. nagios.cfg 載入配置

cfg_file=/usr/local/nagios/etc/servers.cfg
# 重啟 Nagios 生效
service nagios restart

驗證與常見問題

  • Web 界面應顯示 MySQL 服務狀態為 OK,並展示效能指標圖表
  • SSL 握手失敗:雙向確認 allowed_hosts、防火牆 5666 埠、SELinux
  • 共享庫缺失:建立 libmysqlclient.so.18 軟連結至 /usr/lib64/
  • 權限不足:確認 nagios 用戶擁有插件執行權限與 Socket 檔案讀取權限

此架構可輕鬆擴展至監控數十台 MySQL 實例,只需在各被監控端重複部署 NRPE 並調整 commands 定義即可。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved