
Plesk搭載のVPSサーバはWatchdogという監視ツールがインストールされているので、Watchdogを有効にすればプロセスの監視ができるのですが、Plesk搭載されてないサーバについてはご自身で監視ツール
のインストールをしないとプロセスの監視ができません。そこで今回はmonitという監視ツールの
設定についてご紹介します。monitをインストールしておけばプロセスの監視が出来て
自動的にサービスを再起動してくれるのでとても便利です。
夜中や旅行中でも安心して居られるので、便利なツールだと言えると思います。
monitをダウンロード
下記のページから最新版のmonitをダウンロードします。
http://mmonit.com/monit/dist/
私の場合は以下のディレクトリに移動してファイルをダウンロードします。
ご自身の使いやすいディレクトリにファイルをダウンロードしてください。
# cd /usr/local/src # pwd # wget http://mmonit.com/monit/dist/monit-5.5.tar.gz
monit-5.5.tar.gzファイルを取得できました。
monitをインストール
次はmonitのインストールを行います。
# tar -zxvf monit-5.5.tar.gz # cd monit-5.5 # pwd # ./configure # make # make install
/usr/local/bin/ ディレクトリに monitがインストールされます。
インストールが終わったら起動ファイルの配置を行います。
# cp -i /usr/local/src/monit-5.5/contrib/rc.monit /etc/init.d/monit # chmod a+x /etc/init.d/monit
monitを編集してインストールパースを変更します。
# vi /etc/init.d/monit MONIT=/usr/bin/monit
を
MONIT=/usr/local/bin/monit
に変更します。
初期起動時にmonitが自動起動するように設定します。
# /sbin/chkconfig monit on # /sbin/chkconfig --list monit
結果が以下のようになっていれば問題ありません。
monit 0:off 1:off 2:on 3:on 4:on 5:on 6:off
monitの動作を規定します。
# cp /usr/local/src/monit-5.5/monitrc /etc/monitrc
monitrcの編集
# vi /etc/monitrc # monit の監視周期 # -> 60 秒 set daemon 60 # monit のログファイル # -> /var/log/ 以下に出力 set logfile /var/log/monit.log # アラートメールの送信元 # -> とりあえず localhost set mailserver localhost # イベントキューの保持数 # -> /var/monit 以下にイベントキューを 100 件まで保持 set eventqueue basedir /var/monit slots 100 # アラートメールの内容 # -> 以下の内容を元に送信される set mail-format { From: monit@$HOST Subject: $HOST monit alert -- $EVENT $SERVICE message:$EVENT Service $SERVICE Date: $DATE Action: $ACTION Host: $HOST Description: $DESCRIPTION monit detected alert. please check message or log. } # アラートメールの送信先 # -> 必須、ちなみに not on {INSTANCE} を付けると、Monit instance changed 等の monit 自身の通知がこなくなる。 set alert test@test.flxsrv.biz not on {INSTANCE} # ウェブステータスチェック # -> 自分の環境に合わせる set httpd port 2812 and use address test.flxsrv.biz # only accept connection from localhost (サーバのホストネーム) allow localhost # allow localhost to connect to the server and allow 192.168.1.0/24 # allow 0.0.0.0 Gateway to connect to the server and (ウェブステータスページにアクセス許可するIPアドレス) allow admin:password # require user 'admin' with password 'monit'(ウェブステータスページのログインIDとパスワード) allow @monit # allow users of group 'monit' to connect (rw) allow @users readonly # allow users of group 'users' to connect readonly # 設定ファイルのインクルード # -> /etc/monit.d/ 以下に SERVICE_NAME 形式で配置してください include /etc/monit.d/*
monitrcのパミション設定
# chmod 0700 /etc/monitrc
monit.dディレクトリを作成
# mkdir /etc/monit.d
監視項目を設定
監視項目を/etc/monit.dディレクトリに作成しておきます。
http, mysql, sshd, sendmail など
# cd /etc/monit.d/
▽http監視用
# vi httpd check process httpd with pidfile /var/run/httpd.pid start program = "/etc/init.d/httpd start" with timeout 60 seconds stop program = "/etc/init.d/httpd stop" if failed host (SERVER-IP-ADDRESS) port 80 send "GET / HTTP/1.1\r\nHost: (SERVER-IP-ADDRESS)\r\n\r\n" expect "HTTP/1\.[01x] [1-4][0-9]{2} .*\r\n" with timeout 60 seconds then restart if 1000 restarts within 1000 cycles then timeout
▽sendmail監視用
# vi sendmail check process sendmail with pidfile /var/run/sendmail.pid start program = "/etc/init.d/sendmail start" with timeout 60 seconds stop program = "/etc/init.d/sendmail stop" if failed port 25 then restart if 1000 restarts within 1000 cycles then timeout
▽mysql監視用
# vi mysqld check process mysqld with pidfile "/var/run/mysqld/mysqld.pid" start = "/etc/init.d/mysqld start" stop = "/etc/init.d/mysqld stop" if failed unixsocket /var/lib/mysql/mysql.sock with timeout 60 seconds then restart if 5 restarts within 5 cycles then timeout
▽ssh 監視用
# vi sshd check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" with timeout 60 seconds stop program "/etc/init.d/sshd stop" if failed port 22 protocol ssh then restart if 5 restarts within 5 cycles then timeout
確認のためApacheなどを停止して自動起動してくれるかを確認します。
問題がなければ設定が完了です。
Pleskのようなサーバー管理ツールを搭載せずにコマンドでサーバー操作をすることが前提であれば、テクニカルサポートを省いたエンジニア向けのクラウド、ローコストクラウドがオススメです。