monitとは?
簡易的なプロセス監視ツールで、
プロセスが停止した場合には自動的にプロセスを再起動するようなものです。
やってみる
前提
今回はcentos7でやってみます。
monitで監視するプロセスはhttpd。
# cat /etc/redhat-release
CentOS Linux release 7.9.2009 (Core)
インストール
yum install epel-release
yum install monit
設定ファイルの確認
monitの設定ファイルのコメント無しで見てみる
- set daemon 30
- 30秒ごとに監視を行う
- set log syslog
- syslogを使用し、ログを出力
- set httpd port 2812 and ~ allow admin:monit
- monitの管理画面で確認可能
- include /etc/monit.d/*
- /etc/monit.d/配下の設定ファイルを全て見る
cat /etc/monitrc | grep -v -e '^\s*#' -e '^\s*$'
==========
set daemon 30 # check services at 30 seconds intervals
set log syslog
set httpd port 2812 and
use address localhost # only accept connection from localhost (drop if you use M/Monit)
allow localhost # allow localhost to connect to the server and
allow admin:monit # require user 'admin' with password 'monit'
include /etc/monit.d/*
==========
設定ファイルの作成
- check process httpd with pidfile "/var/run/httpd/httpd.pid"
- 監視対象プロセスを指定
- start program = "/usr/bin/systemctl start httpd"
- プロセス起動のコマンド指定
- stop program = "/usr/bin/systemctl stop httpd"
- プロセス終了のコマンド指定
- if failed port 80 with timeout 10 seconds then restart
- 80番ポートが10秒タイムアウトしたら再起動する
- if failed port 443 with timeout 10 seconds then restart
- 443番ポートが10秒タイムアウトしたら再起動する
- if 10 restarts within 10 cycles then unmonitor
- 10回中10回再起動したら監視を止める
vi /etc/monit.d/httpd
==========
check process httpd with pidfile "/var/run/httpd/httpd.pid"
start program = "/usr/bin/systemctl start httpd"
stop program = "/usr/bin/systemctl stop httpd"
if failed port 80 with timeout 10 seconds then restart
if failed port 443 with timeout 10 seconds then restart
if 10 restarts within 10 cycles then unmonitor
==========
設定ファイルチェック
monit -t
> Control file syntax OK と出ることを確認
起動・自動起動設定
systemctl start monit
systemctl enable monit
意図的にhttpdを落としてみる
- httpdをシャットダウン
systemctl status httpd
==========
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2021-05-22 04:30:02 UTC; 16s ago
.....
==========
systemctl stop httpd
systemctl status httpd
==========
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
.....
==========
- 1分程度待ってからstatus確認
起動してる!!!!
systemctl status httpd
==========
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since Sat 2021-05-22 04:31:04 UTC; 1s ago
.....
==========
- 再起動したときのmonitのログ
cat /var/log/monit.log | less
==========
[UTC May 22 04:36:14] error : 'httpd' process is not running
[UTC May 22 04:36:14] info : 'httpd' trying to restart
[UTC May 22 04:36:14] info : 'httpd' start: '/usr/bin/systemctl start httpd'
==========