1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Nagiosサーバと接続されていないサーバをNrpeを用いて間接的に監視する方法

Posted at

今回やりたいことと前提条件

今回は監視サーバと直接繋がっていないサーバへのリモート監視設定を行う。
既にNagiosで監視サーバと接続されたリモートホストの監視環境構築の経験と知識があることを前提とする。
webサイトを構築するwebサーバとそれに連なるデータベースサーバを監視していく。

つまり、ネットワーク図はこんな感じ。

インターネット
   ↓
  監視サーバー
   ↓
   web
   ↓
   DB

このまま監視サーバーから普通にNrpeを走らせてもDBまで届かない。
じゃあ、どうするの? となると、WEBにあるnrpeを仲介させて、DB内の監視コマンドを叩かせる必要があるが、
これがまた凄まじくややこしい。

監視サーバのIPはグローバルをわかりやすく「123.456.789.X」ローカルを「192.168.XXX.X」
WEBサーバのホスト名を「WEB」IPはグローバルを「123.456.ZZZ.Z」ローカルを「192.168.ZZZ.Z」
データベースサーバのホスト名を「DB」IPは外部ネットワークに接続されていないので、ローカルのみで「192.168.YYY.Y」
以上のように定義。

前提条件として環境は、AmazonEC2 Linux。
監視ソフトはNagiosを yum install で構築済み。
ファイヤウォールも設定済み。

監視サービスはひとまずNagiosのデフォルトで用意されている見るといった具合で。必要に応じて個別に対応してください。
今回はひとまず、監視サーバーからwebサーバーを中継してDBサーバー内のNrpeを叩けるようにするまでがゴールとする。

Webサーバへの監視環境構築

Webサーバへの監視環境を構築していく。
まずは監視サーバのNagios内にまずはWebの監視ホストを設定。

# mkdir /etc/nagios/servers/test/
# vim WEB.cfg

=========
define host{
        use                     generic-service
        host_name               WEB
        alias                   WEB
        address                 
        }



# SERVICE DEFINITIONS
define service{
        use                             generic-service
        host_name                       WEB
        service_description             SSH
        check_command                   check_ssh
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             HTTP
        check_command                   check_http
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             DISK
        check_command                   check_nrpe!check_disk
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             LOAD
        check_command                   check_nrpe!check_load
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             MEM
        check_command                   check_nrpe!check_mem
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             SWAP
        check_command                   check_nrpe!check_swap
        }

define service{
        use                             generic-service
        host_name                       WEB
        service_description             NTP
        check_command                   check_nrpe!check_ntp
        }

=========

外部監視が可能なコマンドはそのまま、内部監視が必要なコマンドは Nrpe を仲介させる。
次にDBサーバへの仲介役を果たすWebサーバにNrpeのインストールと設定を行う。

# yum install nrpe nagios-plugins-all nagios-plugin-nrpe
# chkconfig nrpe on
# chkconfig --list nrpe
nrpe            0:off   1:off   2:on    3:on    4:on    5:on    6:off

次にWEBサーバ内の nrpe.cfg ファイルを編集し、nrpe で仲介させるコマンドを定義する。

# vim nrpe.cfg
-----------------
# for WEB
command[check_mem]=/usr/lib64/nagios/plugins/check_mem.pl -u -w 80 -c 90
command[check_load]=/usr/lib64/nagios/plugins/check_load -w 5,5,5 -c 10,10,10
command[check_swap]=/usr/lib64/nagios/plugins/check_swap -w 70% -c 50%
command[check_disk]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10%
command[check_ntp]=/usr/lib64/nagios/plugins/check_ntp_peer -H hogehoge

Webサーバの nrpe を起動

# service nrpe start

監視サーバに戻って、ひとまずcfgファイルの記述に問題がないか確認。

# nagios -v /etc/nagios/nagios.cfg

ここまでは通常のリモートホストに対する監視環境構築手順となる。
問題は次から。

DBサーバへの監視設定

監視サーバでの作業

早速、監視サーバにDB用cfgファイルを設定していきたいが、その前にやることがある。
監視サーバのcommand.cfg に以下のコマンドを新しく定義する。

# vim command.cfg

# DB監視用コマンド
define command{
        command_name    check_db
        command_line    $USER1$/check_nrpe -H 123.456.ZZZ.Z -c $ARG1$ -t 60
        }

