CentOS5.0でNagios2を使う(インストール編)

オープンソースのシステム・ネットワーク監視ツールとして、Nagiosがある。Webベースの管理画面を持ち、標準で用意されているさまざまなプラグインによってPingレベルからアプリケーションレベルまでの柔軟なサービス監視ができる。また被監視サーバ側にエージェントを入れることによりリソース監視もできる。

当サイトではXenで多数の仮想サーバを入れているので、Nagiosを使ってサーバとサービスの監視をしたい。まずはインストール編。

①予め必要なライブラリをインストールしておく。

[root@manager1 ~]# yum install libtool-ltdl.x86_64

②yumのレポジトリを追加する。

[root@manager1 ~]# cat /etc/yum.repos.d/dag.repo
[dag]
name=Dag RPM Repository for Red Hat Enterprise Linux
baseurl=http://ftp.riken.jp/Linux/dag/redhat/el$releasever/en/$basearch/dag
gpgcheck=1
gpgkey=http://ftp.riken.go.jp/pub/Linux/dag/RPM-GPG-KEY.dag.txt
enabled=1

③nagios、プラグイン、依存パッケージをインストールする。

[root@manager1 ~]# yum install nagios
[root@manager1 ~]# yum install perl-Net-SNMP
[root@manager1 ~]# yum install fping
[root@manager1 ~]# yum install nagios-plugins

④apacheの設定も自動的にインストールされるので確認する。

[root@manager1 ~]# cat /etc/httpd/conf.d/nagios.conf

ScriptAlias /nagios/cgi-bin "/usr/lib64/nagios/cgi"

<Directory "/usr/lib64/nagios/cgi">
#  SSLRequireSSL
   Options ExecCGI
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /etc/nagios/htpasswd.users
   Require valid-user
</Directory>

Alias /nagios "/usr/share/nagios"

<Directory "/usr/share/nagios">
#  SSLRequireSSL
   Options None
   AllowOverride None
   Order allow,deny
   Allow from all
#  Order deny,allow
#  Deny from all
#  Allow from 127.0.0.1
   AuthName "Nagios Access"
   AuthType Basic
   AuthUserFile /etc/nagios/htpasswd.users
   Require valid-user
</Directory>

⑤apacheの認証用ユーザを登録する。

[root@manager1 ~]# su nagios
sh-3.1$ htpasswd -c /etc/nagios/htpasswd.users nagios
New password:
Re-type new password:
Adding password for user nagios

⑥apacheを起動する。

[root@manager1 ~]# service httpd start
Starting httpd:                                            [  OK  ]

※iptablesでhttpの通信を許可していること。

ここまで作業をしたら、ブラウザでhttp://localhost/nagios/アクセスするとnagiosのトップページ(静的なHTML)を表示することができる。



nagiosの基本設定ファイルは/etc/nagios/nagis.cfgで、ここで全体の設定やサービス監視に関する設定ファイル名、ログファイル名などを指定する。最小限使う分にはほとんど変更する必要はない。

サービス監視に関する設定ファイルは、以下の2つが有効になっている。

[root@manager1 ~]# vi /etc/nagios/nagis.cfg
...
cfg_file=/etc/nagios/commands.cfg
cfg_file=/etc/nagios/localhost.cfg
...

それ以外の~.cfgファイルはコメントアウトされており、実際にそのようなファイルは存在しない。これはlocalhost.cfgにまとめて書かれている設定内容を複数のファイルに分割して記述する際に使用するものなので、小規模なネットワークだからすべてlocalhost.cfgに書いてしまおうという人は気にしなくてもよい。ここではそのままにしてlocalhost.cfgを編集しよう。

/etc/nagios/cgi.cfgでは、CGIを実行する際の権限を設定する。ApacheのBasic認証の際に使用したユーザ名を使って、どのユーザがどんな権限を持つかを指定する。ここではnagiosというユーザに全権限を与えよう。

[root@manager1 ~]# vi /etc/nagios/cgi.cfg
...
authorized_for_system_information=nagios
authorized_for_configuration_information=nagios
authorized_for_system_commands=nagios
authorized_for_all_services=nagios
authorized_for_all_hosts=nagios
authorized_for_all_service_commands=nagios
authorized_for_all_host_commands=nagios
...

さて、最後に/etc/nagios/localhost.cfgでサービス監視の設定をする。このファイルはいくつかのセクションに分かれている。以下はデフォルトで入っている設定の抜粋。

1.TIME PERIODS

ここで時間に依存した設定をする際に使用する名前を定義する。

define timeperiod{
        timeperiod_name 24x7
        alias           24 Hours A Day, 7 Days A Week
        sunday          00:00-24:00
        monday          00:00-24:00
        tuesday         00:00-24:00
        wednesday       00:00-24:00
        thursday        00:00-24:00
        friday          00:00-24:00
        saturday        00:00-24:00
        }

2.COMMANDS

コマンドの定義は別ファイル(/etc/nagios/commands.cfg)に記述されている。

3.CONTACTS

障害発生時にメールを送信するあて先を定義する。

