monit サービス監視 プロセス監視

monitでサービスを自律監視 落ちたサービスを自動再起動させる【Linux】

優 monit サービス監視 プロセス監視

monit サービス監視 プロセス監視

@see https://mmonit.com/monit/

monitでサービスの監視と自動起動を行おう٩(๑❛ᴗ❛๑)۶

万が一のダウンタイムを最小限に抑える為にMySQLなどのサービスを予め監視しておき、プロセスがダウンをしたら自動起動させるというmonitのご紹介。Watchdogも似ていますが、あれはサーバそのもののリブートだけなんですよね。

 

インストール

 

# yum install monit

全体設定

以下のように設定を行う。

 

# 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監視

 

# 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監視

 

# 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監視

 

# 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監視

 

# 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監視

 

# 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などクリティカルなサービスのダウンタイムを防げる。

# monit -t

Control file syntax OK

 

起動させよう(◍•ᴗ•◍)

 

# service monit restart
# chkconfig monit on

 

ファイアウォール設定

 

# vi /etc/sysconfig/iptables

#monit
-A INPUT -m state --state NEW -m tcp -p tcp --dport 2812 -j ACCEPT
# service iptables restart

WEBブラウザから動作確認

 

アクセスしてみよう。

http://IPアドレス:2812/

 

user:admin
password:パスワード

 

monit サービス自動起動 ダウン 落ちた プロセス監視 linux

監視が出来ている事を確認したら初期設定に切り替えます。
WEBブラウザからサービス操作が出来てしまう為大変危険です。

 

# 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

 

# service monit restart

 

# chkconfig monit on

 

サマリー

 

# 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

 

詳細確認

 

# 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

お疲れ様です。

 

monit サービス監視 プロセス監視
最新情報をチェックしよう!
>システム構築・保守に特化した会社です。

システム構築・保守に特化した会社です。

システムの構築・保守運用「システムガーディアン」 社内システム担当が欲しいが、専属で雇うほどの仕事量はない。 必要な時に必要なだけ頼りたいというお悩みを持つ企業様へ専門知識を持って対応を行っております。 サーバから各種システムまで自社・他社で構築されたシステムに対してサポートを行っております。

CTR IMG