そろそろ、スクリプトで監視するのも面倒になってきたので監視ツールである「nagios-3.2.3」をインストールしてみようと思い、試しに検証環境にインストールする事にしました。
■nagios公式
http://library.nagios.com/
■検証環境
■使用OS
CentOS 5.5 64bit
■nagiosバージョン
nagios-3.2.3
■nagios-pluginバージョン
nagios-plugins-1.4.15
■日本語化パッチ
nagios-jp-3.2.2
※「Nagios-JP」では、「nagios-3.1.0」までの日本語化パッチしかないため、今回はももーいさんが作成して頂いたパッチを使用します。
■必要なパッケージ
httpd
php
gcc
glibc
glibc-common
gd
gd-devel
■説明
「nagios」をインストールした「kurobuti.kanshi.local」が「kurobuti.local」の監視対象を監視します。
また、「kurobuti.kanshi.local」は自ホストのリソースやポート(プロセス)監視も行います。
(1)必要なパッケージをインストールする。
[root@kurobuti ~]# yum -y install httpd php gcc glibc glibc-common gd gd-devel
(2)「nagios」で使用するユーザとグループの作成と設定をする。
[root@kurobuti ~]# useradd -m -s /sbin/nologin nagios [root@kurobuti ~]# groupadd nagcmd [root@kurobuti ~]# usermod -a -G nagcmd nagios [root@kurobuti ~]# usermod -a -G nagcmd apache
(3)「nagios」と「Plugin」のダウンロードをする。
下記のアドレスからダウンロードする。
http://www.nagios.org/download
・ダウンロードするもの
①「Nagios Core」
②「Nagios Plugin」
・ダウンロード方法
①ダウンロード対象の「Get Nagios xx」をクリックする。
②「Email」欄にアドレスを入力して「Submit」をクリックする。
③「Nagios xx」をダウンロードする。
(4)「nagios日本語化パッチ」をダウンロードする。
[root@kurobuti ~]# wget http://ftp.momo-i.org/pub/other/nagios-jp-3.2.3.patch.gz
(5)「nagios」のインストールをする。
[root@kurobuti ~]# tar zxvf nagios-3.2.3.tar.gz [root@kurobuti ~]# gunzip nagios-jp-3.2.3.patch.gz [root@kurobuti ~]# cd nagios-3.2.3 [root@kurobuti nagios-3.2.3]# patch -p1 < ../nagios-jp-3.2.3.patch [root@kurobuti nagios-3.2.3]# ./configure --with-command-group=nagcmd --sysconfdir=/etc/nagios [root@kurobuti nagios-3.2.3]# make all [root@kurobuti nagios-3.2.3]# make install [root@kurobuti nagios-3.2.3]# make install-init [root@kurobuti nagios-3.2.3]# make install-config [root@kurobuti nagios-3.2.3]# make install-commandmode [root@kurobuti nagios-3.2.3]# make install-webconf
(6)「nagios-plugin」のインストールをする。
[root@kurobuti ~]# tar zxvf nagios-plugins-1.4.15.tar.gz [root@kurobuti ~]# cd nagios-plugins-1.4.15 [root@kurobuti nagios-plugins-1.4.15]# ./configure --with-nagios-user=nagios --with-nagios-group=nagios --sysconfdir=/etc/nagios [root@kurobuti nagios-plugins-1.4.15]# make [root@kurobuti nagios-plugins-1.4.15]# make install
(7)「nagios」の設定をする。
[root@kurobuti ~]# cd /etc/nagios/ [root@kurobuti nagios]# vi nagios.cfg ← Nagiosの設定を行う log_file=/usr/local/nagios/var/nagios.log ↓変更 log_file=/var/log/nagios/nagios.log #cfg_dir=/etc/nagios/servers ↓変更(コメントアウト) cfg_dir=/etc/nagios/servers [root@kurobuti nagios]# vi objects/contacts.cfg ← アラートメール送信先アドレスを設定する define contact{ contact_name nagiosadmin ; Short name of user use generic-contact ; Inherit default values from generic-contact template (defined above) alias Nagios Admin ; Full name of user email nagios@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ****** ↓変更(アラート連絡先) email hogehoge@localhost ; <<***** CHANGE THIS TO YOUR EMAIL ADDRESS ****** } [root@kurobuti nagios]# vi objects/commands.cfg ← 日本語でメールを送信する際の文字化け防止設定を行う # 'notify-host-by-email' command definition define command{ command_name notify-host-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ ↓変更 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | /usr/bin/nkf -j | /bin/mail -s "** $NOTIFICATIONTYPE$ Host Alert: $HOSTNAME$ is $HOSTSTATE$ **" $CONTACTEMAIL$ } # 'notify-service-by-email' command definition define command{ command_name notify-service-by-email command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ ↓変更 command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | /usr/bin/nkf -j | /bin/mail -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" $CONTACTEMAIL$ } [root@kurobuti nagios]# vi objects/localhost.cfg ← ローカルホストの設定を行う # Define a service to check SSH on the local machine. # Disable notifications for this service by default, as not all users may have SSH enabled. define service{ use local-service ; Name of service template to use host_name localhost service_description SSH check_command check_ssh notifications_enabled 0 ↓変更 notifications_enabled 1 ← ローカルホストのSSH状態に異常が起きたらアラートメールを送信する } # Define a service to check HTTP on the local machine. # Disable notifications for this service by default, as not all users may have HTTP enabled. define service{ use local-service ; Name of service template to use host_name localhost service_description HTTP check_command check_http notifications_enabled 0 ↓変更 notifications_enabled 1 ← ローカルホストのHTTP状態に異常が起きたらアラートメールを送信する } [root@kurobuti nagios]# vi objects/templates.cfg ← アラート送信時間を変更する define host{ name linux-server use generic-host check_period 24x7 check_interval 5 retry_interval 1 max_check_attempts 10 check_command check-host-alive notification_period workhours ← この状態だと平日の9時~17時の間にしかアラートメールが送信されない ↓変更 notification_period 24x7 ← 24時間365日アラートメールを送信するようにする notification_interval 120 notification_options d,u,r contact_groups admins register 0 } define service{ name generic-service ; The 'name' of this service template ・ ・ ・ notification_interval 120 ; Re-notify about service problems every hour ↓変更(アラート通知を一度のみにする。通常は2時間毎に再送される。) notification_interval 0 ; Re-notify about service problems every hour
(8)「nagios」で使用するディレクトリを作成する。
[root@kurobuti ~]# mkdir /var/log/nagios [root@kurobuti ~]# chown nagios:nagios /var/log/nagios [root@kurobuti ~]# mkdir /etc/nagios/servers
(9)「nagios」で管理者アカウントを作成する。
[root@kurobuti ~]# htpasswd -c /etc/nagios/htpasswd.users nagiosadmin New password:パスワードを入力 Re-type new password:パスワードを入力 Adding password for user nagiosadmin※通常の設定では、「nagiosadmin」でベーシック認証しないと監視ページが見れないです(nagiosのTOPページ+その他は表示されます)。もし独自アカウントを作成、または認証がいらない場合は「/etc/nagis/cgi.cfg」をイジイジしてください。
(10)監視対象ノードの設定をする
[root@kurobuti nagios]# vi /etc/nagios/servers/kurobuti.local.cfg # ---以下を追加--- define host{ use linux-server host_name kurobuti.local ←ホスト名 alias kurobuti.local ←ホスト名 address 192.168.0.62 ← 監視ノードIPアドレス } # ---以下は監視対象ポート(プロセス)--- define service{ use generic-service host_name kurobuti.local service_description HTTP check_command check_http } define service{ use generic-service host_name kurobuti.local service_description SMTP check_command check_smtp!-t 5 } define service{ use generic-service host_name kurobuti.local service_description POP3 check_command check_pop } define service{ use generic-service host_name kurobuti.local service_description SSH check_command check_ssh } define service{ use generic-service host_name kurobuti.local service_description PING check_command check_ping!100.0,20%!500.0,60% }
(11)「nagios」の設定ファイルチェックをする。
[root@kurobuti ~]# /usr/local/nagios/bin/nagios -v /etc/nagios/nagios.cfg Nagios Core 3.2.3 Copyright (c) 2009-2010 Nagios Core Development Team and Community Contributors Copyright (c) 1999-2009 Ethan Galstad Last Modified: 10-03-2010 License: GPL Website: http://www.nagios.org Reading configuration data... Read main config file okay... Processing object config file '/etc/nagios/objects/commands.cfg'... Processing object config file '/etc/nagios/objects/contacts.cfg'... Processing object config file '/etc/nagios/objects/timeperiods.cfg'... Processing object config file '/etc/nagios/objects/templates.cfg'... Processing object config file '/etc/nagios/objects/localhost.cfg'... Processing object config directory '/etc/nagios/servers'... Processing object config file '/etc/nagios/servers/kurobuti.local.cfg'... Read object config files okay... Running pre-flight check on configuration data... Checking services... Checked 12 services. Checking hosts... Checked 2 hosts. Checking host groups... Checked 1 host groups. Checking service groups... Checked 0 service groups. Checking contacts... Checked 1 contacts. Checking contact groups... Checked 1 contact groups. Checking service escalations... Checked 0 service escalations. Checking service dependencies... Checked 0 service dependencies. Checking host escalations... Checked 0 host escalations. Checking host dependencies... Checked 0 host dependencies. Checking commands... Checked 24 commands. Checking time periods... Checked 5 time periods. Checking for circular paths between hosts... Checking for circular host and service dependencies... Checking global event handlers... Checking obsessive compulsive processor commands... Checking misc settings... Total Warnings: 0 Total Errors: 0 Things look okay - No serious problems were detected during the pre-flight check※「Error」や「Warnings」が無いこと。
(12)「nagios」を起動する。
[root@kurobuti ~]# /etc/rc.d/init.d/nagios start Starting nagios:This account is currently not available. done. [root@kurobuti ~]# chkconfig nagios on
(13)「httpd」を起動する。
[root@kurobuti ~]# /etc/rc.d/init.d/httpd start httpd を起動中: [ OK ] [root@kurobuti ~]# chkconfig httpd on
(14)「nagios」の管理ページにアクセスする。
ブラウザのアドレス欄に「http://IP Addr or Domain/nagios/」を入力し、Basic認証を行いアクセスする。

