LoginSignup
6
8

More than 3 years have passed since last update.

Amazon Linux 2 に monit を導入してプロセスを監視する

Last updated at Posted at 2019-06-05

Amazon Linuxサーバでプロセス監視するのにmonitを導入する。
参考: Amazon Linuxでmonitを使ってカジュアルにプロセス監視する!
↑ この記事が古かったので、参考にしつつ一部書き直しました。

準備

バージョンを確認する。

$ cat /etc/system-release
Amazon Linux release 2 (2017.12) LTS Release Candidate

monitをインストール

公式リポジトリにmonitパッケージが用意されてなかったので、EPEL rpmパッケージをインストールして有効にする。
参考: CentOS、RHEL、または Amazon Linux が実行されている Amazon EC2 インスタンスに対して EPEL リポジトリを有効にする方法を教えてください。

sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

monitをインストールする。

$ sudo yum install -y monit
$ monit -V
This is Monit version 5.25.1

※2019/6/3現在

設定ファイルのデフォルトを確認

/etc/monit.conf

コメントを無視して、初期設定の書かれている部分だけ表示する。

/etc/monit.conf
$ sudo cat /etc/monitrc | grep -v -e '^\s*#' -e '^\s*$'
set daemon  30              # check services at 30 seconds intervals
set log syslog
set httpd port 2812 and
    use address localhost  # only accept connection from localhost
    allow localhost        # allow localhost to connect to the server and
    allow admin:monit      # require user 'admin' with password 'monit'
include /etc/monit.d/*

設定ファイルを見てわかること。

/etc/monit.d/logging

デフォルトで/etc/monit.d/配下に、loggingの設定ファイルが置かれている。

/etc/monit.d/logging
$ cat logging
# log to monit.log
set logfile /var/log/monit.log

設定ファイルを見てわかること。

  • /var/log/monit.log という名前のログファイルを出力する

監視設定

nginxを例に監視設定をする。

設定ファイルの作成

/etc/monit.d/ 配下に適当なファイル名で設定ファイルを作成する。

/etc/monit.d/nginx.conf
check process nginx with pidfile /var/run/nginx.pid
start program = "/etc/init.d/nginx start" with timeout 60 seconds
stop program  = "/etc/init.d/nginx stop"
if failed port 80 protocol http then restart
if 5 restarts within 5 cycles then unmonitor

解説

監視対象プロセスのpidファイルを指定
check process nginx with pidfile /var/run/nginx.pid
# 以下のように記載すれば、プロセス名でも監視可能
# check process nginx with matching "nginx"
プロセスの起動/終了方法を指定
start program = "/etc/init.d/nginx start" with timeout 60 seconds
stop program  = "/etc/init.d/nginx stop"
80番ポート、httpが落ちていたら再起動する
if failed port 80 protocol http then restart
再起動を試みる回数と、タイムアウトの時間を指定
if 5 restarts within 5 cycles then unmonitor
# 何らかの不具合があって再起動を繰り返す場合にこの設定は有効
# 上の設定では5回の監視中に、5回再起動を実施した場合に監視を停止する

検証

monitを起動する。

$ sudo service monit start
$ sudo systemctl enable monit # 自動起動ON

nginxを止める。

sudo service nginx stop
sudo service nginx status
  Active: inactive (dead) since Wed 2019-06-05 12:09:12 JST; 3s ago

30秒待つとmonitのログに下記が追加される。

/var/log/monit.log
[JST Jun  5 12:04:00] error    : 'nginx' process is not running
[JST Jun  5 12:04:00] info     : 'nginx' trying to restart
[JST Jun  5 12:04:00] info     : 'nginx' start: '/bin/systemctl start nginx.service'

nginxのステータスを確認。

$ sudo service nginx status
   Active: active (running) since Wed 2019-06-05 12:04:00 JST; 30s ago

動いた!

定期的に監視を復活させる

監視設定で下記のような記述を書くと、監視が外れる(unmonitorになる)場合がある。

if 5 restarts within 5 cycles then unmonitor

そのため、cronで定期的にmonit監視を復活させるコマンドを実行する。ここでは「毎時 0分 1時間に1回」の頻度にした。

/var/spool/cron/root
0 * * * * /usr/bin/monit monitor all

その他 参考

6
8
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
6
8