@see https://mmonit.com/monit/
目次
monitでサービスの監視と自動起動を行おう٩(๑❛ᴗ❛๑)۶
万が一のダウンタイムを最小限に抑える為にMySQLなどのサービスを予め監視しておき、プロセスがダウンをしたら自動起動させるというmonitのご紹介。Watchdogも似ていますが、あれはサーバそのもののリブートだけなんですよね。
インストール
1 |
# yum install monit |
全体設定
以下のように設定を行う。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# vi /etc/monit.conf #30秒毎に監視を行う set daemon 30 #アラート set mail-format { from: monit_alert@ドメイン.com } set alert yuu@ほげほげ.com #WEB設定 set httpd port 2812 and use address サーバIPアドレス # only accept connection from localhost allow クライアントのIPアドレス # allow localhost to connect to the server and allow admin:パスワード # require user 'admin' with password 'monit' allow @monit # allow users of group 'monit' to connect (rw) allow @users readonly # allow users of group 'users' to connect readonly |
サービス監視設定
個別のサービス監視ファイルを作成する。
/etc/monit.d/以下に設置しよう。
MySQL監視
1 2 3 4 5 6 |
# vi /etc/monit.d/mysqld.conf check process mysql with pidfile /var/run/mysqld/mysqld.pid start program = "/etc/init.d/mysqld start" stop program = "/etc/init.d/mysqld stop" if 5 restarts within 5 cycles then timeout |
Apache監視
1 2 3 4 5 6 |
# vi /etc/monit.d/httpd.conf check process httpd with pidfile /var/run/httpd/httpd.pid start program = "/etc/init.d/httpd start" stop program = "/etc/init.d/httpd stop" if 5 restarts within 5 cycles then timeout |
Nginx監視
1 2 3 4 5 6 |
# vi /etc/monit.d/nginx.conf check process nginx with pidfile /var/run/nginx.pid start program = "/etc/init.d/nginx start" stop program = "/etc/init.d/nginx stop" if 5 restarts within 5 cycles then timeout |
SSH監視
1 2 3 4 5 6 |
# vi /etc/monit.d/sshd.conf check process ssh 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 |
NFS監視
1 2 3 4 5 6 |
# vi /etc/monit.d/nfs.conf check process nfs with pidfile /var/run/rpcbind.pid start program = "/etc/init.d/nfs start" stop program = "/etc/init.d/nfs stop" if 5 restarts within 5 cycles then timeout |
構文エラーチェック
構文エラーチェックがあるサービスはチェックを行ってから再起動させる癖をつけよう。ApacheやNginxなどクリティカルなサービスのダウンタイムを防げる。
1 2 3 |
# monit -t Control file syntax OK |
起動させよう(◍•ᴗ•◍)
1 |
# service monit restart |
1 |
# chkconfig monit on |
ファイアウォール設定
1 2 3 4 |
# vi /etc/sysconfig/iptables #monit -A INPUT -m state --state NEW -m tcp -p tcp --dport 2812 -j ACCEPT |
1 |
# service iptables restart |
WEBブラウザから動作確認
アクセスしてみよう。
http://IPアドレス:2812/
user:admin
password:パスワード
監視が出来ている事を確認したら初期設定に切り替えます。
WEBブラウザからサービス操作が出来てしまう為大変危険です。
1 2 3 4 5 6 7 8 |
# vi /etc/monit.conf 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' allow @monit # allow users of group 'monit' to connect (rw) allow @users readonly # allow users of group 'users' to connect readonly |
1 |
# service monit restart |
1 |
# chkconfig monit on |
サマリー
1 2 3 4 5 6 7 8 9 10 |
# monit summary The Monit daemon 5.5 uptime: 0m Process 'ssh' Running Process 'nginx' Running Process 'nfs' Running Process 'mysql' Running Process 'httpd' Running System 'ほげほげ' Running |
詳細確認
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# monit status The Monit daemon 5.5 uptime: 0m Process 'ssh' status Running monitoring status Monitored pid 11836 parent pid 1 uptime 14d 4h 22m children 2 memory kilobytes 1092 memory kilobytes total 6984 memory percent 0.0% memory percent total 0.0% cpu percent 0.0% cpu percent total 0.0% data collected Tue, 01 Sep 2015 18:59:37 Process 'nginx' status Running monitoring status Monitored pid 19507 parent pid 1 uptime 7d 7h 5m children 9 memory kilobytes 1256 memory kilobytes total 32776 memory percent 0.0% memory percent total 0.1% cpu percent 0.0% cpu percent total 0.0% data collected Tue, 01 Sep 2015 18:59:37 Process 'nfs' status Running monitoring status Monitored pid 1101 parent pid 1 uptime 15d 0h 4m children 0 memory kilobytes 748 memory kilobytes total 748 memory percent 0.0% memory percent total 0.0% cpu percent 0.0% cpu percent total 0.0% data collected Tue, 01 Sep 2015 18:59:37 Process 'mysql' status Running monitoring status Monitored pid 1438 parent pid 1234 uptime 15d 0h 4m children 0 memory kilobytes 614140 memory kilobytes total 614140 memory percent 3.7% memory percent total 3.7% cpu percent 0.0% cpu percent total 0.0% data collected Tue, 01 Sep 2015 18:59:37 Process 'httpd' status Running monitoring status Monitored pid 31469 parent pid 1 uptime 6d 5h 7m children 20 memory kilobytes 26220 memory kilobytes total 1406528 memory percent 0.1% memory percent total 8.5% cpu percent 0.0% cpu percent total 0.0% data collected Tue, 01 Sep 2015 18:59:37 System 'ほげほげ' status Running monitoring status Monitored load average [0.52] [0.61] [0.55] cpu 0.0%us 0.0%sy 0.0%wa memory usage 3077064 kB [18.6%] swap usage 42576 kB [1.0%] data collected Tue, 01 Sep 2015 18:59:37 |
お疲れ様です。