このコマンドは、DBの監視ホストで用いられるものであり、監視サーバ内の checkj_nrpe が Webサーバにアクセスし -c でWwbサーバ内の nrpe.cfg 内で指定したDB監視用コマンドをDBへ向けて叩くという仕組みだ。
DBのcfgファイルでは基本的にこのコマンドを叩かせる。
監視サーバに DB監視ホストのcfgファイルを作成する。

# cd /etc/nagios/servers/test
# vim DB.cfg

# HOST DEFINITION
define host{
        use                     generic-server
        host_name               DB
        alias                   DB
        address                 192.168.YYY.Y
        parents                 WEB   #ペアレンツ設定
        check_command           check-host-alive-dummy #ホストのダミー判定
        }

# SERVICE DEFINITIONS
define service{
        use                             generic-service
        host_name                       DB
        service_description             SSH
        check_command                   check_db!check_db_ssh
        }

define service{
        use                             generic-service
        host_name                       DB
        service_description             DISK
        check_command                   check_db!check_db_disk
        }

define service{
        use                             generic-service
        host_name                       DB
        service_description             LOAD
        check_command                   check_db!check_db_load
        }

define service{
        use                             generic-service
        host_name                       DB
        service_description             MEM
        check_command                   check_db!check_db_mem
        }

define service{
        use                             generic-service
        host_name                       DB
        service_description             SWAP
        check_command                   check_db!check_db_swap
        }


define service{
        use                             generic-service
        host_name                       DB
        service_description             NTP
        check_command                   check_db!check_db_ntp
        }

ここで check_db の後に定義されている check_db_hogehoge はWebサーバ内の nrpe.cfg で定義されている DBサーバへの監視コマンドの役割を果たす。
次にそのWEBサーバからDBサーバへの監視コマンドを定義する。

Webサーバでの作業

再度、Webサーバ内での作業に戻る。

# vim nrpe.cfg
---------------
# for DB
command[check_db_mem]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y -c check_mem
command[check_db_swap]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y -c check_swap
command[check_db_disk]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y -c check_disk
command[check_db_load]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y -c check_load
command[check_db_ntp]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y -c check_ntp
command[check_db_ssh]=/usr/lib64/nagios/plugins/check_ssh -H 192.168.YYY.Y -c check_ssh

DBサーバは監視ホストと直接接続されていないため、外部監視が可能であるコマンドも Nrpe を仲介させなければならない。

DBサーバでの作業

ここから先はDBサーバでの作業となる。
DBサーバにログインし、nrpeのインストールと設定

# yum install nrpe nagios-plugins-all nagios-plugins-nrpe
# chkconfig nrpe on
# chkconfig --list nrpe
nrpe            0:off   1:off   2:on    3:on    4:on    5:on    6:off

DBサーバの nrpe.cfg の編集

command[check_mem]=/usr/lib64/nagios/plugins/check_mem.pl -u -w 80 -c 90
command[check_load]=/usr/lib64/nagios/plugins/check_load -w 5,5,5 -c 10,10,10
command[check_swap]=/usr/lib64/nagios/plugins/check_swap -w 70% -c 50%
command[check_disk]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10%
command[check_ntp]=/usr/lib64/nagios/plugins/check_ntp_peer -H hogehoge
command[check_ssh]=/usr/lib64/nagios/plugins/check_ssh

ここまで監視サーバからWEBサーバを仲介させ、DBサーバまで届かせる経緯はこんな具合だ。DISK監視を例にすると

監視サーバで check_db!check_db_disk WEBサーバへ向けて叩く
↓
Webサーバの nrpe.cfg の中の
command[check_db_disk]=/usr/lib64/nagios/plugins/check_nrpe -H 192.168.YYY.Y(DBサーバのIP) -c check_disk
がそれを中継して、check_nrpe から DBサーバの check_disk を叩く。
↓
DBサーバの nrpe.cfg の中の
command[check_disk]=/usr/lib64/nagios/plugins/check_disk -w 20% -c 10%
がサーバ内のディスク容量をチェック。結果をWebサーバを仲介して監視サーバへと送る

といった経路だ。

最後に各サーバの nrpe の起動と nagios の構文チェック、そして再起動を忘れずに。
DBサーバにて

# service nrpe start

Webサーバにて

# service nrpe restart

監視サーバにてcfgファイルの構文をチェック

# nagios -v /etc/nagios/nagios.cfg

つつがない状態になったら満を持してnagios を再起動

# service nagios restart

Nagiosのブラウザ画面で疎通が取れていれば成功。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?