the study of monit

15
the study of monit July,12,2014 Tadayasu Yotsu

Upload: tadayasu-yotsu

Post on 19-Jun-2015

482 views

Category:

Technology


1 download

DESCRIPTION

This is my memo when I study monit.

TRANSCRIPT

Page 1: the study of monit

the study of monit

July,12,2014 Tadayasu Yotsu

Page 2: the study of monit

What is monit ?monitはプロセスを監視し、エラー時に自動でメンテナ

ンスと復旧を行うことができるツール。(http://

mmonit.com/monit/)。例えばプロセスを関して問題を検知したらプロセスを再起動するといったことが可能。

プロセスだけでなく、ファイルやディレクトリ、ファイルシステムの変更なども監視可能。

Monitは独自のDSL(Domain Specific Language)を

使用。DSLが非常に読み易くGood.

Page 3: the study of monit

InstallEnvironment

AWS EC2 t1micro, OS:RHEL6.5

Install$ sudo yum install monitどこに何が格納されたのか確認$ rpm -ql monit/etc/logrotate.d/monit/etc/monit.conf/etc/monit.d/etc/monit.d/logging/etc/rc.d/init.d/monit/usr/bin/monit/usr/share/doc/monit-5.1.1/usr/share/doc/monit-5.1.1/CHANGES.txt/usr/share/doc/monit-5.1.1/COPYING/usr/share/doc/monit-5.1.1/LICENSE/usr/share/doc/monit-5.1.1/PLATFORMS/usr/share/doc/monit-5.1.1/README/usr/share/doc/monit-5.1.1/README.DEVELOPER/usr/share/doc/monit-5.1.1/README.SSL/usr/share/man/man1/monit.1.gz/var/log/monit!

Page 4: the study of monit

Configuration

set daemon < 監視する間隔

set logfile <Path> ログファイルの

set logfile syslog facility <faclity syslog

set mailserver < 何かイベントが発生した場合にメールで通知set alert <

set alert < 通知して欲しいイベントのみ通知

set alert < 特定のイベント以外を通知

include <Path> 別の設定ファイルをインクルード

configuration file is /etc/monit.conf

global設定は/etc/monit.confに記載し、プロセスなどの監視・コントロー

ルについては/etc/monit.d/配下にそれぞれ書いていった方がよさそう。

Page 5: the study of monit

例)global? configコントロール全てに共通の設定を/etc/monit.confに記述

##監視間隔を1分に設定set daemon 60##ログをsyslogに飛ばすset logfile syslog facility log_daemon##プライマリとセカンダリのサーバを指定set mailserver mail1.ex.com,mail2.ex.com set mail-format {##アラートメールのフォーマットなどfrom: [email protected]: $HOST : $SERVICE - $EVENTmessage: MonitACTION : $ACTIONSERVICE : $SERVICEat $DATE on $HOST.DESCRIPTION : $DESCRIPTION}set alert [email protected] /etc/monit.d/*.conf

Page 6: the study of monit

A service does not exist (e.g. process is not running)

Cannot read service data (e.g. cannot get filesystem usage)

Execution of service related scripts failed (e.g. start failed)

Invalid service type (e.g. if path points to directory instead of file)

Custom test script returned error

Ping test failed

TCP/UDP connection and/or port test failed

Resource usage test failed (e.g. cpu usage too high)

Checksum mismatch or change (e.g. file changed)

File size test failed (e.g. file too large)

Appendix)Alert Messages以下のsituationのとき、Alertをraiseする。

Timestamp test failed (e.g. file is older then expected)

Permission test failed (e.g. file mode doesn't match)

An UID test failed (e.g. file owned by different user)

A GID test failed (e.g. file owned by different group)

A process' PID changed out of Monit control

A process' PPID changed out of Monit control

Too many service recovery attempts failed

A file content matched the pattern

Filesystem flags changed

A service action was performed by administrator

Monit was started, stopped or reloaded

Page 7: the study of monit

Appendix) List of possible event typesEvent Failure State Success State

ACTION Action done Action done

CHECKSUM Checksum failed Checksum succeeded

CONNECTION Connection failed Connection succeeded

DATA Data access error Data access succeeded

EXEC Execution failed Execution succeeded

FSFLAGS FIlesystem flags failed Filesystem flags succeeded

UID/GID UID/GID failed UID/GID succeeded

ICMP ICMP failed ICMP succeeded

INSTANCE Monit instance changed Monit instance changed not

INVALID Invalid type Type succeeded

NONEXIST Does not exist Exists

PERMISSION Permission failed Permission succeeded

PID/PPID PID/PPID failed PID/PPID succeeded

RESOURCE Resource limit matched Resource limit succeeded

SIZE Size failed Size succeeded

STATUS Status failed Status succeeded

TIMEOUT Timeout Timeout recovery

TIMESTAMP Timestamp failed Timestamp succeeded

UPTIME Uptime failed Uptime succeeded

Page 8: the study of monit

Control fileControl fileはmonitのDSLを用いて記述する。以下8つの構文をCheckで利用することができる。あとでそれぞれ試してみて挙動を確認してみる。

1 check process <unique name> <PID File Path> | matching <regix>

2 check file <unique name> path <Path>

3 check fifo <unique name> path <Path>

4 check filesystem <unique name> path <Path>

5 check directory <unique name> path <Path>

6 check host <unique name> address <Host Address>

7 check system <unique name>

8 check program <unique name> path <exec_file> [timeout <number> seconds]

Page 9: the study of monit

