CloudWatch Syntheticsのリンク切れチェッカーを使ってみた

記事タイトルとURLをコピーする

CloudWatch Syntheticsは任意の監視スクリプトを実行できるマネージドサービスです。
「任意の」と言っても0からスクリプトを作成するのはハードルが高いので、テンプレートが4つ用意されています。

f:id:swx-watanabe:20210201155619p:plain

前回までの記事では、ハートビートのモニタリングを使っていました。

今回はリンク切れチェッカーを使ってみます。 Webサイトのリンク先が無くなっていたり、障害が起きた場合に検知できるようになります。

準備

下記のリンク構造のWebサイトを用意しました。

f:id:swx-watanabe:20210201155637p:plain

設定

CloudWatch > Canaries > Canaryを作成

設計図

リンク切れチェッカーを指定します。

f:id:swx-watanabe:20210201155649p:plain

Canary ビルダー

任意の名前、チェック対象のURL、リンクの最大数を指定します。

f:id:swx-watanabe:20210201155701p:plain

スケジュール、データ保持、データストレージ

今回はデフォルトのままにします。

f:id:swx-watanabe:20210201155715p:plain

しきい値、アクセス許可

CloudWatchアラームを作成するため、しきい値を有効にします。
もし有効化できない状態になっていたら、Thresholds設定を有効にする必要があります。
アラートについては、CloudWatch Syntheticsのアラートをメール通知する をご覧ください。

適切なRoleを選ぶか、新規でRoleを作成します。

f:id:swx-watanabe:20210201155756p:plain

Canaryを作成します。

動作確認

全てのリンク先が存在している場合

孫ページまで、全てのページをチェックしています。

f:id:swx-watanabe:20210201155808p:plain

Webサーバのログ

18.177.82.170 - - [01/Feb/2020:08:32:43 +0000] "GET / HTTP/1.1" 200 193 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:46 +0000] "GET /child1.html HTTP/1.1" 200 128 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:48 +0000] "GET /child2.html HTTP/1.1" 200 128 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:51 +0000] "GET /child3.html HTTP/1.1" 200 128 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:52 +0000] "GET /grandchild1.html HTTP/1.1" 200 84 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:54 +0000] "GET /grandchild2.html HTTP/1.1" 200 84 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:32:57 +0000] "GET /grandchild3.html HTTP/1.1" 200 84 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"

Canaryのステータス

正常となります。

f:id:swx-watanabe:20210201155820p:plain

リンク先が無くなっている場合

例えば、child2.htmlが無くなっている時は、child3.html以降はチェックされません。

f:id:swx-watanabe:20210201155834p:plain

Webサーバのログ

18.177.82.170 - - [01/Feb/2020:08:47:44 +0000] "GET / HTTP/1.1" 200 193 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:47:47 +0000] "GET /child1.html HTTP/1.1" 200 128 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"
18.177.82.170 - - [01/Feb/2020:08:47:49 +0000] "GET /child2.html HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/75.0.3738.0 Safari/537.36 CloudWatchSynthetics/arn:aws:synthetics:ap-northeast-1:506579938668:canary:link_checker" "-"

Canaryのステータス

失敗となります。

f:id:swx-watanabe:20210201155849p:plain

CloudWatchアラームからのメール

CloudWatchアラームとSNS連携をしている場合は、下記のようなメールを受信可能です。

You are receiving this email because your Amazon CloudWatch Alarm "Synthetics-Alarm-link_checker" in the Asia Pacific (Tokyo) region has entered the ALARM state, because "Threshold Crossed: 2 out of the last 2 datapoints [0.0 (01/02/20 08:49:00), 0.0 (01/02/20 08:44:00)] were less than or equal to the threshold (0.0) (minimum 2 datapoints for OK -> ALARM transition)." at "Saturday 01 February, 2020 08:54:15 UTC".

View this alarm in the AWS Management Console:
https://ap-northeast-1.console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#s=Alarms&alarm=Synthetics-Alarm-link_checker

Alarm Details:
- Name:                       Synthetics-Alarm-link_checker
- Description:                Synthetics alarm thresholds: 2 test failures in 2 mins
- State Change:               OK -> ALARM
- Reason for State Change:    Threshold Crossed: 2 out of the last 2 datapoints [0.0 (01/02/20 08:49:00), 0.0 (01/02/20 08:44:00)] were less than or equal to the threshold (0.0) (minimum 2 datapoints for OK -> ALARM transition).
- Timestamp:                  Saturday 01 February, 2020 08:54:15 UTC
- AWS Account:                xxxxxxxxxxxx

Threshold:
- The alarm is in the ALARM state when the metric is LessThanOrEqualToThreshold 0.0 for 300 seconds. 

Monitored Metric:
- MetricNamespace:                     CloudWatchSynthetics
- MetricName:                          SuccessPercent
- Dimensions:                          [CanaryName = link_checker]
- Period:                              300 seconds
- Statistic:                           Sum
- Unit:                                not specified
- TreatMissingData:                    breaching

State Change Actions:
- OK: 
- ALARM: [arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:Synthetics_Alarms_Topic]
- INSUFFICIENT_DATA: 

--
If you wish to stop receiving notifications from this topic, please click or visit the link below to unsubscribe:
https://sns.ap-northeast-1.amazonaws.com/unsubscribe.html?SubscriptionArn=arn:aws:sns:ap-northeast-1:xxxxxxxxxxxx:Synthetics_Alarms_Topic:xxxxxxx-xxxx-xxxx-b94e-1bbf2b47d4c8&Endpoint=xxxxxxxxxxxx@gmail.com

Please do not reply directly to this email. If you have any questions or comments regarding this email, please contact us at https://aws.amazon.com/support

感想

CloudWatch Synthetics標準テンプレートのリンク切れチェッカーは、うまく動いてくれました。
それなりに複雑な監視も、スクリプト次第で可能とわかりました。
今まで、他のSaaSやOSSで外形監視していたものも、今後はCloudWatch Syntheticsで置き換えられるかもしれません。

渡辺 信秀(記事一覧)

2017年入社 / 地味な内容を丁寧に書きたい