nagios 可以用來監控主機的服務運作狀態,比方說 ping不ping的到、http可不可以瀏覽等等,

以上都是被監控者不需要作什麼額外設定就可以即時被監控得到的,

假如現在我要監控遠端主機的 CPU、硬碟空間、記憶體等等,怎麼作?

Nagios 提供了一個外掛,叫 NRPE (Nagios Remote Plugin Executor),

它可以讓 nagios server 在固定時間去抓 nagios client 被監控的項目回來判斷是否ok。

如何作 :

Nagios Client 端 (被監控者) ——————————

編譯時需要

yum install gcc

編譯 nrpe 時需要

yum install openssl-devel

nrpe 不允許使用 root 使用者去執行,所以要增加一個新的

useradd -c “Nagios” -d /usr/local/nagios -s /sbin/nologin nagios

編譯 nagios plugins

內容包含很多程式,可以用來取主機的數據

tar zxvf nagios-plugins-1.4.15.tar.gz
cd nagios-plugins-1.4.15
./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios
make
make install

編譯 nrpe

故事主角,拿來當daemon,nagios server 就可以透過它來得到 nagios client 資訊

tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios –enable-ssl
make all
make install-plugin
make install-daemon
make install-daemon-config

如果你要讓 nrpe 是搭配 xinetd 來跑的話
要多安裝給 xinetd 用

make install-xinetd

然後編輯 vi /etc/xinetd.d/nrpe,設定允許 nagios server 存取

only_from = 127.0.0.1 10.10.10.123

再編輯 vi /etc/services,加入一行,讓 xinetd 辨識 5666 是 nrpe

nrpe      5666/tcp    # NRPE

最後 service xinetd restart 即可

除了可以使用 xinetd 啟動 nrpe,nrpe 也是可以獨立啟動的
在 nrpe 被編譯完後,目錄裡會有個 init-script 檔,把它複製至 /etc/init.d/

cp -rp nrpe-2.12/init-script /etc/init.d/nrpe
chmod +x /etc/init.d/nrpe

編輯 vi /usr/local/nagios/etc/nrpe.cfg
其它都照預設即可,只要修正 allowed_hosts 即可

log_facility=daemon
pid_file=/var/run/nrpe.pid
server_port=5666
nrpe_user=nagios
nrpe_group=nagios
allowed_hosts=127.0.0.1,10.10.10.123 # 加入 nagios server 的ip,允許它來連線 (逗點區隔)

記得改權限

chown nagios.nagios /usr/local/nagios

chown -R nagios.nagios /usr/local/nagios/libexec

 

就可以這樣子啟動 nrpe 了

/etc/init.d/nrpe start

再設定開機啟動

chkconfig nrpe on

 

 

測試 nagios client 的 nrpe 是否 ok

/usr/local/nagios/libexec/check_nrpe -H localhost

它的回應結果是如下就成功了

NRPE v2.12

安全性考量,有幾個方式可以作
1。iptables 防火牆開放允許 5666 port 給 nagios server 可以存取
2。hosts.allow 要 nrpe: 10.10.10.123,而 hosts.deny 則是 nrpe:ALL
3。nrpe.cfg 的 allowed_hosts=127.0.0.1,10.10.10.123

nrpe.cfg 它的 allowed_hosts 可以使用 domain.com 名稱(我試了是working的),
所以我只使用 iptables + nrpe.cfg的allowed_hosts,在我改變 nagios server 的ip時,
就不用再去改 nagios client 的設定,
而 iptables 使用 -s domain.com 它直接會抓ip來設定,所以如果多台nagios client時,
我就只會 iptables -I INPUT -m state –state NEW -p tcp –dport 5666 -j ACCEPT,
再更好一點是在這些 nagios client 前面加個硬體防火牆,再由它統一開放給 nagios server 存取

Nagios Service 端 (監控者) ——————————

這裡只說明如何安裝使用 nrpe

編譯nrpe

tar zxvf nrpe-2.12.tar.gz
cd nrpe-2.12
./configure –prefix=/usr/local/nagios –with-nagios-user=nagios –with-nagios-group=nagios –enable-ssl
make all
make install-plugin           # nagios server 上只要有 plugin即可

記得改權限

chown nagios.nagios /usr/local/nagios

chown -R nagios.nagios /usr/local/nagios/libexec

 

測試 nagios server 可不可以成功存取到 nagios client

/usr/local/nagios/libexec/check_nrpe -H 10.10.10.135

一樣回應結果是如下就成功了

NRPE v2.12

到這邊 nrpe 的安裝算是可以了

接著測試怎麼使用 nrpe 去抓到遠端主機的 load average

在 nagios server 執行

 

/usr/local/nagios/libexec/check_nrpe -H 10.10.10.135 -c check_load

得到如下就是我們想要的結果,

OK – load average: 0.00, 0.00, 0.00|load1=0.000;15.000;30.000;0; load5=0.000;10.000;25.000;0; load15=0.000;5.000;20.000;0;

check_nrpe -H 就是要抓那一台主機
check_nrpe -c  用那支程式來抓主機的內容

那nagios server為何知道是 check_load,這是在nagios client 端的  nrpe.cfg 裡就會定義好的 (如下)

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

check_load 名稱就定義在以上的中括號裡,而等於後面就是實際執行的動作

那我是不是可以在 nagios server 端自定 -w 與 -c 的值是多少,這樣子可以不用在 nagios client 端作了 (如果要考濾多台nagios client 與隨時可更動這個判斷數值)

nrpe 是可以達到的
只要在一開始編譯 nrpe 給 client 時多個參數 (–enable-command-args)
及編輯 nrpe.cfg 裡的 bont_blame_nrpe=1

再將nrpe.cfg裡的

command[check_load]=/usr/local/nagios/libexec/check_load -w 15,10,5 -c 30,25,20

換成

command[check_load]=/usr/local/nagios//libexec/check_load -w $ARG1$ -c $ARG2$

此時在 nagios server 端執行的方式就變成

/usr/local/nagios/libexec/check_nrpe -H 10.10.10.135  -c check_load -a 15,10,5 30,25,20

以上的 -a 後面接了兩個數值
$ARG1$ 就是 15,10,5
$ARG2$ 就是 30,25,20

PS: 像這樣子的動作有一個安全性的問題是要被考濾的,如果我帶了 `reboot`,你認為 client 會怎麼辦 !!!

nrpe 就有保護措施了,它是不允許你的參數是帶

   | ` & > < ‘ ” [ ] { } ; !

Related posts 相關文章
使用 Grafana 與 Prometheus 監控主機
More...
簡單容易自己架設的監控平台-Uptime Kuma
More...
監控系統 icinga (nagios 的分支) 安裝 icinga-web 時遇到 500 internal server error
More...
監控系統 icinga (nagios 的分支) Q&A 篇
More...

作者

留言

撰寫回覆或留言

發佈留言必須填寫的電子郵件地址不會公開。