文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 的 CloudWatch 範例 AWS CLI
下列程式碼範例示範如何使用 AWS Command Line Interface with CloudWatch 來執行動作和實作常見案例。
Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然 動作會示範如何呼叫個別服務函數,但您可以在其相關案例中查看內容中的動作。
每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。
主題
動作
下列程式碼範例示範如何使用 delete-alarms
。
- AWS CLI
-
刪除警示
下列範例使用
delete-alarms
命令來刪除名為「myalarm」的 Amazon CloudWatch 警示:aws cloudwatch delete-alarms --alarm-names
myalarm
輸出:
This command returns to the prompt if successful.
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DeleteAlarms
。
-
下列程式碼範例示範如何使用 describe-alarm-history
。
- AWS CLI
-
若要擷取警示的歷史記錄
下列範例使用
describe-alarm-history
命令來擷取名為 "myalarm" 的 Amazon CloudWatch 警示歷史記錄:aws cloudwatch describe-alarm-history --alarm-name
"myalarm"
--history-item-typeStateUpdate
輸出:
{ "AlarmHistoryItems": [ { "Timestamp": "2014-04-09T18:59:06.442Z", "HistoryItemType": "StateUpdate", "AlarmName": "myalarm", "HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"testing purposes\"},\"newState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}}}", "HistorySummary": "Alarm updated from ALARM to OK" }, { "Timestamp": "2014-04-09T18:59:05.805Z", "HistoryItemType": "StateUpdate", "AlarmName": "myalarm", "HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.839999999999996, 39.714].\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2014-03-11T22:45:41.569+0000\",\"startDate\":\"2014-03-11T22:30:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.839999999999996,39.714],\"threshold\":70.0}},\"newState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"testing purposes\"}}", "HistorySummary": "Alarm updated from OK to ALARM" } ] }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeAlarmHistory
。
-
下列程式碼範例示範如何使用 describe-alarms-for-metric
。
- AWS CLI
-
顯示與指標相關聯的警示資訊
下列範例使用
describe-alarms-for-metric
命令來顯示與 Amazon EC2 CPUUtilization 指標和 ID 為 i-0c986c72 的執行個體相關聯的任何警示資訊:aws cloudwatch describe-alarms-for-metric --metric-name
CPUUtilization
--namespaceAWS/EC2
--dimensionsName=InstanceId,Value=i-0c986c72
輸出:
{ "MetricAlarms": [ { "EvaluationPeriods": 10, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm2", "StateUpdatedTimestamp": "2013-10-30T03:03:51.479Z", "AlarmConfigurationUpdatedTimestamp": "2013-10-30T03:03:50.865Z", "ComparisonOperator": "GreaterThanOrEqualToThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:NotifyMe" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2013-10-30T03:03:51.479+0000\",\"startDate\":\"2013-10-30T02:08:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[40.698,39.612,42.432,39.796,38.816,42.28,42.854,40.088,40.760000000000005,41.316],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm2", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 10 datapoints were not greater than or equal to the threshold (70.0). The most recent datapoints: [40.760000000000005, 41.316].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" }, { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:111122223333:alarm:myHighCpuAlarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2014-04-09T22:26:05.958Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:111122223333:HighCPUAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myHighCpuAlarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": false, "MetricName": "CPUUtilization" } ] }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeAlarmsForMetric
。
-
下列程式碼範例示範如何使用 describe-alarms
。
- AWS CLI
-
列出有關警示的資訊
下列範例使用
describe-alarms
命令提供名為 "myalarm" 的警示相關資訊:aws cloudwatch describe-alarms --alarm-names
"myalarm"
輸出:
{ "MetricAlarms": [ { "EvaluationPeriods": 2, "AlarmArn": "arn:aws:cloudwatch:us-east-1:123456789012:alarm:myalarm", "StateUpdatedTimestamp": "2014-04-09T18:59:06.442Z", "AlarmConfigurationUpdatedTimestamp": "2012-12-27T00:49:54.032Z", "ComparisonOperator": "GreaterThanThreshold", "AlarmActions": [ "arn:aws:sns:us-east-1:123456789012:myHighCpuAlarm" ], "Namespace": "AWS/EC2", "AlarmDescription": "CPU usage exceeds 70 percent", "StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2014-04-09T18:59:06.419+0000\",\"startDate\":\"2014-04-09T18:44:00.000+0000\",\"statistic\":\"Average\",\"period\":300,\"recentDatapoints\":[38.958,40.292],\"threshold\":70.0}", "Period": 300, "StateValue": "OK", "Threshold": 70.0, "AlarmName": "myalarm", "Dimensions": [ { "Name": "InstanceId", "Value": "i-0c986c72" } ], "Statistic": "Average", "StateReason": "Threshold Crossed: 2 datapoints were not greater than the threshold (70.0). The most recent datapoints: [38.958, 40.292].", "InsufficientDataActions": [], "OKActions": [], "ActionsEnabled": true, "MetricName": "CPUUtilization" } ] }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DescribeAlarms
。
-
下列程式碼範例示範如何使用 disable-alarm-actions
。
- AWS CLI
-
停用警示的動作
下列範例使用
disable-alarm-actions
命令來停用名為 myalarm 之警示的所有動作:aws cloudwatch disable-alarm-actions --alarm-names
myalarm
如果成功,此命令會回到提示字元。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DisableAlarmActions
。
-
下列程式碼範例示範如何使用 enable-alarm-actions
。
- AWS CLI
-
啟用警示的所有動作
下列範例使用
enable-alarm-actions
命令來啟用名為 myalarm 之警示的所有動作:aws cloudwatch enable-alarm-actions --alarm-names
myalarm
如果成功,此命令會回到提示字元。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 EnableAlarmActions
。
-
下列程式碼範例示範如何使用 get-metric-statistics
。
- AWS CLI
-
若要取得每個 CPU 執行個體的 EC2 使用率
下列範例使用
get-metric-statistics
命令來取得 ID 為 i-abcdef 的 Word 執行個體的 EC2 CPU使用率。aws cloudwatch get-metric-statistics --metric-name
CPUUtilization
--start-time2014-04-08T23:18:00Z
--end-time2014-04-09T23:18:00Z
--period3600
--namespaceAWS/EC2
--statisticsMaximum
--dimensionsName=InstanceId,Value=i-abcdef
輸出:
{ "Datapoints": [ { "Timestamp": "2014-04-09T11:18:00Z", "Maximum": 44.79, "Unit": "Percent" }, { "Timestamp": "2014-04-09T20:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T19:18:00Z", "Maximum": 50.85, "Unit": "Percent" }, { "Timestamp": "2014-04-09T09:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T03:18:00Z", "Maximum": 76.84, "Unit": "Percent" }, { "Timestamp": "2014-04-09T21:18:00Z", "Maximum": 48.96, "Unit": "Percent" }, { "Timestamp": "2014-04-09T14:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T08:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T16:18:00Z", "Maximum": 45.55, "Unit": "Percent" }, { "Timestamp": "2014-04-09T06:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T13:18:00Z", "Maximum": 45.08, "Unit": "Percent" }, { "Timestamp": "2014-04-09T05:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T18:18:00Z", "Maximum": 46.88, "Unit": "Percent" }, { "Timestamp": "2014-04-09T17:18:00Z", "Maximum": 52.08, "Unit": "Percent" }, { "Timestamp": "2014-04-09T07:18:00Z", "Maximum": 47.92, "Unit": "Percent" }, { "Timestamp": "2014-04-09T02:18:00Z", "Maximum": 51.23, "Unit": "Percent" }, { "Timestamp": "2014-04-09T12:18:00Z", "Maximum": 47.67, "Unit": "Percent" }, { "Timestamp": "2014-04-08T23:18:00Z", "Maximum": 46.88, "Unit": "Percent" }, { "Timestamp": "2014-04-09T10:18:00Z", "Maximum": 51.91, "Unit": "Percent" }, { "Timestamp": "2014-04-09T04:18:00Z", "Maximum": 47.13, "Unit": "Percent" }, { "Timestamp": "2014-04-09T15:18:00Z", "Maximum": 48.96, "Unit": "Percent" }, { "Timestamp": "2014-04-09T00:18:00Z", "Maximum": 48.16, "Unit": "Percent" }, { "Timestamp": "2014-04-09T01:18:00Z", "Maximum": 49.18, "Unit": "Percent" } ], "Label": "CPUUtilization" }
指定多個維度
下列範例說明如何指定多個維度。每個維度都指定為「名稱/值」對,名稱和值之間用逗號分隔。使用空格分隔多個維度。如果單一指標包含多個維度,則必須為每個定義的維度指定值。
如需使用
get-metric-statistics
命令的更多範例,請參閱 Amazon CloudWatch 開發人員指南中的取得指標的統計資料。aws cloudwatch get-metric-statistics --metric-name
Buffers
--namespaceMyNameSpace
--dimensionsName=InstanceID,Value=i-abcdef
Name=InstanceType,Value=m1.small
--start-time2016-10-15T04:00:00Z
--end-time2016-10-19T07:00:00Z
--statisticsAverage
--period60
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 GetMetricStatistics
。
-
下列程式碼範例示範如何使用 list-metrics
。
- AWS CLI
-
列出 Amazon SNS 的指標
下列
list-metrics
範例顯示 Amazon SNS 的指標。aws cloudwatch list-metrics \ --namespace
"AWS/SNS"
輸出:
{ "Metrics": [ { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "PublishSize" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "PublishSize" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfNotificationsFailed" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfNotificationsDelivered" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "NotifyMe" } ], "MetricName": "NumberOfMessagesPublished" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfMessagesPublished" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfNotificationsDelivered" }, { "Namespace": "AWS/SNS", "Dimensions": [ { "Name": "TopicName", "Value": "CFO" } ], "MetricName": "NumberOfNotificationsFailed" } ] }
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 ListMetrics
。
-
下列程式碼範例示範如何使用 put-metric-alarm
。
- AWS CLI
-
在 CPU 使用率超過 70% 時傳送 Amazon Simple Notification Service 電子郵件訊息
下列範例使用
put-metric-alarm
命令,在 CPU 使用率超過 70% 時傳送 Amazon Simple Notification Service 電子郵件訊息:aws cloudwatch put-metric-alarm --alarm-name
cpu-mon
--alarm-description"Alarm when CPU exceeds 70 percent"
--metric-nameCPUUtilization
--namespaceAWS/EC2
--statisticAverage
--period300
--threshold70
--comparison-operatorGreaterThanThreshold
--dimensions"Name=InstanceId,Value=i-12345678"
--evaluation-periods2
--alarm-actionsarn:aws:sns:us-east-1:111122223333:MyTopic
--unitPercent
如果成功,此命令會回到提示字元。如果已存在具有相同名稱的警示,將會被新警示所覆寫。
指定多個維度
下列範例說明如何指定多個維度。每個維度都指定為「名稱/值」對,名稱和值之間用逗號分隔。多個維度由空格分隔:
aws cloudwatch put-metric-alarm --alarm-name
"Default_Test_Alarm3"
--alarm-description"The default example alarm"
--namespace"CW EXAMPLE METRICS"
--metric-nameDefault_Test
--statisticAverage
--period60
--evaluation-periods3
--threshold50
--comparison-operatorGreaterThanOrEqualToThreshold
--dimensionsName=key1,Value=value1
Name=key2,Value=value2
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutMetricAlarm
。
-
下列程式碼範例示範如何使用 put-metric-data
。
- AWS CLI
-
將自訂指標發佈至 Amazon CloudWatch
下列範例使用
put-metric-data
命令將自訂指標發佈至 Amazon CloudWatch:aws cloudwatch put-metric-data --namespace
"Usage Metrics"
--metric-datafile://metric.json
指標本身的值會儲存在 JSON 檔案 中
metric.json
。以下是該檔案的內容:
[ { "MetricName": "New Posts", "Timestamp": "Wednesday, June 12, 2013 8:28:20 PM", "Value": 0.50, "Unit": "Count" } ]
如需詳細資訊,請參閱 Amazon CloudWatch 開發人員指南中的發佈自訂指標。
指定多個維度
下列範例說明如何指定多個維度。每個維度均指定為 Name=Value 對。使用逗號分隔多個維度:
aws cloudwatch put-metric-data --metric-name
Buffers
--namespaceMyNameSpace
--unitBytes
--value231434333
--dimensionsInstanceID=1-23456789,InstanceType=m1.small
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 PutMetricData
。
-
下列程式碼範例示範如何使用 set-alarm-state
。
- AWS CLI
-
暫時變更警示的狀態
下列範例使用
set-alarm-state
命令暫時變更名為 "myalarm" 的 Amazon CloudWatch 警示狀態,並將其設定為 ALARM 狀態以供測試之用:aws cloudwatch set-alarm-state --alarm-name
"myalarm"
--state-valueALARM
--state-reason"testing purposes"
如果成功,此命令會回到提示字元。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 SetAlarmState
。
-