サーバー運用にあたっては、何らかの監視ツールが必要です。 本日は、「monitによるサービス監視」についてです。
こんにちは。猫ITソリューションズ広報の齊藤メイ(♀)です。 本日は、「monitによるサービス監視」についてです。貴重な情報をありがとうございます。深く感謝の意を表します。
参考:
サーバプロセスの監視 – monit編 ->> OpenMediaLaboratory | Web,Wordpress,サーバ構築,Linux,PHP,iPhoneの話題がメインです。
Monitでサービス・プロセス監視、止まったら勝手にリスタート(3) « SawanoBlog.
monit で httpd を監視するときの tips | Carpe Diem
マニュアル
Monit - utility for monitoring services on a Unix system
※SSLでの接続はOFFにしている。
※管理画面は使わない
移動
1 |
# cd /usr/local/src |
ダウンロード
以下を参照し、最新RPMをダウンロード。http://dag.wieers.com/rpm/packages/monit/
1 |
# wget http://dag.wieers.com/rpm/packages/monit/monit-4.9-2.el5.rf.x86_64.rpm |
インストール
追記:yumにある?
1 2 |
# yum install monit # rpm -ivh monit-4.9-2.el5.rf.x86_64.rpm |
デフォルトの設定ファイルをバックアップ
1 |
# cp /etc/monit.conf /etc/monit.conf.bk |
設定ファイル(全体)を編集
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# vi /etc/monit.conf # 監視を行う間隔を秒で指定 set daemon 120 # ログファイルの記録方法 ※/var/log/messagesへ出力 set logfile syslog facility log_daemon # アラートメールなどを配信するためのメールサーバ set mailserver localhost # アラートメールを配信するメールアドレス set alert xxxx@xxxx.com # アラートメールのメールフォーマット set mail-format { subject: monit alert-- $EVENT $SERVICE @$HOST message: Monit $ACTION $SERVICE at $DATE on $HOST. Yours sincerely, monit } #### 管理画面に関する情報 #### # ポート番号:2812 set httpd port 2812 # SSLにてアクセス ※まだSSLを設定していないので今回は未使用 #SSL ENABLE # PEMファイルの場所を指定 #PEMFILE /etc/monit.d/pem/monit.pem # アクセスの際に指定するアドレス use address localhost # アクセス許可するIPアドレス allow localhost #allow xxx.xxx.xxx.xxx # アクセスする際に使用するIDとパスワード allow ID:パスワード # その他設定ファイルの設置先 include /etc/monit.d/* #initからの起動にする set init |
監視用スクリプトを作成
httpd(apache)
1 2 3 4 5 6 7 8 9 10 |
# vi /etc/monit.d/httpd.conf check process apache with pidfile /var/run/httpd.pid start program = "/usr/sbin/apachectl start" stop program = "/usr/sbin/apachectl stop" if 5 restarts within 5 cycles then timeout if failed port 80 then restart if cpu > 95% for 5 cycles then restart if totalmem > 800 MB for 5 cycles then restart if children > 250 then restart if 3 restarts within 5 cycles then timeout |
ssh
1 2 3 4 5 6 |
# vi /etc/monit.d/sshd.conf check process sshd with pidfile /var/run/sshd.pid start program "/etc/init.d/sshd start" stop program "/etc/init.d/sshd stop" if 5 restarts within 5 cycles then timeout if failed port 22 protocol ssh then restart |
上記ファイルのチェックは以下コマンドで可能。
1 |
# /usr/bin/monit -t |
initからの起動設定
1 2 3 |
# vi /etc/inittab 最後に追記 mo:2345:respawn:monit -Ic /etc/monit.conf |
動作確認
monitが停止していることを確認したら、initの再読み込みを下記コマンドで行う(=起動)
1 2 |
# ps aux|grep monit # telinit q |
起動しているか確認
1 2 3 |
# ps aux|grep monit root 2022 1.0 0.3 52884 1744 ? Ssl 22:43 0:00 monit -Ic /etc/monit.conf ←起動している root 2029 0.0 0.1 65380 824 pts/0 R+ 22:43 0:00 grep monit |
試しに停止
1 |
# kill -9 2022 |
再度確認
1 2 3 |
# ps aux|grep monit root 2031 0.5 0.3 52884 1752 ? Ssl 22:43 0:00 monit -Ic /etc/monit.conf ←また起動している root 2038 0.0 0.1 65380 824 pts/0 R+ 22:43 0:00 grep monit |