🐶
Monit入門:軽量な監視ツールの基本的な使い方
軽量な監視ツールMonitについて勉強しました。
公式サイトを参考に基本的な機能をまとめました。
最後にデモとして、Apacheをわざと落としてMonitに再起動させました。
Monitとは
- 軽量な監視ツール
- サーバー上のプロセスやリソースなどを監視できる
- 監視対象のプロセスが停止したさいに自動で再起動させることができる
- リソースの使用状況を監視してアラートを発報することができる
- 他にも、ファイルやディレクトリの更新日時を監視したり、再起動やアラートの代わりにシェルスクリプトの実行ができる
Monitの特徴・機能
- サーバー上のプロセスなどを監視する
- 監視できるもの
- ファイル
- ディレクトリ
- ディスク
- プロセス
- システム
- ホスト
- コンテナ
- 監視できるもの
- 監視対象の検証が失敗したときに様々なアクションを自動で実行できる
- アラート
- プロセスの再起動
- シェルスクリプトの実行
- コンテナ内でPID 1として起動できることが特徴のひとつ
- これにより初期化と監視をまとめて担える
- CLIに加えて、ブラウザから確認できるUIもある(画像参照)
Monitの基本的な使い方
-
monitrc
に監視設定を記述 -
monit
コマンドを実行するとdaemonとして起動する- 終了は
monit quit
- 終了は
ホストの監視
- ポート80や443が死んでいたらアラート
check host mmonit.com with address mmonit.com
if failed port 80 protocol http then alert
if failed port 443 protocol https then alert
ホストの監視条件を複雑にする
- HTTP Statusの検証
- エンドポイントへのアクセスと、レスポンスの検証
- HTTP, HTTPS以外のプロトコル(例:FTP, SSHなど)も検証できる
check host mmonit.com with address mmonit.com
if failed
port 80 protocol http
and status = 200
and request /monit/ with content = "Monit [0-9.]+"
then alert
プロセスの監視
プロセスの起動チェック
- 例えば、Apacheの起動確認は以下の通り
- Apacheが起動していなければアラートになる
check process apache with pidfile /var/run/httpd.pid
-
start program
を記述することでアラートの代わりにプロセスを起動できる-
stop program
はプロセスを止める
-
check process apache with pidfile /var/run/httpd.pid
start program = "/etc/init.d/apache2 start"
stop program = "/etc/init.d/apache2 stop"
-
restart program
で再起動することもできる
check process apache with pidfile /var/run/httpd.pid
restart program = "/etc/init.d/apache2 restart"
if failed port 80 protocol http then restart
プロセスが使用しているリソースの監視
- 例えば、Apacheが使用しているCPU、メモリ、ディスクを監視するには
check process apache with pidfile /var/run/httpd.pid
...
if cpu > 95% for 2 cycles then alert
if total cpu > 99% for 10 cycles then restart
if memory > 50 MB then alert
if total memory > 500 MB then restart
if disk read > 10 MB/s for 2 cycles then alert
Monitを使ってみる
- MonitとApacheをコンテナ上で起動して、Monitの機能を使ってみる
-
localhost:8888
でApacheにアクセス -
localhost:8889
でMonitのUIにアクセス
-
- Apacheのプロセスを人力で終了させて、Monitが再起動させる様子を観察する
MonitとApacheを実行するコンテナの準備
- Dockerfile
FROM ubuntu:22.04
RUN apt-get update && apt-get install -y \
apache2 \
monit \
&& rm -rf /var/lib/apt/lists/*
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf
COPY monitrc /etc/monit/monitrc
RUN chmod 600 /etc/monit/monitrc
EXPOSE 80 2812
# Monitを直接PID 1として起動
ENTRYPOINT ["/usr/bin/monit", "-I"]
- monitrc
# 監視間隔(30秒ごと)
set daemon 30
# ログ設定
set log syslog
# UIの設定
set httpd port 2812 and
use address 0.0.0.0
# デモなのですべてのIPアドレスからの接続を許可
allow 0.0.0.0/0.0.0.0
allow admin:monit
# システム全体の監視
check system localhost
if loadavg (1min) > 4 then alert
if loadavg (5min) > 2 then alert
if memory usage > 75% then alert
if cpu usage > 95% for 10 cycles then alert
# Apache2の監視設定
check process apache2 with pidfile /var/run/apache2/apache2.pid
start program = "/usr/sbin/apachectl start"
stop program = "/usr/sbin/apachectl stop"
# Apache2が起動していない場合、startを実行
if does not exist then start
if failed host localhost port 80
protocol http
request "/"
for 2 cycles
then restart
if 3 restarts within 5 cycles then timeout
- イメージのビルド
$ docker build -t monit-apache-demo .
[+] Building 1.7s (10/10) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 380B 0.0s
=> [internal] load metadata for docker.io/library/ubuntu:22.04 1.5s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/5] FROM docker.io/library/ubuntu:22.04@sha256:4e0171b9275e12d375863f2b3ae9ce00a4c53ddda176bd55868df 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 820B 0.0s
=> CACHED [2/5] RUN apt-get update && apt-get install -y apache2 monit && rm -rf /var/lib/apt 0.0s
=> CACHED [3/5] RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf 0.0s
=> [4/5] COPY monitrc /etc/monit/monitrc 0.0s
=> [5/5] RUN chmod 600 /etc/monit/monitrc 0.1s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:1cf9166a3b9a1f3db3ca7a973362367a03cec6c58fb86ef2038ea04e0c156957 0.0s
=> => naming to docker.io/library/monit-apache-demo 0.0s
View build details: docker-desktop://dashboard/build/default/default/sgi1mjviytdktlpzdcm4hirkf
- コンテナ起動
-
http://localhost:8888
でApacheを起動 -
http://localhost:8889
でMonitのUIを起動
-
$ docker run -d \
--name monit-test \
-p 8888:80 \
-p 8889:2812 \
monit-apache-demo
- コンテナが正常に起動したことをログで確認
$ docker logs monit-test
New Monit id: 492f9b77dd6a26795363dc02998a75f4
Stored in '/root/.monit.id'
Starting Monit 5.31.0 daemon with http interface at [0.0.0.0]:2812
'localhost' Monit 5.31.0 started
'apache2' process is not running
'apache2' start: '/usr/sbin/apachectl start'
MonitとApacheの起動確認
- プロセスの状態を確認
- MonitとApacheが起動していることがわかる
$ docker exec monit-test ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.0 84816 7680 ? Ssl 10:50 0:00 /usr/bin/monit -I
root 20 0.0 0.0 6780 4448 ? Ss 10:50 0:00 /usr/sbin/apache2 -k start
www-data 21 0.0 0.0 1997872 4072 ? Sl 10:50 0:00 /usr/sbin/apache2 -k start
www-data 22 0.0 0.0 1997872 4072 ? Sl 10:50 0:00 /usr/sbin/apache2 -k start
root 77 0.0 0.0 7064 2816 ? Rs 10:50 0:00 ps aux
- コンテナを起動してから30秒以内にMonitのステータスを確認
- 監視間隔をmonitrcで30秒に設定(
set daemon 30
)しているため、MonitはまだApache2が存在しないと認識している
- 監視間隔をmonitrcで30秒に設定(
$ docker exec monit-test monit status
Monit 5.31.0 uptime: 0m
System 'localhost'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [1.30] [1.29] [1.20]
cpu 0.0%usr 0.0%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice
memory usage 6.0 GB [19.1%]
swap usage 0 B [0.0%]
uptime 6h 12m
boot time Sun, 07 Sep 2025 04:38:06
filedescriptors 13472 [0.0% of 9223372036854775807 limit]
data collected Sun, 07 Sep 2025 10:50:16
Process 'apache2'
status Does not exist
monitoring status Monitored
monitoring mode active
on reboot start
data collected Sun, 07 Sep 2025 10:50:16
- コンテナを起動してから30秒以上経過後にMonitのステータスを確認
- Apache2の起動が反映されている
$ docker exec monit-test monit status
Monit 5.31.0 uptime: 0m
System 'localhost'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [1.30] [1.29] [1.20]
cpu 3.0%usr 4.5%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice
memory usage 5.9 GB [18.9%]
swap usage 0 B [0.0%]
uptime 6h 12m
boot time Sun, 07 Sep 2025 04:38:06
filedescriptors 13408 [0.0% of 9223372036854775807 limit]
data collected Sun, 07 Sep 2025 10:50:46
Process 'apache2'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 20
parent pid 1
uid 0
effective uid 0
gid 0
uptime 0m
threads 1
children 2
cpu 0.0%
cpu total 0.0%
memory 0.0% [4.3 MB]
memory total 0.0% [12.3 MB]
security attribute docker-default (enforce)
filedescriptors 8 [0.1% of 8192 limit]
total filedescriptors 30
read bytes 0 B/s [71.7 kB total]
disk read bytes 0 B/s [0 B total]
disk read operations 0.0 reads/s [21 reads total]
write bytes 0 B/s [278 B total]
disk write bytes 0 B/s [8 kB total]
disk write operations 0.0 writes/s [3 writes total]
port response time -
data collected Sun, 07 Sep 2025 10:50:46
Apacheを終了させる
- pkillでApache終了
$ docker exec monit-test pkill apache2
- Apache終了後30秒以内に、
ps aux
,monit status
,monit summary
を確認-
ps aux
の結果からApacheのプロセスが終了したことがわかる- ゾンビが残ってしまった…(PID: 20)
-
monit status
の結果はまだstatus OK
となっている- プロセスを終了させてから30秒が経過していないため
-
monit summary
の結果はData access error
となっている
-
$ docker exec monit-test ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 84816 7808 ? Ssl 10:50 0:00 /usr/bin/monit -I
root 20 0.0 0.0 0 0 ? Zs 10:50 0:00 [apache2] <defunct>
root 113 0.0 0.0 7064 2816 ? Rs 11:32 0:00 ps aux
$ docker exec monit-test monit status
Monit 5.31.0 uptime: 42m
System 'localhost'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [1.19] [1.16] [1.15]
cpu 3.0%usr 4.3%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice
memory usage 5.8 GB [18.7%]
swap usage 0 B [0.0%]
uptime 6h 54m
boot time Sun, 07 Sep 2025 04:38:06
filedescriptors 13344 [0.0% of 9223372036854775807 limit]
data collected Sun, 07 Sep 2025 11:32:16
Process 'apache2'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 20
parent pid 1
uid 0
effective uid 0
gid 0
uptime 42m
threads 1
children 2
cpu 0.0%
cpu total 0.0%
memory 0.0% [4.3 MB]
memory total 0.0% [14.5 MB]
security attribute docker-default (enforce)
filedescriptors 8 [0.1% of 8192 limit]
total filedescriptors 30
read bytes 0 B/s [71.7 kB total]
disk read bytes 0 B/s [0 B total]
disk read operations 0.0 reads/s [21 reads total]
write bytes 0 B/s [278 B total]
disk write bytes 0 B/s [8 kB total]
disk write operations 0.0 writes/s [3 writes total]
port response time 0.408 ms to localhost:80/ type TCP/IP protocol HTTP
data collected Sun, 07 Sep 2025 11:32:16
$ docker exec monit-test monit summary
Monit 5.31.0 uptime: 42m
Service Name Status Type
localhost OK System
apache2 Data access error Process
- プロセスを終了させてから30秒後
- MonitがApacheにアクセスできないことを検出
$ docker exec monit-test monit status
Monit 5.31.0 uptime: 46m
System 'localhost'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [1.33] [1.24] [1.19]
cpu 2.5%usr 3.9%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice
memory usage 5.9 GB [18.8%]
swap usage 0 B [0.0%]
uptime 6h 58m
boot time Sun, 07 Sep 2025 04:38:06
filedescriptors 13312 [0.0% of 9223372036854775807 limit]
data collected Sun, 07 Sep 2025 11:36:47
Process 'apache2'
status Connection failed | Data access error
monitoring status Monitored
monitoring mode active
on reboot start
data collected Sun, 07 Sep 2025 11:36:17
$ docker exec monit-test monit summary
Monit 5.31.0 uptime: 46m
Service Name Status Type
localhost OK System
apache2 Connection failed | Dat... Process
- さらに30秒後
- Apacheが起動している
- MonitがApacheにアクセスできないことを検出し、自動でrestartさせた!
- monitrcの
if failed host localhost port 80 ... then restart
によるもの
- Apacheが起動している
$ docker exec monit-test monit status
Monit 5.31.0 uptime: 48m
System 'localhost'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
load average [1.18] [1.20] [1.18]
cpu 2.6%usr 4.0%sys 0.0%nice 0.0%iowait 0.0%hardirq 0.0%softirq 0.0%steal 0.0%guest 0.0%guestnice
memory usage 5.8 GB [18.5%]
swap usage 0 B [0.0%]
uptime 7h 1m
boot time Sun, 07 Sep 2025 04:38:06
filedescriptors 13440 [0.0% of 9223372036854775807 limit]
data collected Sun, 07 Sep 2025 11:38:47
Process 'apache2'
status OK
monitoring status Monitored
monitoring mode active
on reboot start
pid 246
parent pid 1
uid 0
effective uid 0
gid 0
uptime 1m
threads 1
children 2
cpu 0.0%
cpu total 0.0%
memory 0.0% [4.5 MB]
memory total 0.0% [13.3 MB]
security attribute docker-default (enforce)
filedescriptors 8 [0.1% of 8192 limit]
total filedescriptors 30
read bytes 0 B/s [71.7 kB total]
disk read bytes 0 B/s [0 B total]
disk read operations 0.0 reads/s [21 reads total]
write bytes 0 B/s [281 B total]
disk write bytes 0 B/s [8 kB total]
disk write operations 0.0 writes/s [3 writes total]
port response time 0.471 ms to localhost:80/ type TCP/IP protocol HTTP
data collected Sun, 07 Sep 2025 11:38:47
$ docker exec monit-test monit summary
Monit 5.31.0 uptime: 48m
Service Name Status Type
localhost OK System
apache2 OK Process
Discussion