■Nagiosのインストール
yumから行うのではなくソースからインストールを行います。yumからインストールする場合に比較して必要なパッケージの事前追加やユーザ/グループの追加の手動設定など、面倒なことがありますので順を追って作業していきます。
まず、Nagios本体のファイルを以下のサイトからダウンロードします。
http://sourceforge.net/projects/nagios/files/nagios-4.x/nagios-4.0.8/ ( nagios-4.0.8.tar.gz )
http://www.nagios.org/download/plugins ( nagios-plugins-2.0.3.tar.gz )
本体のインストール以前に関連する必要なパッケージを事前にインストールしておきます。
# yum install wget httpd php gcc glibc glibc-common gd gd-devel make net-snmp openssl openssl-devel |
次にユーザnagiosおよびグループnagios/nagcmdを追加します。nagiosは/usr/local/nagios配下にごそっとインストールしますのでホームディレクトリもこれに指定しています。
# useradd -d /usr/local/nagios nagios # chmod 755 /usr/local/nagios # groupadd nagcmd # usermod -a -G nagcmd nagios |
次にようやくnagios本体をインストールします。
# tar xvzf nagios-4.0.8.tar.gz # tar xvzf nagios-plugins-2.0.3.tar.gz |
configureを行います。問題なく実行されれば最後にconfigのサマリーが表示されます。
# cd nagios-4.0.8
# ./configure --with-command-group=nagcmd
<省略>
*** Configuration summary for nagios 4.0.8 08-12-2014 ***:
General Options:
-------------------------
Nagios executable: nagios
Nagios user/group: nagios,nagios
Command user/group: nagios,nagcmd
Event Broker: yes
Install ${prefix}: /usr/local/nagios
Install ${includedir}: /usr/local/nagios/include/nagios
Lock file: ${prefix}/var/nagios.lock
Check result directory: ${prefix}/var/spool/checkresults
Init directory: /etc/rc.d/init.d
Apache conf.d directory: /etc/httpd/conf.d
Mail program: /usr/bin/mail
Host OS: linux-gnu
IOBroker Method: epoll
Web Interface Options:
------------------------
HTML URL: http://localhost/nagios/
CGI URL: http://localhost/nagios/cgi-bin/
Traceroute (used by WAP): /usr/bin/traceroute
Review the options above for accuracy. If they look okay,
type 'make all' to compile the main program and CGIs.
|
続けてコンパイルおよびインストールを行います。
# make all # コンパイルを実行 # make install # バイナリファイルをインストール # make install-init # 起動スクリプトをインストール # make install-commandmode # /usr/local/nagios/var/rwの属性を変更 # make install-config # 設定ファイル一式をインストール # make install-webconf # Apache用の設定ファイルをインストール # make install-exfoliation # 管理画面のexfolicationテーマをインストール |
一部のファイルをコピーしてオーナーを変更しておきます。
# cp -R contrib/eventhandlers/ /usr/local/nagios/libexec/ # chown -R nagios.nagios /usr/local/nagios/etc/nagios.cfg |
nagios本体に引き続きnagios-pluginをインストールします。configureを実行した際にインストールに必要なソフトウェアが欠落している場合はそのプラグインはインストールされません。configureの際にWARNINGが表示されますので、それを見ながら必要に応じてインストールを行ってください。
# cd nagios-plugins-2.0.3 # ./configure --with-nagios-user=nagios --with-nagios-group=nagios --with-openssl # make # make install |
まだデフォルトの状態ですが、nagiosの設定ファイルの書式チェックを以下のコマンドで行えます。書式にエラーなどがあった場合などは最後に記載されますので0になるまで修正を繰り返してください。
# /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
Nagios Core 4.0.8
Copyright (c) 2009-present Nagios Core Development Team and Community Contributors
Copyright (c) 1999-2009 Ethan Galstad
Last Modified: 08-12-2014
License: GPL
Website: http://www.nagios.org
Reading configuration data...
Read main config file okay...
Read object config files okay...
Running pre-flight check on configuration data...
Checking objects...
Checked 8 services.
Checked 1 hosts.
Checked 1 host groups.
Checked 0 service groups.
Checked 1 contacts.
Checked 1 contact groups.
Checked 24 commands.
Checked 5 time periods.
Checked 0 host escalations.
Checked 0 service escalations.
Checking for circular paths...
Checked 1 hosts
Checked 0 service dependencies
Checked 0 host dependencies
Checked 5 timeperiods
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
|
デフォルトだとWeb経由の管理画面にアクセスするにはパスワード制限がかかっております。但し、それ用のユーザ名、パスワードは未登録の状態なので以下のコマンドでユーザを登録します。下記ではユーザ名「nagiosadmin」で登録しています。
# htpasswd -c /usr/local/nagios/etc/htpasswd.users nagiosadmin New password:[パスワード入力] Re-type new password:[再度パスワード入力] Adding password for user nagiosadmin |
CentOS7という前提ですが、nagios-4.0.8では/etc/rc.d/init.dからの起動となりますので、従来通りのchkconfigで起動サービスを有効にして起動します。
# chkconfig --add nagios # /etc/rc.d/init.d/nagios start |
そしてapacheも起動します。
# systemctl start httpd.service |
http://ip-address/nagios/でnagiosの管理画面にアクセスします。ログインユーザIDおよびパスワードを聞かれますので上で設定したものを入力してください。以下の画面が見えればインストールは成功です。
■Nagiosの設定
nagiosには複数の設定ファイルがインストールされ、それぞれのコンフィグファイルが連携して動く仕様となっており少々ややこしいです。設定ファイル一覧を下記にまとめてみました。
| 設定ファイル | ファイルの説明 |
| /usr/local/nagios/etc/nagios.cfg | nagiosのメイン設定ファイル |
| /usr/local/nagios/etc/objects/commands.cfg | チェックコマンド一覧。実際に実行されるのはnagios plugin。 |
| /usr/local/nagios/etc/objects/contacts.cfg | 異常が発生した際の通知先を指定。 |
| /usr/local/nagios/etc/objects/timeperiods.cfg | 通知の時間帯を指定。 |
| /usr/local/nagios/etc/objects/templates.cfg | 監視対象ホストや監視サービスに関する監視間隔や通知時間などのテンプレート。 |
| /usr/local/nagios/objects/任意ファイル名 | /usr/local/nagios/etc/nagios.cfgにてファイル名を指定。 |
上記にも書いたようにNagiosのメイン設定ファイルは/usr/local/nagios/etc/nagios.cfgです。まず、このファイルを編集して監視対象を登録するファイルを指定します。下記では監視対象を定義するobjects.cfgというファイルを指定しています。
# vi /usr/local/nagios/etc/nagios.cfg <省略> # Definitions for monitoring the local (Linux) host #cfg_file=/usr/local/nagios/etc/objects/localhost.cfg cfg_file=/usr/local/nagios/etc/objects/objects.cfg <省略> |
上記の/usr/local/nagios/etc/objects/objects.cfgファイルに監視対象の機器を定義していきます。記載例が下記となります。
# vi /usr/local/nagios/etc/objects/objects.cfg
define host{
use generic-switch
host_name core-switch
alias センタースイッチ
address 192.168.18.1
}
define host{
use generic-switch
host_name access-switch
alias アクセススイッチ
parents core-switch
address 192.168.21.1
}
define service{
use generic-service
host_name core-switch
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
define service{
use generic-service
host_name access-switch
service_description PING
check_command check_ping!100.0,20%!500.0,60%
}
|
この中で設定している項目を下記にまとめます。
| use | 使用するtemplateを指定する。 templaste一覧は/usr/local/nagios/objects/templates.cfgを参照。 generic-switch / windows-server / linux-serverなどがあり、 明示的な指定がなければtemplateで設定されている値が適用される。 |
| host_name | ホスト名を指定。 |
| alias | ホスト名の別名を指定。 |
| address | 監視対象のIPアドレスを指定。 |
| parents | 監視対象の親ホストを指定。例えばwebサーバを監視するときにwebサーバ に接続しているスイッチがダウンした場合はwebサーバにもアクセスできな くなる。このような場合において、スイッチはwebサーバの親ホストとな り、スイッチがダウンした場合はwebサーバのステータスがUNREACHABLE 状態となる。 |
| check_command | 実行するpluginを指定する。plugin一覧はcommands.cfgを参照。 check_ping!100.0,20%!500.0,60%は!で引数を区切っており、1つ目の引 数は応答が100ms以上もしくは20%以上ロストで警告、2つ目の引数は応答 が500ms以上もしくは60%以上ロストでクリティカルという意味となる。 |
上記で指定しているgeneric-switchはtemplate.cfgの中で以下のように定義されています。generic-serviceについてもほぼ同様な意味となり明示的に指定しない場合はここの設定されている値が適用されます。
# cat /usr/local/nagios/etc/objects/templates.cfg
<省略>
###############################################################################
###############################################################################
#
# HOST TEMPLATES
#
###############################################################################
###############################################################################
<省略>
# Define a template for switches that we can reuse
define host{
name generic-switch ; The name of this host template
use generic-host ; Inherit default values from the generic-
host template
check_period 24x7 ; By default, switches are monitored round
the clock
check_interval 5 ; Switches are checked every 5 minutes
retry_interval 1 ; Schedule host check retries at 1 minute
intervals
max_check_attempts 10 ; Check each switch 10 times (max)
check_command check-host-alive ; Default command to check if rout
ers are "alive"
notification_period 24x7 ; Send notifications at any time
notification_interval 30 ; Resend notifications every 30 minutes
notification_options d,r ; Only send notifications for specific hos
t states
contact_groups admins ; Notifications get sent to the admins by
default
register 0 ; DONT REGISTER THIS - ITS JUST A TEMPLATE
}
<省略>
|
各項目の意味は次のようになります。
| check_period | 24時間365日監視をする |
| check_interval | 5分間隔で監視を実行する |
| retry_interval | pingに応答しなかった際に1分後にリトライする |
| max_check_attempts | リトライを最大10回繰り返す |
| check_commend | 実際に実行するプラグインを指定する。 |
| notification_period | 通知を行う時間帯を指定する。24×7は常に通知する。 |
| notification_interval | 30分毎に再通知を行う。0を指定すると再通知は行わない。 |
| notification_options | どの状態で通知を行うか指定する。ステータスは以下の通り。 d → down r → recovery u → unreachable w → warning c → critical |
| contact_groups | 通知先を指定する。これは/usr/local/nagios/etc/objects/contacts.cfgで設定する。 |
generic-serviceについてもほぼ同様な意味なのでここでは割愛します。
■通知先の設定
実際に異常を検知した際の通知先を変更しておきます。
# vi /usr/local/nagios/etc/objects/contacts.cfg
<省略>
# Just one contact defined by default - the Nagios admin (that's you)
# This contact definition inherits a lot of default values from the 'generic-contact'
# template which is defined elsewhere.
define contact{
contact_name nagiosadmin ; Short name of user
use generic-contact ; Inherit default values f
rom generic-contact template (defined above)
alias Nagios Admin ; Full name of user
email webmaster@unix-power.net ; <<***** CHANGE T
HIS TO YOUR EMAIL ADDRESS ******
}
<省略>
|
設定を変更した際にはnagiosを再起動して変更を有効にします。
# /etc/rc.d/init.d/nagios restart |
■監視コマンドの設定
実際に監視をする際にはpingのみなんてことはなく、各サーバで動いているサービス各々に対して行うことになり ます。普通に考えてHTTP / HTTPS / SMTP / POP3 / DNSなどはざらに存在しているでしょう。これらを監視するには少しだけ設定ファイルを修正する必要がありますのでそれらを紹介します。
■DNSサービス
DNSサービスのチェックには以下のファイルにそれぞれ内容を追記します。
# vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_dig' command definition
define command{
command_name check_dig
command_line $USER1$/check_dig -H $HOSTADDRESS$ -l $ARG1$
}
# vi /usr/local/nagios/etc/objects/objects.cfg
define service{
use generic-service
host_name dns.xxyy.jp
service_description DNS
check_command check_dig!www.yahoo.co.jp
}
|
上記ではwww.yahoo.co.jpをdigコマンドで定期的にチェックを行うという意味になります。どのURLをチェック対象にするのかは任意に決めることができます。
■HTTPサービス
HTTPは標準でcommands.cfgに記載されていますが、URLを指定するオプションが指定されていませんので、commands.cfgファイルに修正を入れます。下記の$HOSTADDRESSのあとにURLの-uオプションを指定しています。
# vi /usr/local/nagios/etc/objects/commands.cfg
<省略>
# 'check_http' command definition
define command{
command_name check_http
#command_line $USER1$/check_http -I $HOSTADDRESS$ $ARG1$
command_line $USER1$/check_http -I $HOSTADDRESS$ -u $ARG1$
}
<省略>
# vi /usr/local/nagios/etc/objects/objects.cfg
define service{
use generic-service
host_name www.unix-power.net
service_description HTTP
check_command check_http!/
}
|
check_http!の後ろの部分にURLを記載します。トップページであっても省略するのではなく、必ずパラメータとして!/は最低限、指定するようにしてください。
■HTTPSサービス
HTTPは標準でcommands.cfgに記載されていますが、HTTPSは記載がないので手動で追記する必要 があります。
# vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_https' command definition
define command{
command_name check_https
command_line $USER1$/check_http --ssl -I $HOSTADDRESS$ -u $ARG1$
}
# vi /usr/local/nagios/etc/objects/objects.cfg
define service{
use generic-service
host_name ssl.xxyy.jp
service_description HTTPS
check_command check_https!/ssl/
}
|
check_https!の後ろの部分にURLを記載します。トップページであっても省略せずに必ずパラメーターとして!/は最低限、指定してください。
■NTPサービス
# vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_ntp' command definition
define command{
command_name check_ntp
command_line $USER1$/check_ntp -H $ARG1$ -w $ARG2$ -c $ARG3$
}
# vi /usr/local/nagios/etc/objects/server.cfg
define service{
use generic-service
host_name ntp.xxyy.jp
service_description NTP
check_command check_ntp!xx.yy.xx.yy!5!10
}
|
xx.yy.xx.yyに実際に同期するNTPサーバのIPアドレスを記載します。!の後の5と10に関して、NTPサーバと5秒ずれればwarning、10秒ずれれば異常とみなす、という意味です。その他メール関連のSMTP / POP3 / IMAPなどはcommands.cfgを触らなくてもそのまま使用できます。他にもnagios pluginは多数用意されておりますが、私自身、ここで上げたプラグインぐらいしか使ったことがありません。
■任意のポート
アプリケーションによっては独自のポートを使って稼働するサービスなども多数ありますが、特定のポートに対してレスポンスがあるかをチェックするcheck_tcpというプラグインも用意されています。例えば以下の場合、ポート3306 ( MySQL ) に対して死活監視を行うコマンドおよびホスト設定となります。
# vi /usr/local/nagios/etc/objects/commands.cfg
# 'check_mysql' command definition
define command{
command_name check_mysql
command_line $USER1$/check_tcp -H $HOSTADDRESS$ -p 3306 -w $ARG1$ -c $ARG2$
}
# vi /usr/local/nagios/etc/objects/server.cfg
define service{
use generic-service
host_name mysql-server
service_description MySQL
check_command check_mysql!5!10
}
|
-pオプションでポート番号を指定します。-wの引数1と-cの引数2はそれぞれ応答速度が引数1以上だとwarning、2以上だとcriticalという意味なります。上記では5ms以上でwarning、10ms以上でcriticalということになります。