akishin999の日記

調べた事などを書いて行きます。

Monit で pidfile が存在しないプロセスを監視する

Monit で pid ファイルが存在しないプロセスを監視する方法を調べたのでメモ。

Monit でのプロセス監視の例として、以下のように pidfile を使用してプロセスID を指定するものをよく見かけるかと思います。

check process sshd with pidfile "/var/run/sshd.pid"

しかし、当然ですが pid ファイルがそもそも存在しないプロセスの場合にはこの方法は使えません。

こうしたプロセスを監視するために、 Monit 5.2 から matching という機能が追加されたようです。

Added support for monitoring processes without pidfile using pattern
matching. You can use POSIX regular expressions or string matching
process name with arguments as provided by the 'ps' utility. If the
pattern matches multiple processes, the first match is used.
Example:
check process debian
matching "/usr/lib/vmware/bin/vmware-vmx .*deb.vmx"

http://lists.gnu.org/archive/html/monit-general/2010-09/msg00081.html

これはなかなか便利そうです。
早速試してみるため、まずは Monit をインストールします。

CentOS 6 の場合、EPEL に用意されている Monit は若干古いので、今回は Repoforge のものを使用します。

Index of /monit
http://pkgs.repoforge.org/monit/

上記から最新バージョンの RPM を選び、yum でインストール。

# yum install -y http://pkgs.repoforge.org/monit/monit-5.5-1.el6.rf.x86_64.rpm

インストールしたら初期設定をします。

# vi /etc/monit.conf

以下のような感じで最低限の設定を行いました。

# 監視間隔
set daemon 60
# ログファイル
set logfile /var/log/monit
# SMTP サーバ
set mailserver localhost
# アラート送信元アドレス
set mail-format { from: monit@example.com }
# アラート送信先アドレス
set alert akishin@example.com

これで準備は OK。

ここでは自宅サーバ環境で、時々知らない間に落ちてしまっていて困っていた DiCE の監視を行う事にしました。

DiCE 監視設定用のファイルを作成します。

# vi /etc/monit.d/diced

以下のような設定を追加しました。

check process diced matching "diced"
  start program = "/usr/local/DiCE/diced -d -l"
  stop  program = "/usr/bin/pkill diced"
  if 5 restarts within 5 cycles then unmonitor

ファイルを保存したら monit を起動します。

# service monit start

正しくプロセスが監視できているかを確かめるため、試しに diced のプロセスを kill してみます。

# pkill diced
# ps -ef | grep [d]iced

しばらくすると diced が見つからない旨のアラートメールが送信されてきたので、ログを確認してみます。

# tail -f /var/log/monit
[JST Aug  1 23:24:11] error    : State file '/var/monit/state': Unable to read magic
[JST Aug  1 23:24:11] info     : 'ceylon' Monit started
[JST Aug  1 23:27:22] info     : monit daemon with pid [17021] killed
[JST Aug  1 23:27:22] info     : 'ceylon' Monit stopped
[JST Aug  1 23:27:22] info     : Starting monit daemon
[JST Aug  1 23:27:22] info     : 'ceylon' Monit started
[JST Aug  1 23:28:22] error    : 'diced' process is not running
[JST Aug  1 23:28:22] info     : 'diced' trying to restart
[JST Aug  1 23:28:22] info     : 'diced' start: /usr/local/DiCE/diced
[JST Aug  1 23:29:22] info     : 'diced' process is running with pid 17116

diced が落ちているのを検知した後、monit がプロセスを起動してくれていますね。
ps で確認してみると確かにプロセスが起動していました。

# ps -ef | grep [d]iced
root     17116     1  2 23:28 ?        00:00:05 /usr/local/DiCE/diced -d -l

しばらくすると今度はプロセスの存在を確認した旨のアラートメールが来ているはずです。

取りあえずこれで安心して放っておける・・・かな?
実際に問題ないかしばらく気を付けて見ていたいと思います。


ちなみに matching には正規表現のパターンを指定できるのですが、パターンが合っているかどうか(本当に目的の pid を取得できているかどうか)を確認するためのコマンドが 5.2.4 から追加されています。

Added the "procmatch" CLI command which allows for easy testing
of pattern for process match check. The command takes regular
expression as an argument and displays all running processes
matching the pattern. Example usage:
$ monit procmatch "iChatAgent"

http://lists.gnu.org/archive/html/monit-announce/2011-02/msg00000.html

こんな感じで使います。

# monit procmatch "diced"
List of processes matching pattern "diced":
------------------------------------------
        /usr/local/DiCE/diced -d -l
------------------------------------------
Total matches: 1

上手く検出できていますね。
絞り込むのが難しいプロセスの場合などに重宝しそうです。

ちなみに、最初 EPEL の RPM を入れていたため、monit procmatch が存在しないと言われたりして悩んでいました。
ちゃんと使いたい機能が追加されたバージョンを確認するようにしないと駄目ですね・・・反省。

それはそれとして、Monit は地味に結構便利な機能が追加されたりしているので、出来るだけ最新版を入れるのが良さそうです。