define contact{
        contact_name                    nagios-admin
        alias                           Nagios Admin
        service_notification_period     24x7
        host_notification_period        24x7
        service_notification_options    w,u,c,r
        host_notification_options       d,r
        service_notification_commands   notify-by-email
        host_notification_commands      host-notify-by-email
        email                           nagios-admin@localhost
        }

4.CONTACT GROUPS

障害発生時にメールを送信するあて先のグループを定義する。

define contactgroup{
        contactgroup_name       admins
        alias                   Nagios Administrators
        members                 nagios-admin
        }

5.HOSTS

監視をする対象のホストを定義する。最後にregister 0と書いている定義はテンプレートであり、実際のホスト定義をするときにuseで呼び出して使用できる。

define host{
        name                            generic-host    ; The name of this host template
        notifications_enabled           1               ; Host notifications are enabled
        event_handler_enabled           1               ; Host event handler is enabled
        flap_detection_enabled          1               ; Flap detection is enabled
        failure_prediction_enabled      1               ; Failure prediction is enabled
        process_perf_data               1               ; Process performance data
        retain_status_information       1               ; Retain status information across program restarts
        retain_nonstatus_information    1               ; Retain non-status information across program restarts
        notification_period             24x7            ; Send host notifications at any time
        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }
...
define host{
        name                            linux-server    ; The name of this host template
        use                             generic-host    ; This template inherits other values from the generic-host template
        check_period                    24x7            ; By default, Linux hosts are checked round the clock
        max_check_attempts              10              ; Check each Linux host 10 times (max)
        check_command                   check-host-alive ; Default command to check Linux hosts
        notification_period             workhours       ; Linux admins hate to be woken up, so we only notify during the day
                                                        ; Note that the notification_period variable is being overridden from
                                                        ; the value that is inherited from the generic-host template!
        notification_interval           120             ; Resend notification every 2 hours
        notification_options            d,u,r           ; Only send notifications for specific host states
        contact_groups                  admins          ; Notifications get sent to the admins by default
        register                        0               ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL HOST, JUST A TEMPLATE!
        }
...
define host{
        use                     linux-server            ; Name of host template to use
                                                        ; This host definition will inherit all variables that are defined
                                                        ; in (or inherited by) the linux-server host template definition.
        host_name               localhost
        alias                   localhost
        address                 127.0.0.1
        }

6.HOST GROUPS

ホストを場所や重要度などでグループ分けする。

define hostgroup{
        hostgroup_name  test
        alias           Test Servers
        members         localhost
        }

7.SERVICES

監視をしたいサービスを定義し、その中で監視対象のホストを列記する。HOSTSと同様に、register 0となっているものはテンプレート。

define service{
        name                            generic-service         ; The 'name' of this service template
        active_checks_enabled           1                       ; Active service checks are enabled
        passive_checks_enabled          1                       ; Passive service checks are enabled/accepted
        parallelize_check               1                       ; Active service checks should be parallelized (disabling this can lead to maj
or performance problems)
        obsess_over_service             1                       ; We should obsess over this service (if necessary)
        check_freshness                 0                       ; Default is to NOT check service 'freshness'
        notifications_enabled           1                       ; Service notifications are enabled
        event_handler_enabled           1                       ; Service event handler is enabled
        flap_detection_enabled          1                       ; Flap detection is enabled
        failure_prediction_enabled      1                       ; Failure prediction is enabled
        process_perf_data               1                       ; Process performance data
        retain_status_information       1                       ; Retain status information across program restarts
        retain_nonstatus_information    1                       ; Retain non-status information across program restarts
        is_volatile                     0                       ; The service is not volatile
        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        }
...
define service{
        name                            local-service           ; The name of this service template
        use                             generic-service         ; Inherit default values from the generic-service definition
        check_period                    24x7                    ; The service can be checked at any time of the day
        max_check_attempts              4                       ; Re-check the service up to 4 times in order to determine its final (hard) st
ate
        normal_check_interval           5                       ; Check the service every 5 minutes under normal conditions
        retry_check_interval            1                       ; Re-check the service every minute until a hard state can be determined
        contact_groups                  admins                  ; Notifications get sent out to everyone in the 'admins' group
        notification_options            w,u,c,r                 ; Send notifications about warning, unknown, critical, and recovery events
        notification_interval           60                      ; Re-notify about service problems every hour
        notification_period             24x7                    ; Notifications can be sent out at any time
        register                        0                       ; DONT REGISTER THIS DEFINITION - ITS NOT A REAL SERVICE, JUST A TEMPLATE!
        }
...
define service{
        use                             local-service         ; Name of service template to use
        host_name                       localhost
        service_description             PING
        check_command                   check_ping!100.0,20%!500.0,60%
        }

ここまで設定して/usr/bin/nagios -v /etc/nagios/nagios.cfgとやると設定エラーをチェックしてくれる。問題がなかったらservice nagios start、chkconfig nagios onとすれば、ブラウザから状態を確認することができる。

初期状態ではlocalhostの監視しかできないので、ここから監視対象をHOSTSやSERVICESに追記していく。設定変更の反映はservice nagios reloadが必要。