10分くらいではじめられる、Monitによるサービス監視の自動化。

Monitとは、プロセス・プログラム・ディレクトリなどを監視するためのシステムマネジメントツールです。

本投稿では、Monitのインストールからサービス監視の方法まで紹介します。

 

目次

以下の順で、Monitについて紹介していきます。

1.Monitのインストール方法

2.monitrcの設定とMonitの起動方法

3.アラート機能の設定方法

4.サービス監視の追加方法

5.グループ機能の使い方

6.Webインターフェイス機能の設定方法

 

作業環境
OS: CentOS 7.1
Monit: Version 5.6

 

 

1.Monitのインストール方法

 

Monitをインストール

Monitのインストールは、Yumで簡単に行えます。以下のコマンドでインストールは終わりです。

# yum install monit

YumのBaseリポジトリでインストールした場合、Monitバージョンは5.6です。

 

 

2.monitrcの設定とMonitの起動方法

 

Monitを設定

Monitの基本的な設定は、「/etc/monitrc」に記述します。

以下のコマンドで、必ずバックアップを取得してから編集するようにしましょう。

# cp -av /etc/monitrc{,.bak}
`/etc/monitrc' -> `/etc/monitrc.bak'

初期状態では、大部分がコメントアウト(#)になっているかと思います。必要項目のみ、コメントアウトを外しましょう。

コメントアウトも全て載せると見難くなるため、以下ではコメントアウト行を全て削除してあります。実際に必要なのは以下の設定項目だけです。

# vim /etc/monitrc
set daemon  60                 # Monitデーモンによる、サービス監視周期

# Monit Webインターフェイスの設定  ※ Monit Webインターフェイスのがないと 「# monit status」コマンドが実行できない。
set httpd port 2812 and        # Monit専用、Webインターフェイスのポートを指定(初期:2812)
    use address localhost      # Monin Webインターフェイスに接続するためのドメイン名またはサーバIPアドレスを指定
    allow localhost            # Monin Webインターフェイスを利用できるドメイン名またはIPアドレスを指定
    allow admin:monit          # Monin Webインターフェイスのログインに必要なユーザ名(admin)とパスワード(monit)
    allow @monit               # Monin Webインターフェイスを利用できるグループを指定
    allow @users readonly      # Monin Webインターフェイスを利用できるグループを指定し、読み取りだけ許される権限にする


include /etc/monit.d/*         # その他設定ファイルを読み込み

 

monitrcの編集を終えたら、構文に間違いなどないか確認する必要があります。

構文チェックは、Monitコマンドで行うことができます。

# monit -t
Control file syntax OK

「syntax OK」と表示されれば、正しく設定できているということです。

 

Monitの起動と自動起動の設定

monitrcの構文チェックでエラーなど、無ければMonistをします。

Monitデーモンは、以下のコマンドを実行することで起動できます。

# systemctl start monit

 

MonitデーモンをOS起動時に自動的に起動するよう、以下のコマンドで設定します。

# systemctl enable monit

 

Monitデーモンの起動と自動起動が有効になっていることを確認します。

# systemctl status monit
monit.service - Pro-active monitoring utility for unix systems
   Loaded: loaded (/usr/lib/systemd/system/monit.service; enabled)
   Active: active (running) since 土 2015-07-04 20:20:40 JST; 1h 36min ago
 Main PID: 1959 (monit)
   CGroup: /system.slice/monit.service
           mq1959 /usr/bin/monit -I

 7月 04 20:20:40 monit systemd[1]: Starting Pro-active monitoring utility for unix systems...
 7月 04 20:20:40 monit systemd[1]: Started Pro-active monitoring utility for unix systems.
 7月 04 20:20:40 monit monit[1959]: monit: generated unique Monit id 1bd7990bb25e140a0c413...id'
 7月 04 20:20:40 monit monit[1959]: Starting monit daemon with http interface at [localhos...12]
 7月 04 20:56:13 monit systemd[1]: Started Pro-active monitoring utility for unix systems.
Hint: Some lines were ellipsized, use -l to show in full.

「Active:」のステータスが active になっていれば起動は成功です。自動起動が有効になっている場合「Loaded:」の行末に enabled と表示されます。

 

 

3.アラート機能の設定方法

 

メールフォーマットを登録

Monitのアラート機能を使うためには、メールフォーマットを設定する必要があります。

アラートメール関連の設定も、「/etc/monitrc」に記述することはできますが本投稿では、monitrcでインクルードしているディレクトリ配下(/etc/monit.d/)に設定ファイルを作成します。

# vim /etc/monit.d/mail-format
# アラートメールの送信用サーバを指定
set mailserver localhost

# アラート通知先のメールアドレスを指定
#「not on {INSTANCE}」で「Monit instance changed」などの通知を無効化
set alert hoge@yahoo.co.jp not on {INSTANCE}

# アラートメールのフォーマット
set mail-format {
        from: monit@$HOST
        subject: monit alert --  $EVENT $SERVICE
        message: $EVENT Service $SERVICE
                Date:        $DATE
                Action:      $ACTION
                Host:        $HOST
                Description: $DESCRIPTION

                Your faithful employee,
                Monit
}

※アラートメールを送信するためには、サーバ側でPostfixが動作している必要があります。

 

 

4.サービス監視の追加方法

 

サービス監視の設定①

Monitに監視させるサービスを登録します。本投稿では、「/etc/monit.d/」配下にサービス毎の設定ファイルを作成する方法で紹介します。

以下は、httpdのPIDを監視するシンプルな設定で。

# vim /etc/monit.d/httpd
# サービス監視でhttpdのPIDファイルがない場合、httpdを再起動する
check process httpd with pidfile /var/run/httpd/httpd.pid

        # httpdの起動方法を定義する
        start program = "/bin/systemctl start httpd"

        # httpdの停止方法を定義する
        stop program  = "/bin/systemctl stop httpd"

        #サービス監視でlocalhostの80番ポートにhttpdプロトコルの通信ができなかった場合、httpdを再起動する
        if failed
          host localhost
          port 80
          protocol http
        then restart

※if文の条件で「host」には、ドメイン名やグローバルIPアドレスが設定可能です。

 

Monitでhttpdの監視が実際に実施されているのか、以下のコマンドで確認します。

# monit status
The Monit daemon 5.6 uptime: 0m

Process 'httpd'
  status                            Running
  monitoring status                 Monitored
  pid                               2835
  parent pid                        1
  uptime                            1h 57m
  children                          5
  memory kilobytes                  4912
  memory kilobytes total            23152
  memory percent                    0.4%
  memory percent total              2.2%
  cpu percent                       0.0%
  cpu percent total                 0.0%
  port response time                0.002s to localhost:80 [HTTP via TCP]
  data collected                    Sat, 04 Jul 2015 21:36:48


System 'monit'
  status                            Running
  monitoring status                 Monitored
  load average                      [0.00] [0.01] [0.02]
  cpu                               0.0%us 0.0%sy 0.0%wa
  memory usage                      128664 kB [12.6%]
  swap usage                        0 kB [0.0%]
  data collected                    Sat, 04 Jul 2015 05:36:48

「status」が Running であれば、起動確認は終わりです。

 

サービス監視の設定②

①にCPU,メモリ,ロードアベレージなどの監視項目を加え、アラート機能も使います。

# vim /etc/monit.d/httpd
check process httpd with pidfile /var/run/httpd/httpd.pid
        start program = "/bin/systemctl start httpd"
        stop program  = "/bin/systemctl stop httpd"
        if failed host localhost port 80 protocol http then restart

        # サービス監視で5分平均のロードアベレージが5を超えた状態が5回続いた場合、アラートメールを送信する
        if loadavg(5min) greater than 5 for 5 cycles then alert

        # サービス監視で5分平均のロードアベレージが10を超えた状態が3回続いた場合、httpdを再起動する
        if loadavg(5min) greater than 10 for 3 cycles then restart

        # サービス監視でhttpdのプロセス数が128を超えた状態が2回続いた場合、「/bin/systemctl reload httpd」コマンドを実行する
        if children > 128 for 2 cycles then exec "/bin/systemctl reload httpd"

        # サービス監視でCPU使用率が40%超えの状態が2回続いた場合、アラートメールを送信する
        if cpu > 40% for 2 cycles then alert

        # サービス監視でCPU使用率が60%超えの状態が4回続いた場合、httpdを再起動する
        if cpu > 60% for 4 cycles then restart

        # サービス監視でhttpdプロセスがメモリを200MB超え使用した状態が8回続いた場合、httpdを再起動する
        if totalmem > 200.0 MB for 8 cycles then restart

各追加項目の閾値など、皆さんのサーバ環境に合った値に調整してみてください。

 

Monitの設定ファイルを編集した後は、設定をMonitに反映させるためリロードもしくわリスタートが必要です。

# monit reload

または

# systemctl restart monit

 

 

5.グループ機能の使い方

 

グループを設定

Monitでは、監視サービスをグループで管理することができます。

以下の例では、httpdとMySQLをwwwグループに所属させます。グループの設定方法は、各設定ファイルの行末に「group グループ名」を追加します。

# vim /etc/monit.d/httpd
check process httpd with pidfile /var/run/httpd/httpd.pid
        start program = "/bin/systemctl start httpd"
        stop program  = "/bin/systemctl stop httpd"
        group www

 

# vim /etc/monit.d/mariadb
check process mariadb with pidfile /var/run/mariadb/mariadb.pid
        start program = "/bin/systemctl start mariadb"
        stop program  = "/bin/systemctl stop mariadb"
        group www

 

「group グループ名」を追加し終えたら、Monitをリロードします。

# monit reload

 

グループ単位の命令

グループ化により、サービスのリスタート、リロード、ストップ、スタート、モニターやアンモニターをグループ単位で命令できるようになる。

以下は、wwwグループのhttpdとmariadbをMonitの監視対象から除外(Unmonitor)する例です。

# monit -g www unmonitor

 

 

6.Webインターフェイス機能の設定方法

 

MonitのWebインターフェイス

手順2の monitrc で既に以下のような、設定になっていると思います。

set httpd port 2812 and        # Monit専用、Webインターフェイスのポートを指定(初期:2812)
    use address localhost      # Monin Webインターフェイスに接続するためのドメイン名またはサーバIPアドレスを指定
    allow localhost            # Monin Webインターフェイスを利用できるドメイン名またはIPアドレスを指定
    allow admin:monit          # Monin Webインターフェイスのログインに必要なユーザ名(admin)とパスワード(monit)
    allow @monit               # Monin Webインターフェイスを利用できるグループを指定
    allow @users readonly      # Monin Webインターフェイスを利用できるグループを指定し、読み取りだけ許される権限にする

 

Monitのステータスを確認する方法には、「monit status」とコマンドを実行する以外にも、Webインターフェイスを使う方法があります。

以下のように「use address localhost」を削除して、Webインターフェイスに接続するIPアドレスを記述するだけでブラウザから接続できるようになります。

set httpd port 2812 and
    allow localhost
    allow <接続を許可するIPアドレス>
    allow hoge:hogehoge123

「use address」を削除する理由は、独自ドメインやIPアドレスを複数設定することができないため、もし「use address」に独自ドメインやIPアドレスを設定した場合「monit status」コマンドが実行できなくなるからです。

「allow localhost」は、「monit status」コマンドの実行になので必ず設定してください。

 

ブラウザからMonit Webインターフェイスにアクセスするには、以下のようにIPアドレスまたは、独自ドメインの後にポート番号(2812)まで指定してください。

http://アドレスまたは独自ドメイン名>:<ポート番号>

WebUIにアクセスしてユーザ名とパスワードを入力すると以下のような画面が表示されます。

f:id:sabakan1204:20150710072913j:plain

 

 

以上で、Monitについての紹介は終わりです。