例1)apacheプロセスの監視localhostの80へのHTTPリクエストを行い、応答がなければhttpdのプロセスをリス

タートする処理をmonitを使って行う。

/etc/monit.d/apache

##監視対象のデーモンのpidファイルを指定 check process apache with pidfile /var/run/httpd/httpd.pid  #起動と停止のプログラムを指定 start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/apache2 stop" if failed host localhost port 80 protocol HTTP request /keepalive.txt then restart group apache

Page 10: the study of monit

確認//monitを起動$ sudo /etc/init.d/monit startmonit を起動中: [ OK ]!//httpdを停止し、monitで検知、monitがhttpdを起動することを確認する。$ sudo /etc/init.d/httpd stophttpd を停止中: [ OK ]//httpdがないことを確認$ ps -ef |grep [h]ttpd![ec2-user@ip-172-31-38-39 monit.d]$ tail /var/log/monit [EDT Jul 11 11:24:07] error : HTTP error: Server returned status 404[EDT Jul 11 11:24:07] error : 'apache' failed protocol test [HTTP] at INET[localhost:80] via TCP[EDT Jul 11 11:24:07] info : 'apache' trying to restart[EDT Jul 11 11:24:07] info : 'apache' stop: /etc/init.d/httpd[EDT Jul 11 11:24:08] info : 'apache' start: /etc/init.d/httpd[EDT Jul 11 11:25:09] error : 'apache' process is not running  <-httpdが起動してないことを検知[EDT Jul 11 11:25:09] info : 'apache' trying to restart [EDT Jul 11 11:25:09] info : 'apache' start: /etc/init.d/httpd <-httpdを起動している![ec2-user@ip-172-31-38-39 monit.d]$ ps -ef |grep [h]ttpdroot 1621 1 0 11:25 ? 00:00:00 /usr/sbin/httpdapache 1623 1621 0 11:25 ? 00:00:00 /usr/sbin/httpdapache 1624 1621 0 11:25 ? 00:00:00 /usr/sbin/httpdapache 1625 1621 0 11:25 ? 00:00:00 /usr/sbin/httpdapache 1626 1621 0 11:25 ? 00:00:00 /usr/sbin/httpd

例1)apacheプロセスの監視

Page 11: the study of monit

例2)sshdプロセスの監視sshdのプロセスを監視し、プロセスが落ちているときはプロセスを再起動する。ただし、何かの不具合で何回も再起動が行われるのを避けるため、3回の監視タイミングで2回再起動を行った場合、モニタリングの対象から外す。

/etc/monit.d/sshd

##監視対象であるsshdのpidファイルを指定check process sshd with pidfile /var/run/sshd.pid##set daemonで指定した秒数 x every [num cycle]のタイミングで監視を実行every 2 cyclestart program = "/etc/rc.d/init.d/sshd start"stop program = "/etc/rc.d/init.d/sshd stop"##再起動を試みる回数は2回。3回の監視タイミングで2回再起動したらモニタを停止if 2 restarts within 3 cycles then unmonitorgroup ssh

Page 12: the study of monit

確認//sshdを停止$ sudo /etc/init.d/sshd stopsshd を停止中: [ OK ]!//接続できないことを確認$ ./ssh-ec2 XXXXssh: connect to host XXXX port 22: Connection refused!//var/log/monitには以下が記載[EDT Jul 12 11:42:43] error : 'sshd' process is not running[EDT Jul 12 11:42:43] info : 'sshd' trying to restart[EDT Jul 12 11:42:43] info : 'sshd' start: /etc/rc.d/init.d/sshd!//接続できるようになっていることを確認$ ./ssh-ec2 XXXX!//2回続けてsshdを停止。モニタリングから外した旨のメッセージが出力されていることを確認。[EDT Jul 12 11:52:20] error : 'sshd' service restarted 2 times within 2 cycles(s) - unmonitor

例2)sshdプロセスの監視

Page 13: the study of monit

例3)メモリ使用量の監視プロセスの生存だけでなく、プロセスの使用しているメモリ使用量をトリガーとして処理することも可能。アプリによってはメモリが増え続けてしまうアプリがあるので、そういうアプリに対して、メモリの使用量を監視し、一定以上に増えた場合に再起動を行う。

/etc/monit.d/apache_mem

##監視対象のデーモンのpidファイルを指定 check process apache with pidfile /var/run/httpd/httpd.pid  #起動と停止のプログラムを指定 start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/apache2 stop” ##メモリ使用量が全体の5%を超えた時に再起動 if memory usage > 5% then restart if 3 restarts within 3 cycles then stop group apache

Page 14: the study of monit

例4)CPU使用率の監視プロセスのCPU使用率ををトリガーとして処理することも可

能。CPUパワーを浪費しているものはアラートを上げるようにする。

/etc/monit.d/apache_cpu

##監視対象のデーモンのpidファイルを指定 check process apache with pidfile /var/run/httpd/httpd.pid  #起動と停止のプログラムを指定 start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/apache2 stop” ##CPU使用率が全体の60%を超えた時にアラートを上げる if cpu usage > 60% then alert group apache

Page 15: the study of monit

例5)ファイルの監視/etc/apache2/conf/httpd.conf の内容が変更され

たら、httpdにその設定内容を反映させる。

/etc/monit.d/apache_conf

check file httpd.conf with path /etc/httpd/conf/httpd.conf## チェックサムでファイルの変更をチェックif changed checksum then exec "/etc/init.d/httpd graceful”# タイムスタンプでファイルの変更をチェックする場合## if changed timestamp then exec "/etc/init.d/apache2 graceful"