もし、「localhost」の「HTTP」が以下のような警告を出していた場合

これは、「localhost」の「DocumentRoot」にコンテンツが無い場合に発生します。
対処法としては、「DocumentRoot」にコンテンツを置くか、または以下の設定をします。
[root@kurobuti ~]# vi /etc/nagios/objects/localhost.cfg define service{ use local-service ; Name of service template to use host_name localhost service_description HTTP check_command check_http ↓変更 check_command check_http!-e 403 notifications_enabled 1 }
(15)サーバに異常が発生した場合メールが送信されるか確認する。

上図のように、エラーが発生した場合は「/etc/nagios/objects/contacts.cfg」に設定したアドレス宛へメールが送信されること。
また、復旧後も復旧メールが送信されること。
参考文献
インストール時に参考にした文献
http://nagios.sourceforge.net/docs/3_0/quickstart-fedora.html
設定時に参考にした文献
http://www.isl.ne.jp/it/nagios/nagios_003.html
http://umiushi.dip.jp/wordpress/linuxconfig/nagios-menu/nagios30_04/
http://centossrv.com/nagios.shtml
http://www.fireproject.jp/feature/nms/nagios/intro.html
「nagios3.x」日本語ドキュメント
http://oss.aspect-sys.co.jp/nagios_jp/
日本語パッチ配布
http://sourceforge.jp/projects/nagios-jp/
http://www.momo-i.org/chapter5/nagios-3x.html