

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 CloudWatch 警示以監控 AWS IoT
<a name="creating_alarms"></a>

您可以建立 CloudWatch 警報，在警示變更狀態時傳送 Amazon SNS 訊息。警示會在您指定的期間，監看單一指標。當指標值在數個期間內超過指定的閾值時，會執行一或多個動作。此動作可為傳送至 Amazon SNS 主題或 Auto Scaling 政策的通知。警示僅會觸發持續狀態變更的動作。CloudWatch 警示不會只因處於特定狀態就觸發動作，狀態必須已遭變更且已維持了一段指定的時間。

**Topics**
+ [如何每天都在我的物件連線失敗時收到通知？](#how_to_detect_connection_failures)
+ [如何每天都在我的物件並未推送資料時收到通知？](#how_to_detect_publish_failures)
+ [如何每天都在我的物件的影子更新遭拒時收到通知？](#detect_rejected_updates)
+ [如何為任務建立 CloudWatch 警示？](#cw-jobs-alarms)

 您可以在 看到 CloudWatch [AWS IoT 指標和維度](metrics_dimensions.md) 警示可以監控的所有指標。

## 如何每天都在我的物件連線失敗時收到通知？
<a name="how_to_detect_connection_failures"></a>

1. 建立名為 `things-not-connecting-successfully` 的 Amazon SNS 主題，並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為 ` sns-topic-arn `。

   如需如何建立 Amazon SNS 通知的詳細資訊，請參閱 [Amazon SNS 入門](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html)。

1. 建立警示。

   ```
   aws cloudwatch put-metric-alarm \
       --alarm-name ConnectSuccessAlarm \
       --alarm-description "Alarm when my Things don't connect successfully" \
       --namespace AWS/IoT \
       --metric-name Connect.Success \
       --dimensions Name=Protocol,Value=MQTT \
       --statistic Sum \
       --threshold 10 \
       --comparison-operator LessThanThreshold \
       --period 86400 \
       --evaluation-periods 1 \
       --alarm-actions sns-topic-arn
   ```

1. 測試警示。

   ```
   aws cloudwatch set-alarm-state --alarm-name ConnectSuccessAlarm --state-reason "initializing" --state-value OK
   ```

   ```
   aws cloudwatch set-alarm-state --alarm-name ConnectSuccessAlarm --state-reason "initializing" --state-value ALARM
   ```

1. 確認警示顯示於 [CloudWatch 主控台](https://console.aws.amazon.com/cloudwatch)中。

## 如何每天都在我的物件並未推送資料時收到通知？
<a name="how_to_detect_publish_failures"></a>

1. 建立名為 `things-not-publishing-data` 的 Amazon SNS 主題，並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為 ` sns-topic-arn `。

   如需如何建立 Amazon SNS 通知的詳細資訊，請參閱 [Amazon SNS 入門](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html)。

1. 建立警示。

   ```
   aws cloudwatch put-metric-alarm \
       --alarm-name PublishInSuccessAlarm\
       --alarm-description "Alarm when my Things don't publish their data \
       --namespace AWS/IoT \
       --metric-name PublishIn.Success \
       --dimensions Name=Protocol,Value=MQTT \
       --statistic Sum \
       --threshold 10 \
       --comparison-operator LessThanThreshold \
       --period 86400 \
       --evaluation-periods 1 \
       --alarm-actions sns-topic-arn
   ```

1. 測試警示。

   ```
   aws cloudwatch set-alarm-state --alarm-name PublishInSuccessAlarm --state-reason "initializing" --state-value OK
   ```

   ```
   aws cloudwatch set-alarm-state --alarm-name PublishInSuccessAlarm --state-reason "initializing" --state-value ALARM
   ```

1. 確認警示顯示於 [CloudWatch 主控台](https://console.aws.amazon.com/cloudwatch)中。

## 如何每天都在我的物件的影子更新遭拒時收到通知？
<a name="detect_rejected_updates"></a>

1. 建立名為 `things-shadow-updates-rejected` 的 Amazon SNS 主題，並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為 ` sns-topic-arn `。

   如需如何建立 Amazon SNS 通知的詳細資訊，請參閱 [Amazon SNS 入門](https://docs.aws.amazon.com/sns/latest/dg/sns-getting-started.html)。

1. 建立警示。

   ```
   aws cloudwatch put-metric-alarm \
       --alarm-name UpdateThingShadowSuccessAlarm \
       --alarm-description "Alarm when my Things Shadow updates are getting rejected" \
       --namespace AWS/IoT \
       --metric-name UpdateThingShadow.Success \
       --dimensions Name=Protocol,Value=MQTT \
       --statistic Sum \
       --threshold 10 \
       --comparison-operator LessThanThreshold \
       --period 86400 \
       --unit Count \
       --evaluation-periods 1 \
       --alarm-actions sns-topic-arn
   ```

1. 測試警示。

   ```
   aws cloudwatch set-alarm-state --alarm-name UpdateThingShadowSuccessAlarm --state-reason "initializing" --state-value OK
   ```

   ```
   aws cloudwatch set-alarm-state --alarm-name UpdateThingShadowSuccessAlarm --state-reason "initializing" --state-value ALARM
   ```

1. 確認警示顯示於 [CloudWatch 主控台](https://console.aws.amazon.com/cloudwatch)中。

## 如何為任務建立 CloudWatch 警示？
<a name="cw-jobs-alarms"></a>

任務服務提供 CloudWatch 指標，以供您監控任務。您可以建立 CloudWatch 警示來監控任何 [任務指標](metrics_dimensions.md#jobs-metrics) 。

下列命令會建立 CloudWatch 警示，以監控任務 *SampleOTAJob* 的任務執行失敗次數總計，並會在超過 20 個任務執行失敗時，通知您。警示每 300 秒會檢查報告的值，以監控任務指標 `FailedJobExecutionTotalCount`。當單一報告值大於 20 時，便會啟動警示，表示自任務啟動以來，失敗的任務執行次數超過 20 次。當警示關閉時，會傳送通知給所提供的 Amazon SNS 主題。

```
aws cloudwatch put-metric-alarm \
    --alarm-name TotalFailedJobExecution-SampleOTAJob \
    --alarm-description "Alarm when total number of failed job execution exceeds the threshold for SampleOTAJob" \
    --namespace AWS/IoT \
    --metric-name FailedJobExecutionTotalCount \
    --dimensions Name=JobId,Value=SampleOTAJob \
    --statistic Sum \
    --threshold 20 \
    --comparison-operator GreaterThanThreshold \
    --period 300 \
    --unit Count \
    --evaluation-periods 1 \
    --alarm-actions arn:aws:sns:<AWS_REGION>:<AWS_ACCOUNT_ID>:SampleOTAJob-has-too-many-failed-job-ececutions
```

下列命令會建立 CloudWatch 警示，以監控在指定期間內任務 *SampleOTAJob* 失敗的任務執行次數。然後，當在此期間有超過五個任務執行失敗時，會通知您。警示每 3600 秒會檢查報告的值，以監控任務指標 `FailedJobExecutionCount`。當單一報告值大於 5 時，便會啟動警示，表示在過去一小時內，失敗的任務執行次數超過 5 次。當警示關閉時，會傳送通知給所提供的 Amazon SNS 主題。

```
aws cloudwatch put-metric-alarm \
    --alarm-name FailedJobExecution-SampleOTAJob \
    --alarm-description "Alarm when number of failed job execution per hour exceeds the threshold for SampleOTAJob" \
    --namespace AWS/IoT \
    --metric-name FailedJobExecutionCount \
    --dimensions Name=JobId,Value=SampleOTAJob \
    --statistic Sum \
    --threshold 5 \
    --comparison-operator GreaterThanThreshold \
    --period 3600 \
    --unit Count \
    --evaluation-periods 1 \
    --alarm-actions arn:aws:sns:<AWS_REGION>:<AWS_ACCOUNT_ID>:SampleOTAJob-has-too-many-failed-job-ececutions-per-hour
```