Nagios によるサーバ監視
Nagios は perl ベースのサーバ監視ツールです。デフォルトでは SNMP を使用しないため、サーバやデーモンの死活確認が主な利用目的となります。
Nagios 本体のインストール・日本語化
Nagios は SourceForge より tar ボールをダウンロードします。
※ rpmforge から YUM でインストールすることもできますが、その場合 コンフィグパスは /etc/nagios になります。
また、Nagios-JPよりバージョンの一致する日本語化パッチをダウンロードします。このパッチを適用することにより、web インターフェースが日本語(utf-8)で表示されます
1 2 3 4 5 6 7 8 9 10 11 12 |
<span class="comment"># nagios 本体をダウンロード</span> $ wget http://jaist.dl.sourceforge.net/project/nagios/nagios-3.x/nagios-3.1.0/nagios-3.1.0.tar.gz <span class="comment"># tar.gz を展開</span> $ tar -zxvf nagios-3.1.0.tar.gz $ cd nagios-3.1.0/ <span class="comment"># 日本語化パッチをダウンロード</span> $ wget http://jaist.dl.sourceforge.jp/nagios-jp/38828/nagios-3.1.0-ja-utf8.patch.gz <span class="comment"># パッチを適用</span> $ gzip -dc nagios-3.1.0-ja-utf8.patch.gz| patch -p0 |
Nagios のコンパイルとインストール
先に Nagios の起動ユーザを追加しておきます。
1 2 |
<span class="comment"># nagios ユーザを追加</span> $ useradd -d /usr/local/nagios -m nagios |
1 2 3 4 5 6 7 |
<span class="comment"># コンパイル&インストール</span> $ ./configure --with-httpd-conf=/usr/local/apache2/conf/extra $ make all $ make fullinstall <span class="comment"># 設定ファイル サンプルのインストール</span> $ make install-config |
nagios プラグイン インストール
やはり SourceForge より nagios プラグイン をダウンロードします。
1 2 3 4 5 6 7 8 |
$ wget wget http://jaist.dl.sourceforge.net/sourceforge/nagiosplug/nagios-plugins-1.4.13.tar.gz <span class="comment"># コンパイル&インストール</span> $ tar -zxvf nagios-plugins-1.4.13.tar.gz $ cd nagios-plugins-1.4.13 $ ./configure $ make && make install |
Nagios 設定
Nagios の全体的な設定を行うには /usr/local/nagios/etc/nagios.cfg を編集します。
1 2 3 4 5 6 7 |
## /usr/local/nagios/etc/nagios.cfg ## servers 行をアンコメント cfg_dir=/usr/local/nagios/etc/servers #cfg_dir=/usr/local/nagios/etc/printers #cfg_dir=/usr/local/nagios/etc/switches #cfg_dir=/usr/local/nagios/etc/routers |
1 2 |
$ mkdir /usr/local/nagios/etc/servers $ chown nagios. /usr/local/nagios/etc/servers |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## /usr/local/nagios/etc/cgi.cfg ## デフォルトユーザ行をアンコメント default_user_name=guest ## authorized_for ... = nagiosadmin の行を全て「*」に変更 ## <b>スペース・タブ等を入れるとうまく動きません</b> authorized_for_system_information=* authorized_for_configuration_information=* authorized_for_system_commands=* authorized_for_all_services=* authorized_for_all_hosts=* authorized_for_all_service_commands=* authorized_for_all_host_commands=* |
1 2 3 4 5 6 |
## apache を nagios グループに登録する $ usermod -G nagios apache ## グループユーザ(apache)が cgi を実行できるよう ## アクセス権を変更する $ chmod -R 755 /usr/local/nagios |
サーバ設定ファイルの作成
監視ホストの定義
監視対象とするサーバ/サービスの設定を行います。etc/servers/ 以下に「*.cfg」という名前で作成します。
ex.)nodes.cfg
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
######## # 監視ホストの設定 define host{ use linux-server # ホストが linux の場合 host_name www-server # ホスト名を定義 alias www.cro-pel.com # エイリアス名 address 192.168.10.2 # ホストのIPアドレス } # 監視サービスの設定 define service{ use generic-service host_name <b>www-server</b> service_description SSH # サービス名を定義 check_command check_ssh # ssh 接続をチェック } define service{ use generic-service host_name <b>www-server</b> service_description PING # サービス名を定義 check_command check_ping!100.0,20%!500.0,60% # ping 応答をチェック } define service{ use generic-service host_name <b>www-server</b> service_description VNC # サービス名を定義 check_command check_tcp!5901!2!5 # VNC で利用する 5901/tcp をチェック } |
ホストグループの定義
複数の監視ホストがある場合、例えば東京のホスト群、大阪のホスト群というようにグルーピングして管理することができます。ホストグループの設定も、etc/servers/ 以下に任意の *.cfg ファイルを作成します。
ex.)nodes.cfg
1 2 3 4 5 |
define hostgroup{ hostgroup_name Tokyo alias tokyo_group members akihabara,shinjuku,ikebukuro, } |
設定ファイルについて
nagios の設定ファイルは nagios.cfg にすべてまとめることも、ホストの設定ごとに host-01.cfg、host-02.cfg・・・というようにバラバラに作成することもできます。上記のホスト設定とグループ設定も一つのファイルにまとめてしまうことも可能です。
Nagios の起動
すべての設定が完了したら、Nagios を起動します。nagios の起動は rc スクリプトで行います。
1 |
$ /etc/rc.d/init.d/nagios start |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
$ /usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg Nagios 3.1.0 Copyright (c) 1999-2009 Ethan Galstad (http://www.nagios.org) Last Modified: 01-25-2009 License: GPL Reading configuration data... Read main config file okay... Processing object config file '/usr/local/nagios/etc/objects/commands.cfg'... Processing object config file '/usr/local/nagios/etc/objects/contacts.cfg'... Processing object config . . . file '/usr/local/nagios/etc/servers/nodes.cfg'... Error: Could not find any host matching '192.168.10.2' (config file '/usr/local/nagios/etc/servers/nodesgroup.cfg', starting on line 1) Error: Could not expand members specified in hostgroup (config file '/usr/local/nagios/etc/servers/nodesgroup.cfg', starting on line 1) Error processing object config files! ***> One or more problems was encountered while processing the config files... Check your configuration file(s) to ensure that they contain valid directives and data defintions. If you are upgrading from a previous version of Nagios, you should be aware that some variables/definitions may have been removed or modified in this version. Make sure to read the HTML documentation regarding the config files, as well as the 'Whats New' section to find out what has changed. |
nagios モジュール の利用
/usr/local/nagios/libexec 以下に格納されているモジュールは、コマンドラインから直接実行することができます。
例えば 監視対象が ssh ログイン可能かどうか確認するには、check_ssh を使用します。
1 2 3 4 5 |
$ cd /usr/local/nagion/libexec/ $ ./check_ssh -p 22 -t 5 -H 192.168.1.5 SSH OK $ |
1 2 3 4 5 6 |
$ cd /usr/local/nagion/libexec/ $ ./check_tcp -p 22 -t 5 -H 192.168.1.5 SSH OK - OpenSSH_4.3 (protocol 2.0) $ ./check_tcp -p 22 -t 5 -H 192.168.1.5 TCP OK - 0.024 second response time on port 5124|time=0.024371s;;;0.000000;5.000000 |