1. 安裝 nrpe 並只編譯 check_nrpe 插件:
[root@nagios ~]# tar -zxvf nrpe-2.14.tar.gz [root@nagios ~]# cd nrpe-2.14 [root@nagios nrpe-2.14]# ./configure [root@nagios nrpe-2.14]# make all [root@nagios nrpe-2.14]# make install-plugin
監控端只需要 check_nrpe 這個插件,所以執行到這步就夠了。
2. 在 commands.cfg 定義 nrpe 的用法:
[root@nagios ~]# vim /usr/local/nagios/etc/objects/commands.cfg
# 'check_nrpe ' command definition
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
說明:command_name 定義名稱為 check_nrpe,services.cfg 裡要引用這個名字;command_line 是實際執行的插件,寫法必須完全對應 check_nrpe 的用法(可用 check_nrpe --h 查).
-c 後面的 $ARG1$ 是傳給 nrpe daemon 的檢測命令名稱,而且必須是被控端 nrpe.cfg 裡有定義的那幾條命令之一;在 services.cfg 使用時要用 ! 帶上這個參數。
3. 在 nagios.cfg 中載入設定檔:
[root@nagios ~]# vim /usr/local/nagios/etc/nagios.cfg 在 cfg_file=/usr/local/nagios/etc/objects/localhost.cfg 下添加: cfg_file=/usr/local/nagios/etc/objects/services.cfg
4. 建立 services.cfg 定義監控對象與服務(address 為被控端 IP):
[root@nagios ~]# vim /usr/local/nagios/etc/objects/services.cfg
#定義被監控端名稱和地址
define host{
use linux-server
host_name server1
alias server1
address 192.168.7.93
}
接著為每個監控項目各定義一個 service,格式如下(check_command 帶 ! 加上被控端定義的命令名):
define service{
host_name server1
service_description check_ping
check_command check_nrpe!check_ping
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
}
依同樣格式再複製出 check_users、check_load、check_disk_sda3、check_zombie_procs、check_total_procs、check_swap、check_http、check_ssh、check_mysql 等服務(把 service_description 與 check_command 的參數換掉即可)。
注意:services.cfg 若有語法錯誤,會導致 Nagios 無法啟動。存檔後先驗證:
[root@nagios ~]# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
5. 重啟服務:
[root@nagios ~]# service nagios restart [root@nagios ~]# service httpd restart
6. 若無法訪問,先確認 iptables 並放行 5666:
[root@nagios ~]# service iptables status [root@nagios ~]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT [root@nagios ~]# service iptables restart
測試監控端與被控端 nrpe daemon 的通信:
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.7.93 //被監控端 IP NRPE v2.14
1. 增加 nagios 使用者:
[root@web ~]# useradd nagios [root@web ~]# passwd nagios
2. 安裝 nagios 插件:
[root@web ~]# tar -zxvf nagios-plugins-1.4.16.tar.gz [root@web ~]# cd nagios-plugins-1.4.16 [root@web nagios-plugins-1.4.16]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios [root@web nagios-plugins-1.4.16]# make;make install [root@web ~]# ls /usr/local/nagios/ include libexec share [root@web ~]# chown -R nagios.nagios /usr/local/nagios [root@web ~]# chown -R nagios.nagios /usr/local/nagios/libexec
3. 安裝 nrpe 服務:
[root@web ~]# tar -zxvf nrpe-2.14.tar.gz [root@web ~]# cd nrpe-2.14 [root@web nrpe-2.14]# ./configure
輸出會列出編譯摘要,確認 NRPE port 為 5666、user/group 為 nagios:
*** Configuration summary for nrpe 2.8.1 05-10-2007 ***: General Options: ------------------------- NRPE port: 5666 NRPE user: nagios NRPE group: nagios Nagios user: nagios Nagios group: nagios
接著編譯並安裝:
[root@web nrpe-2.14]# make all [root@web nrpe-2.14]# make install-plugin [root@web nrpe-2.14]# make install-daemon [root@web nrpe-2.14]# make install-daemon-config [root@web nrpe-2.14]# ls /usr/local/nagios/ bin etc include libexec share
NRPE daemon 是作為 xinetd 下的服務運行,系統一般已預裝 xinetd。
4. 安裝並編輯 xinetd 腳本:
[root@web nrpe-2.14]# make install-xinetd
/usr/bin/install -c -m 644 sample-config/nrpe.xinetd /etc/xinetd.d/nrpe
[root@web ~]# vim /etc/xinetd.d/nrpe
# default: on
# description: NRPE (Nagios Remote Plugin Executor)
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 //在後面加上監控主機位址
}
改為:
only_from = 127.0.0.1,192.168.7.249
5. 設定 nrpe.cfg:
[root@web ~]# vi /usr/local/nagios/etc/nrpe.cfg 81 allowed_hosts=127.0.0.1,192.168.7.249 //允許監控主機來查 將 219-223 這 5 行註解掉,並在末尾添加: command[check_ping]=/usr/local/nagios/libexec/check_ping -H 192.168.7.93 -w 3000.0,80% -c 5000.0,100% -p 5 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_disk_sda3]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /dev/sda3 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 command[check_swap]=/usr/local/nagios/libexec/check_swap -w 20% -c 10% command[check_http]=/usr/local/nagios/libexec/check_http -H 192.168.7.93 command[check_ssh]=/usr/local/nagios/libexec/check_ssh -H 192.168.7.93 command[check_mysql]=/usr/local/nagios/libexec/check_tcp -H 192.168.7.93 -p 3306 ---------------------
重點對應關係:客戶端 nrpe.cfg 裡的 command[check_mysql] 要與服務端 services.cfg 裡的 check_command check_nrpe!check_mysql 配對;services.cfg 的格式在服务端 commands.cfg 中有詳解。
6. 啟動 NRPE:
[root@web ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d #netstat -an | grep 5666 //確認是否監聽 5666
重啟方式:用 lsof -i:5666 找到 PID,kill -9 後再重跑上面的啟動指令。
7. 放行防火牆 5666:
[root@web ~]# service iptables status [root@web ~]# vim /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 5666 -j ACCEPT [root@web ~]# service iptables restart
8. 回到服務端測試:
[root@nagios ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.7.93 NRPE v2.14
9. 查看 nrpe 行程:
[root@web ~]# ps -elf |grep nrpe 5 S nagios 1480 1 0 80 0 - 9708 poll_s Mar14 ? 00:00:00 /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
10. 登入 Web 介面。設定密碼:
htpasswd -c /usr/local/nagios/htpasswd.users nagiosadmin
瀏覽器訪問 https://192.168.7.249/nagios/,輸入 nagiosadmin 與密碼即可。

點擊 Services 查看各項監控狀態:

大功告成。
疑難:Connection refused by host。在 CentOS 6.3 x64 上裝好 nrpe 後執行 check_nrpe -H localhost 卻被拒絕,解法是先把 NRPE 起起來:
[root@web ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d # netstat -an | grep 5666 tcp 0 0 127.0.0.1:5666 0.0.0.0:* LISTEN
看到 LISTEN 就是啟動成功,再執行 check_nrpe -H localhost 會回傳 NRPE v2.14。
【客戶端】1. 新增自訂腳本:
vi check_mem.sh
-----------------
TOTAL=`free -m | head -2 |tail -1 |gawk '{print $2}'`
# Free memory
FREE=`free -m | head -2 |tail -1 |gawk '{print $4}'`
# to calculate free percent
# use the expression free * 100 / total
FREETMP=`expr $FREE \* 100`
PERCENT=`expr $FREETMP / $TOTAL`
echo "$FREE MB ($PERCENT%) Free Memory"
exit 0
-------------------------
# cp check_mem.sh /usr/local/nagios/libexec
# chmod u+x check_mem.sh
# chown nagios check_mem.sh
2. 在 nrpe.cfg 註冊命令:
vi /usr/local/nagios/etc/nrpe.cfg command[check_mem]=/usr/local/nagios/libexec/check_mem.sh -w 150 -c 100
3. 本機先測腳本:
# ./check_mem.sh 200 MB (1%) Free Memory
4. 重啟 nrpe:
[root@server1 libexec]# lsof -i:5666 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME nrpe 4457 nagios 4u IPv4 485359 0t0 TCP *:5666 (LISTEN) [root@server1 libexec]# kill -9 4457 [root@server1 libexec]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
【服務端】1. 在 services.cfg 加上監控項:
vi /usr/local/nagios/etc/object/services.cfg
---------
define service{
host_name server1
service_description check_mem
check_command check_nrpe!check_mem
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
}
---------
2. 重啟 nagios:
service nagios restart
3. 重新訪問 https://192.168.7.249/nagios/ 即可看到 check_mem:

原文出自「一路向北」部落格:https://showerlee.blog.51cto.com/2047005/1158125