monit のインストール
パッケージが存在するので導入は簡単です。以下のコマンドを入力します。sudo aptitude install monit
monit の設定
まず全体的な設定を行います。デフォルトの設定にアラートを通知するメールの設定を追加します。
sudo vi /etc/monit/monitrc
set mailserver smtp.gmail.com port 587
username "メールユーザ名" password "メールパスワード"
using tlsv1
set mail-format {
from: メールアドレス
subject: $SERVICE $EVENT at $DATE
message:
Monit $ACTION $SERVICE
at $DATE
on $HOST: $DESCRIPTION.
Yours sincerely,
monit
}
set alert メールアドレス
ここでmonitのサービスを再起動すると、メールアドレスにmonitが再起動したメッセージが通知されます。なお、メールのパスワードの文字列に「"」等が含まれる場合はエスケープする必要があります。monitはruby言語で作られているようで、rubyの文字列と認識されるように適宜エスケープをしてください。シングルクウォート文字でも大丈夫でした。
監視対象の追加
監視対象の設定の追加は、/etc/monit/monitrc.d/ にファイルとして追加し、/etc/monit/conf.d にリンクをはると、monit 起動時に反映されるという使い方のようです。http://shanon-tech.blogspot.jp/2011/11/15monit.html
には設定項目の説明が掲載されていますので、参考にしましょう。
apache2 の設定
check process apache2 with pidfile /var/run/apache2.pid
start program = "/usr/sbin/service apache2 start"
stop program = "/usr/sbin/service apache2 stop"
if failed host 127.0.0.1 port 80 protocol http
and request "/" then restart
if 3 restarts within 3 cycles then timeout
group www-data
MySQL の設定
check process mysql with pidfile /var/run/mysqld/mysqld.pid
start program = "/usr/sbin/service mysql start"
stop program = "/usr/sbin/service mysql stop"
if failed host 127.0.0.1 port 3306 then restart
if 3 restarts within 3 cycles then timeout
group www-data
sshd の設定
check process sshd with pidfile /var/run/sshd.pid
start program = "/usr/sbin/service ssh start"
stop program = "/usr/sbin/service ssh stop"
if failed port 22 protocol ssh then restart
if 3 restarts within 3 cycles then timeout
group connection