monitとは?
cron , nginx 等が落ちた時に自動復旧してくれる。
インストール
yum install -y monit
設定ファイルの編集 (デフォルトのままでも良い。)
vim /etc/monitrc
スタート
systemctl start monit
systemctl enable monit
sudo monit status
自動起動設定ファイルを作っていく
cron
/etc/monit.d/crond.rc
check process cron with pidfile /run/crond.pid
group system
start program = "/bin/systemctl start crond"
stop program = "/bin/systemctl stop crond"
if 5 restarts within 5 cycles then unmonitor # 5回の監視中に、5回再起動を実施して監視を停止する
sshd
/etc/monit.d/sshd.rc
check process sshd with pidfile /var/run/sshd.pid
start program = "/bin/systemctl start sshd.service"
stop program = "/bin/systemctl stop sshd.service"
if 5 restarts within 5 cycles then unmonitor
mysql (mariadb)
199-9-149-299.pid 数字を適宜変更
/etc/monit.d/mysql.rc
check process mysql with pidfile /var/lib/mysql/199-9-149-299.pid
start program = "/bin/systemctl start mysqld.service"
stop program = "/bin/systemctl start mysqld.service"
if 5 restarts within 5 cycles then unmonitor
redis
/etc/monit.d/redis.rc
check process redis with pidfile /var/run/redis_6379.pid
start program = "/bin/systemctl start redis.service"
stop program = "/bin/systemctl start redis.service"
if 5 restarts within 5 cycles then unmonitor
構文チェックし、リスタート
これやらずにリスタートするとバグに気づかないのでやっとく。
monit -t
sudo systemctl restart monit
ここまで設定完了
動作チェック
pidを確認します
find / -name '*.pid' -print
サービス停止してみます
sudo systemctl stop nginx
find / -name '*.pid' -print
nginx.pid がありませんね。
30秒待ちまーす
find / -name '*.pid' -print
nginx.pid 復活しましたね。