LoginSignup
3
1

More than 5 years have passed since last update.

Supervisorで常駐化している任意のプロセスをMackerelでモニターする

Last updated at Posted at 2016-10-12

はじめに

supervisorを運用していて、まれにプロセスが落ちているときはないでしょうか?
Mackerelエージェントに、監視結果を出力するコマンドを登録することで、任意の常駐化プロセスをモニターしてみます。

環境

尚、動作環境はEC2上に構築してみました。

  • OS: Amazon Linux 2016.03

supervisord自体のモニターは公式プラグインでする

公式チェックプラグインを使うとsupervisord自体の死活監視は簡単に行うことができます。

プラグインのインストール

$ yum install mackerel-check-plugins

設定

Mackerelの設定ファイル(/etc/mackerel-agent/mackerel-agent.conf)に、下記のように設定を追加します。

/etc/mackerel-agent/mackerel-agent.conf
[plugin.checks.supervisord]
command = "check-procs -p supervisord"

参考URL

supervisorctl statusコマンドを使ってモニターする

supervisorctl status {program name}コマンドを使って、任意のプロセスの状態をモニターします。

スクリプトの作成

pythonで簡単なスクリプトを作成してみます。
やっていることは単純です。

  • supervisorctl statusで出力される文字を分解
  • 配列の2番目に状態を表す文字が入っているので、その状態によって、exitコードを切り分ける
  • exitコードは、それぞれ、0: OK, 1: WARNING, 2: CRITICAL, 左記以外: UNKNOWN の意味になる
  • 標準出力に情報を出力しておくと、Mackerelに送信されるので便利
check_supervisor_example_program.py
import sys
import commands

def main():
    output = commands.getoutput('supervisorctl status example_program')
    print(output)
    parts = output.split()
    print(parts[0])
    print(parts[1])
    if (parts[0] == 'example_program'):
        if (parts[1] in ['RUNNING']):
            sys.exit(0)
        if (parts[1] in ['STARTING','BACKOFF']):
            sys.exit(1)
        if (parts[1] in ['STOPPED','STOPPING','EXITED','FATAL']):
            sys.exit(2)

    sys.exit(9)


if __name__ == '__main__':
    main()

実行結果

$ sudo python ./check_supervisor_example_program.py 
example_program                  RUNNING   pid 25436, uptime 0:35:27
example_program
RUNNING

設定

check-procsのときと同様に、設定ファイルに下記の設定を追加する。

/etc/mackerel-agent/mackerel-agent.conf
[plugin.checks.supervisord_example_program]
command = "python /path/to/check_supervisor_example_program.py"

参考URL

モニター結果

example_programがSTOPPEDの場合

supervisord_monitor_program.png

supervisord_alert_edited.png

supervisordが停止している場合

supervisord_monitor_service.png

supervisord_alert_service_edited.png

おわりに

あまり需要がないかもしれませんが、supervisorに限らず、監視したい項目を簡単なスクリプトでモニターできますので、トライしてみると良いかと。

3
1
1

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
3
1