CentOS 6.5 + Nginx環境にOSSの監視ツールNagiosをインストールする。
公式サイトではソースコードからインストール方法が記載されているが、メンテが面倒なのでyumでインストールする。
1. インストール
公式リポジトリにはないため、epelを利用する。
# yum install --enablerepo=epel nagios nagios-plugins
nagiosユーザ、グループも追加される。
nginxがnagios関連ファイルにアクセスできるようにnginxユーザをnagiosグループに追加する。
# usermod -Ga nagios nginx
2. BASIC認証の設定
# htpasswd -bc /etc/nagios/passwd id password
idとpasswordは任意の値を設定。
3. 通知先を設定
/etc/nagios/objects/contacts.cfg
email nagios@localhot → 通知先に変更
Nagiosの自動起動の設定と、サービスの起動。
# chkconfig nagios on
# service nagios start
この時点で監視が開始されるが、NagiosのWebインタフェース(例えばTactical View)はCGIの設定が完了するまで利用できない。
4. CGIの設定
NagiosのWebインタフェース(管理画面)では、PHPとCのCGIを利用するため、php-fpmとfgiwrap + spawn-fcgiをインストールする。php-fpmの設定はこのエントリーで実施済みなので、fcgiwrap + spawn-fcgiの設定をこの記事を参考に行う。
まずはfcgiwrapのインストール。
# cd /usr/local/src/
# git clone git://github.com/gnosek/fcgiwrap.git
# cd /usr/local/src/fcgiwrap
# autoreconf -i
# ./configure
# make
# mv fcgiwrap /usr/local/bin/
initスクリプトを上記サイトのものをそのまま作成する。
/etc/init.d/fcgiwrap
fcgiwrapが自動起動するように/etc/rc.localの最後へ下記を追記する。
sudo -u nginx /etc/init.d/fcgiwrap
fcgiwrapの設定が終わった時点で単純なCGIは動作するが、NagiosのWebインタフェースはうまく動作せず、以下のようなエラーが表示される。おそらく認証周りの問題。
Whoops!
Error: Could not read object configuration data!
Here are some things you should check in order to resolve this error:
Verify configuration options using the -v command-line option to check for errors.
Check the Nagios log file for messages relating to startup or status data errors.
よって、spawn-fcgiを導入する
# yum install spawn-fcgi
ここを参考に、/etc/sysconfig/spawn-fcgiへ以下を記載。最初から記載していある項目はコメントアウト。
CGI_SOCKET=/var/run/fcgiwrap.socket
FCGI_PROGRAM=/usr/local/bin/fcgiwrap
FCGI_USER=nginx
FCGI_GROUP=nginx
FCGI_EXTRA_OPTIONS="-M 0700"
OPTIONS="-u $FCGI_USER -g $FCGI_GROUP -s $FCGI_SOCKET -S $FCGI_EXTRA_OPTIONS -F 1 -P /var/run/spawn-fcgi.pid -- $FCGI_PROGRAM"
起動と自動起動の設定。
# chkconfig spawn-fcgi on
# service spawn-fcgi start
5. Pluginの設定
この時点でWebインタフェースのServiceを確認すると全ての監視項目がNGになっている。監視を行うにはplug-inのインストールが必要。
今回は、デフォルトの8つに加えて、smtp, popのplug-inをインストールする。
yum install --enablerepo=epel nagios-plugins-ping nagios-plugins-load nagios-plugins-users nagios-plugins-http nagios-plugins-disk nagios-plugins-ssh nagios-plugins-swap nagios-plugins-procs nagios-plugins-smtp nagios-plugins-pop
smtp, popの設定を行う。smtpはsmtp over SSLでポート番号465を利用する場合下記。
/etc/nagios/objects/localhost.cfg
# smtp(abeerforyou's original)
define service{
use local-service
host_name localhost
service_description SMTP
check_command check_smtp!'-P 465 -w 60 -c 300'
notifications_enabled 1
}
# pop(abeerforyou's original)
define service{
use local-service
host_name localhost
service_description POP
check_command check_pop!'-w 60 -c 300'
notifications_enabled 1
}
以上で、監視が開始される。

Webインタフェースで確認できるReportの値をリセットするには/var/log/nagios/archives配下で該当日付のログを消去すればよい。
例えば3日より古いデータを消すには以下。
find /var/log/nagios/archives/ -mtime +3 | xargs rm -f
■参考