Monitでサーバー監視(Ping監視、URL監視)

概要

今回はMonitで外部監視を行います。前回のMonitの記事で作成した環境を流用します。
Ping監視、HTTPS監視、SSL証明書期限の監視を実際に設定して確認していきます。

検証環境

OSRed Hat Enterprise Linux 8.6 (Ootpa)
監視monit-5.30.0-1.el8

[1]Ping監視

まず、疎通確認でよく使用するPingによる監視を設定します。
本記事ではローカルのサーバを指定していますが、Pingが通るなら外部のサーバをFQDNで指定することもできます。Pingの回数、サイズ、タイムアウト等細かく設定できます。

※responsetimeはマニュアルに記載があるので監視条件として使用できるかと思ったのですが、今回導入のmonit-5.30では未実装でした。monit-5.32では使用できるようですが未検証です。
[参考:tildeslash]

# vi /etc/monit.d/ping
check host ping_local with address 10.14.206.10
    if failed ping
        count 5
        size 64
#        responsetime 100 milliseconds
        timeout 1 seconds
    then alert

設定ファイルを作成したら、シンタックスチェックを行いmonitのreloadを実行します。

# monit -t
Control file syntax OK
# monit reload
Reinitializing monit daemon

monitの状態を確認するときちんと監視できていることが確認できます。

# monit status
Monit 5.30.0 uptime: 7d 0h 53m

Remote Host 'ping_local'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  ping response time           0.572 ms
  data collected               Wed, 21 Sep 2022 16:16:20
~ 省略 ~

設定ファイルのcount項目で「5」を指定していますが、上記のstatusでは5回Pingを実行しているかわからないので、実際に5回Pingパケットが飛んでいるか確認してみます。

# tcpdump icmp
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes
16:17:20.092003 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 1, length 72
16:17:20.092344 IP 10.14.206.10 > monit02.cvtk.test: ICMP echo reply, id 12851, seq 1, length 72
16:18:20.139446 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 1, length 72
16:18:20.139966 IP 10.14.206.10 > monit02.cvtk.test: ICMP echo reply, id 12851, seq 1, length 72

1回しかPingを実行していないですね。
マニュアルでは「Pingの最大数を指定する」と記載があるので1回目で成功した場合はその時点で検査を終了しているのかもしれません。
Pingが通らない場合に設定どおり5回実行しているか対向サーバ(今回の場合は、10.14.206.10のサーバ)を停止させてtcpdumpで観察してみます。

# tcpdump icmp
dropped privs to tcpdump
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens192, link-type EN10MB (Ethernet), capture size 262144 bytes
16:24:20.396630 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 1, length 72
16:24:21.398478 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 2, length 72
16:24:22.399864 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 3, length 72
16:24:23.401276 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 4, length 72
16:24:24.402697 IP monit02.cvtk.test > 10.14.206.10: ICMP echo request, id 12851, seq 5, length 72

Pingが疎通できない場合は5回Pingを実行しています。countは最大試行回数という意味合いですね。
monitの状態を確認すると、「ICMP failed」が表示されていることを確認できます。

# monit status
Monit 5.30.0 uptime: 7d 1h 1m

Remote Host 'ping_local'
  status                       ICMP failed
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  ping response time           connection failed
  data collected               Wed, 21 Sep 2022 16:24:25
~ 省略 ~

アラートメールでもきちんと通知されています。

Subject: monit alert --  monit02.cvtk.test ICMP failed ping_local
Date: Wed, 21 Sep 2022 07:34:11 GMT
X-Mailer: Monit 5.30.0
Content-Type: text/plain; charset=utf-8
Status: R

ICMP failed Service ping_local
                Date:        Wed, 21 Sep 2022 16:34:11
                Action:      alert
                Host:        monit02.cvtk.test
                Description: ping test failed

           Your faithful employee,
           Monit

停止したPing疎通先サーバを復旧させると、メールでもPing疎通ができたことが通知されます。

Subject: monit alert --  monit02.cvtk.test ICMP succeeded ping_local
Date: Wed, 21 Sep 2022 07:42:46 GMT
X-Mailer: Monit 5.30.0
Content-Type: text/plain; charset=utf-8
Status: R

ICMP succeeded Service ping_local
                Date:        Wed, 21 Sep 2022 16:42:46
                Action:      alert
                Host:        monit02.cvtk.test
                Description: ping test succeeded [response time 1.449 ms]

           Your faithful employee,
           Monit

[2]URL監視

次にURL監視の設定です。httpsでの疎通確認の設定を行います。

下記の設定ファイルを作成します。

# vi /etc/monit.d/https
check host httpbin.org with address httpbin.org
    if failed
        port 443
        protocol https
        request "/ip"
        with ssl options {verify: enable}
    then alert

※確認先に指定している「httpbin.org」はアクセスに対して様々なhttpレスポンスを返すことができるサイトです。

addressの後ろに接続先のFQDNを記載します。
requestにディレクトリやファイル等リソースへのパスを指定します。
with ssl optionsでSSL証明書の検証実施有無や自己証明書でも成功とする等SSL証明書に関する様々な指定が可能です。

シンタックスチェックを行いmonitのreloadを実行します。

# monit -t
Control file syntax OK
# monit reload
Reinitializing monit daemon

monitの状態を確認するときちんと監視できていることが確認できます。
レスポンスタイムやSSL証明書の残りの期限が出ています。

# monit status
Monit 5.30.0 uptime: 26m

Remote Host 'httpbin.org'
  status                       OK
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  port response time           897.849 ms to httpbin.org:443/ip type TCP/IP using TLS (certificate valid for 82 days) protocol HTTP
  data collected               Wed, 28 Sep 2022 10:30:47              Wed, 21 Sep 2022 16:16:20
~ 省略 ~

SSL証明書の期限の確認方法は時々運用で話題に出てくることがありますが、カレンダーでの自己管理だけではなくシステム的な通知としてmonitが使えそうです。

実際に上のhttpsの設定ファイルのSSL証明書の有効期限を確認する設定を追加します。
先ほどのhttpbin.orgの確認でSSL証明書の残り期間が82daysと出ていたので、90日未満の場合にアラートを出すように設定します。

# vi /etc/monit.d/https
check host httpbin.org with address httpbin.org
    if failed
        port 443
        protocol https
        request "/ip"
        with ssl options {verify: enable}
        and certificate valid > 90 days
    then alert

SSL証明書の有効期限のチェックもきちんとできて、statusがTimestamp failedとなりメールでの通知も行われます。

# monit status
Monit 5.30.0 uptime: 1h 2m

Remote Host 'httpbin.org'
  status                       Timestamp failed
  monitoring status            Monitored
  monitoring mode              active
  on reboot                    start
  port response time           751.798 ms to httpbin.org:443/ip type TCP/IP using TLS (certificate valid for 82 days) protocol HTTP
  data collected               Wed, 28 Sep 2022 11:06:54

monitでのURL監視では、例に挙げた検査項目の他にレスポンスコードやヘッダ情報、コンテンツ内容等も検査可能で、リクエストにパラメータを渡してサイトの動作状況を確認したり、コンテンツ改竄の検知もできますので汎用性は高そうです。

技術情報

商標について

関連サービス

OSS導入支援

お気軽にお問い合わせください。応対時間 9:30-17:30 [ 土・日・祝日除く ]

お問い合わせ
  • X