第一次在 Ubuntu 上裝 Nagios,把過程完整記錄下來。Nagios 本身很強大,能與大量軟體整合,這篇會隨後續加上更多插件而持續補充。特別感謝 https://nagios-cn.sourceforge.net/ 的作者。
從 www.nagios.org 下載 nagios-3.0.3.tar.gz 與 nagios-plugins-1.4.12.tar.gz,然後:
apt-get install apache2 php5-gd build-essential libgd2-xpm-dev php5 \ mysql-server libmysqlclient15-dev libssl-dev perl
useradd nagios passwd nagios groupadd nagcmd usermod -G nagcmd nagios usermod -G nagcmd www-data
tar zxvf nagios-3.0.3.tar.gz cd nagios-3.0.3 ./configure --with-command-group=nagcmd make all make install make install-init make install-config make install-commandmode vi /usr/local/nagios/etc/objects/contacts.cfg make install-webconf htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin /etc/init.d/apache2 reload
tar zxvf nagios-plugins-1.4.12.tar.gz cd nagios-plugins-1.4.12 ./configure --with-nagios-user=nagios --with-nagios-group=nagios make make install ln -s /etc/init.d/nagios /etc/rcS.d/S99nagios /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios start apt-get install mailx
瀏覽器打開 https://localhost/nagios 即可看到畫面。

apt-get install xinetd tar zxvf nrpe-2.12.tar.gz cd nrpe-2.12 ./configure make all make install-plugin make install-daemon make install-daemon-config make install-xinetd vim /etc/xinetd.d/nrpe
把 only_from 改為 127.0.0.1 加上 Nagios 伺服端的位址,並在 /etc/services 末尾補上:
nrpe 5666/tcp #NRPE
重啟 xinetd 並驗證:
/etc/init.d/xinetd restart netstat -an | grep nrpe /usr/local/nagios/libexec/check_nrpe -H localhost
看到 tcp 0 0 *:nrpe *:* LISTEN,且 check_nrpe 回傳 NRPE v2.12,就代表成功。之後可編輯 /usr/local/nagios/etc/nrpe.cfg 加入要監控的項目。
tar zxvf nrpe-2.12 cd nrpe-2.12 ./configure make all make install-plugin /usr/local/nagios/libexec/check_nrpe -H 192.168.0.1
在 commands.cfg 末尾加上:
define command{
command_name check_nrpe
command_line $USER1$/check_nrpe -H $HOSTADDRESS$ -c $ARG1$
}
之後就能在 service 定義裡寫 check_nrpe!check_load 這類指令。
PNP 是個小型開源包,基於 PHP 與 Perl,用 rrdtool 把 Nagios 採集的數據畫成圖表。事前準備三件事:整合 Apache 與 PHP、安裝 rrdtool、安裝 Perl。Ubuntu 上直接 apt-get 最省事。
原理在於 Nagios 的數據輸出介面。host 與 service 定義中都有 process_perf_data,設為 1 才會啟用數據輸出。主設定檔 nagios.cfg 需要:
process_performance_data= 1 service_perfdata_command=process-service-perfdata
想對某個監控項出圖,還要在該 service 定義中加 process_perf_data 1。Nagios 預設的 process-service-perfdata 定義較簡單,PNP 提供了更完整的 Perl 腳本,把它換上去:
define command{
command_name process-service-perfdata
command_line /usr/local/nagios/libexec/process_perfdata.pl
}
接著編譯安裝 PNP:
./configure --with-rrdtool=/usr/bin/rrdtool \ --with-perfdata-dir=/usr/local/nagios/share/perfdata/ make all make install make install-config cd /usr/local/nagios/etc/pnp mv rra.cfg-sample rra.cfg mv process_perfdata.cfg-sample process_perfdata.cfg mv npcd.cfg-sample npcd.cfg /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg /etc/init.d/nagios restart
瀏覽器開啟 https://IP/nagios/pnp/ 驗證。

如果圖是空的,通常代表 service.cfg 裡還沒設定要監控的主機,補上即可。
本文出自「linuxer」部落格,請務必保留出處 https://deidara.blog.51cto.com/400447/88449