LoginSignup
39
36

More than 5 years have passed since last update.

monitで手軽にプロセスの監視を行う

Last updated at Posted at 2014-02-07

今回の記事はUbuntuでの設定を想定しています。
他の環境をお使いの方は適宜読み替えてください。


インストール

$ sudo aptitude install monit

monitの設定

/etc/monit/monitrcにて監視タイミングなどの設定を行います。

監視の実行タイミング

監視を行う間隔を秒単位で設定します。

set daemon 120

ログの出力先

syslogなどにも出力できるそうですが、とりあえず別ファイルに。

set logfile /var/log/monit.log

アラートメールを送信するときに使用するSMTPサーバ

set mailserver localhost

アラートメールのフォーマット

フォーマットには変数も埋め込めます。詳細は公式サイトでご確認ください。

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
 }

アラートメールの送信先

下記のように複数行で設定することでアラートメールの送信先を複数設定できます。

set alert admin@foo.bar
set alert operator@foo.bar
set alert alert@foo.bar not on { INSTANCE }

メールアドレスの後ろにnot on { INSTANCE }をつけると、monitの設定変更などの通知がこなくなります。

監視項目の読み込み

監視項目は別ファイルにしておいた方が管理がしやすいので。

include /etc/monit/conf.d/*

監視項目の設定

/etc/monit/conf.d配下に監視項目を定義したテキストファイルを置いていきます。

Unicornのプロセス監視を行う場合

ただしunicornは/etc/init.d/unicornという名前でinitスクリプトが作成されているものとします。

/etc/monit/conf.d/unicorn
check process unicorn
  with pidfile /path/for/pids/unicorn.pid
  start program = "/bin/sh -l -c '/etc/init.d/unicorn start'"
  stop program = "/bin/sh -l -c '/etc/init.d/unicorn stop'"

  group unicorn

参考にしたサイト

http://mmonit.com/monit/
http://shanon-tech.blogspot.jp/2011/11/15monit.html
http://blog.twiwt.org/e/232155
http://ex-cloud.jp/techblog/?p=28

39
36
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
39
36