

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建要监控的 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 Resource Name (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 Resource Name (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 Resource Name (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 警报以监控 Job 的失败任务执行总数，*SampleOTAJob*并在超过 20 个任务执行失败时通知您。警报通过每 300 秒检查报告值来监控 Jobs 指标 `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 警报，以监控 Job 在给定时间段内失败*SampleOTAJob*的任务执行次数。然后，在该时段内有超过 5 个任务执行失败时，它会通知您。警报通过每 3600 秒检查报告值来监控 Jobs 指标 `FailedJobExecutionCount`。它在单个报告值大于 5 时激活，这意味着过去 1 小时内失败的任务执行数超过了 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
```