本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
建立 CloudWatch 警示以監控AWS IoT
您可以建立 CloudWatch 警報,在警示變更狀態時傳送 Amazon SNS 訊息。警示會在您指定的期間,監看單一指標。當指標值在數個期間內超過指定的閾值時,會執行一或多個動作。此動作可為傳送至 Amazon SNS 主題或 Auto Scaling 政策的通知。警示僅會觸發持續狀態變更的動作。CloudWatch 警示不會只因處於特定狀態就觸發動作,狀態必須已遭變更且已維持了一段指定的時間。
下列主題說明使用 CloudWatch 警示的一些範例。
您可以在 看到 CloudWatch AWS IoT指標和維度 警示可以監控的所有指標。
如何每天都在我的物件連線失敗時收到通知?
-
建立名為
things-not-connecting-successfully的 Amazon SNS 主題,並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為。sns-topic-arn如需如何建立 Amazon SNS 通知的詳細資訊,請參閱 Amazon SNS 入門。
-
建立警示。
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-actionssns-topic-arn -
測試警示。
aws cloudwatch set-alarm-state --alarm-name ConnectSuccessAlarm --state-reason "initializing" --state-value OKaws cloudwatch set-alarm-state --alarm-name ConnectSuccessAlarm --state-reason "initializing" --state-value ALARM -
確認警示顯示於 CloudWatch 主控台
中。
如何每天都在我的物件並未推送資料時收到通知?
-
建立名為
things-not-publishing-data的 Amazon SNS 主題,並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為。sns-topic-arn如需如何建立 Amazon SNS 通知的詳細資訊,請參閱 Amazon SNS 入門。
-
建立警示。
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-actionssns-topic-arn -
測試警示。
aws cloudwatch set-alarm-state --alarm-name PublishInSuccessAlarm --state-reason "initializing" --state-value OKaws cloudwatch set-alarm-state --alarm-name PublishInSuccessAlarm --state-reason "initializing" --state-value ALARM -
確認警示顯示於 CloudWatch 主控台
中。
如何每天都在我的物件的影子更新遭拒時收到通知?
-
建立名為
things-shadow-updates-rejected的 Amazon SNS 主題,並記錄其 Amazon 資源名稱 (ARN)。此程序會將您主題的 ARN 稱為。sns-topic-arn如需如何建立 Amazon SNS 通知的詳細資訊,請參閱 Amazon SNS 入門。
-
建立警示。
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-actionssns-topic-arn -
測試警示。
aws cloudwatch set-alarm-state --alarm-name UpdateThingShadowSuccessAlarm --state-reason "initializing" --state-value OKaws cloudwatch set-alarm-state --alarm-name UpdateThingShadowSuccessAlarm --state-reason "initializing" --state-value ALARM -
確認警示顯示於 CloudWatch 主控台
中。
如何為任務建立 CloudWatch 警示?
任務服務提供 CloudWatch 指標,以供您監控任務。您可以建立 CloudWatch 警示來監控任何 任務指標 。
下列命令會建立 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