AWS CLIを利用してCloudWatchのアラームを設定してみます。
今回は、SQSを例として使い、SNSに通知を行います。
前提条件
CloudWatchへの権限
- CloudWatchに対してフル権限があること。
SQSへの権限
- SQSに対してフル権限があること。
AWS CLIのバージョン
以下のバージョンで動作確認済
- AWS CLI 1.10.26
aws --version
aws-cli/1.10.26 Python/2.7.11 Darwin/15.4.0 botocore/1.4.17
バージョンが古い場合は最新版に上げることをオススメします。
sudo -H /opt/local/bin/pip install -U awscli
- 準備
=======
0.1. リージョンの決定
利用するリージョンを決めます。 (カレントユーザが利用するカレントリージ
ョンも切り変わります。)
export AWS_DEFAULT_REGION='ap-northeast-1'
0.2. 変数の確認
プロファイルが想定のものになっていることを確認します。
aws configure list
Name Value Type Location
---- ----- ---- --------
profile admin-prjz-mbp13 env AWS_DEFAULT_PROFILE
access_key ****************XXXX shared-credentials-file
secret_key ****************XXXX shared-credentials-file
region ap-northeast-1 env AWS_DEFAULT_REGION
0.3. 通知トピックの指定
アラームの通知先トピックを指定します。
SNS_TOPIC_NAME="$(date +%Y%m%d)-handson-topic" \
&& echo ${SNS_TOPIC_NAME}
aws sns list-topics | grep ${SNS_TOPIC_NAME}
存在することを確認します。
"TopicArn": "arn:aws:sns:us-west-2:882762460436:20160509-handson-cloudwatch"
トピックがまだ存在しない場合は、以下の手順で作成してください。
- トピックの作成 http://qiita.com/tcsh/items/e05e21e7338668cbf3b8
- 通知先の登録 (e-mail) http://qiita.com/tcsh/items/ca81576b0018bfa32cbe
SNS_TOPIC_ARN=$( \
aws sns list-topics \
--query "Topics[?contains(TopicArn, \`${SNS_TOPIC_NAME}\`)].TopicArn" \
--output text \
) \
&& echo ${SNS_TOPIC_ARN}
arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:20160509-handson-cloudwatch-topic
0.4. キューの指定
対象となるキューを指定します。
存在しない場合は、以下の手順を行なってください。
- キューの作成 http://qiita.com/tcsh/items/a41ac99e41430c1d758c
- "1. キューの作成" だけ実施すれば本ハンズオンは実施可能です。
- 対象の指定
=============
通知先の指定
アラーム対象のトピックを指定
CWATCH_ALARM_TOPIC=${SNS_TOPIC_ARN}
Insufficient Data actionsの通知先
今回は割愛
OK actionsの通知先
今回は割愛
名前空間の指定
CloudWatchの名前空間を指定します。
CWATCH_NAMESPACE='AWS/SQS'
ディメンションの指定
CloudWatchのディメンションを指定します。
SQSのディメンションはキュー名の1つしかありません。
CWATCH_DIMENSIONS="Name=QueueName,Value=${SQS_QUEUE_NAME}" \
&& echo ${CWATCH_DIMENSIONS}
- メトリックの一覧
===================
指定した名前空間の指定したディメンションで利用できるメトリックの一覧を表示してみましょう。
aws cloudwatch list-metrics \
--namespace ${CWATCH_NAMESPACE} \
--dimensions ${CWATCH_DIMENSIONS}
キューの作成後、一覧表示されるまで15分ほど必要になるようです。
{
"Metrics": [
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "SentMessageSize"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "NumberOfMessagesSent"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "ApproximateNumberOfMessagesVisible"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "ApproximateNumberOfMessagesNotVisible"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "ApproximateNumberOfMessagesDelayed"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "NumberOfMessagesDeleted"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "NumberOfEmptyReceives"
},
{
"Namespace": "AWS/SQS",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"MetricName": "NumberOfMessagesReceived"
}
]
}
- アラームの設定
=================
では、SQSのメトリック情報に変化があった場合にアラームがあがるように設定していきます。
3.1. 監視周期の決定
まず、監視周期を決めます。
SQSでは、5分(300秒)毎のmetricデータ更新ですが、本ハンズオンでははやめにアラーム通知が欲しいので60秒に指定します。
CWATCH_ALARM_PERIOD='60'
3.2. アラーム対象の決定
アラームの対象となる、metric名(必須)とUnit単位(任意)を指定します。
CWATCH_METRIC_NAME='ApproximateNumberOfMessagesVisible'
CWATCH_METRIC_UNIT='Count'
CWATCH_STATISTICS='Average'
3.3. アラーム名の決定
アラーム名を決定します。
可能な限りDescriptionも指定するようにしましょう。
CWATCH_ALARM_NAME='visible-test'
CWATCH_ALARM_DESC='Test Alarm for SQS Visible'
3.4. アラーム閾値の決定
アラームの閾値を指定します。
- evaluation period: 2回(period)状態が継続した場合にアラームを通知します。
- 閾値と比較演算子: キューにジョブが1つ以上(GreaterThanOrEqualToThreshold)あるとアラームを通知します。
CWATCH_EVALUATION_PERIOD='1'
CWATCH_THRESHOLD='1'
CWATCH_COMPARISON='GreaterThanOrEqualToThreshold'
3.5. アラームの設定
実際にアラームを設定してみましょう。
cat << ETX
CWATCH_NAMESPACE: ${CWATCH_NAMESPACE}
CWATCH_METRIC_NAME: ${CWATCH_METRIC_NAME}
CWATCH_METRIC_UNIT: ${CWATCH_METRIC_UNIT}
CWATCH_STATISTICS: ${CWATCH_STATISTICS}
CWATCH_DIMENSIONS: ${CWATCH_DIMENSIONS}
CWATCH_ALARM_PERIOD: ${CWATCH_ALARM_PERIOD}
CWATCH_EVALUATION_PERIOD: ${CWATCH_EVALUATION_PERIOD}
CWATCH_THRESHOLD: ${CWATCH_THRESHOLD}
CWATCH_COMPARISON: ${CWATCH_COMPARISON}
CWATCH_ALARM_NAME: ${CWATCH_ALARM_NAME}
CWATCH_ALARM_DESC: "${CWATCH_ALARM_DESC}"
CWATCH_ALARM_TOPIC: ${CWATCH_ALARM_TOPIC}
ETX
aws cloudwatch put-metric-alarm \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--unit ${CWATCH_METRIC_UNIT} \
--statistic ${CWATCH_STATISTICS} \
--dimensions ${CWATCH_DIMENSIONS} \
--period ${CWATCH_ALARM_PERIOD} \
--evaluation-periods ${CWATCH_EVALUATION_PERIOD} \
--threshold ${CWATCH_THRESHOLD} \
--comparison-operator ${CWATCH_COMPARISON} \
--alarm-name ${CWATCH_ALARM_NAME} \
--alarm-description "${CWATCH_ALARM_DESC}" \
--alarm-actions ${CWATCH_ALARM_TOPIC}
(戻り値なし)
3.6. アラーム変更履歴の確認
アラーム変更履歴に、以下の2つのエントリがあることを確認します。
- アラーム作成
- アラーム状態の変更 (Insufficient Data > OK)
CWATCH_MAX_ITEMS='2'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T02:24:05.584Z",
"HistoryItemType": "StateUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"INSUFFICIENT_DATA\",\"stateReason\":\"Unchecked: Initial alarm creation\"},\"newState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T02:24:05.571+0000\",\"startDate\":\"2015-01-05T02:20:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}}}",
"HistorySummary": "Alarm updated from INSUFFICIENT_DATA to OK"
},
{
"Timestamp": "2015-01-05T02:24:04.968Z",
"HistoryItemType": "ConfigurationUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"type\":\"Create\",\"createdAlarm\":{\"namespace\":\"AWS/SQS\",\"threshold\":1.0,\"unit\":\"Count\",\"stateValue\":\"INSUFFICIENT_DATA\",\"metricName\":\"ApproximateNumberOfMessagesVisible\",\"period\":60,\"dimensions\":[{\"name\":\"QueueName\",\"value\":\"example-queue\"}],\"evaluationPeriods\":1,\"comparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"actionsEnabled\":true,\"alarmName\":\"visible-test\",\"okactions\":[],\"alarmActions\":[\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\"],\"alarmArn\":\"arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test\",\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T02:24:04.968+0000\",\"stateUpdatedTimestamp\":\"2015-01-05T02:24:04.968+0000\",\"statistic\":\"Average\",\"insufficientDataActions\":[],\"alarmDescription\":\"Test Alert for SQS Visible\"}}",
"HistorySummary": "Alarm \"visible-test\" created"
}
]
}
3.7. アラーム設定の確認
アラームの設定内容を確認します。
aws cloudwatch describe-alarms \
--alarm-names ${CWATCH_ALARM_NAME}
{
"MetricAlarms": [
{
"EvaluationPeriods": 1,
"AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test",
"StateUpdatedTimestamp": "2015-01-05T02:24:05.584Z",
"AlarmConfigurationUpdatedTimestamp": "2015-01-05T02:24:04.968Z",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [
"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
],
"Namespace": "AWS/SQS",
"AlarmDescription": "Test Alert for SQS Visible",
"StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T02:24:05.571+0000\",\"startDate\":\"2015-01-05T02:20:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}",
"Period": 60,
"StateValue": "OK",
"Threshold": 1.0,
"AlarmName": "visible-test",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"Unit": "Count",
"Statistic": "Average",
"StateReason": "Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).",
"InsufficientDataActions": [],
"OKActions": [],
"ActionsEnabled": true,
"MetricName": "ApproximateNumberOfMessagesVisible"
}
]
}
以下を確認しておきましょう。
- StateValueが"OK"になっていること。
- StateReasonが"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0)."になっていること。
- アラームテスト
=================
アラームの設定をしたら、実際に通知が行なわれるかテストをしましょう。
4.1. ステータス変更
今回は、ステータスがALARMになった場合のみ通知を行うように設定しているので、以下のようにステータスの値としてALARMを指定します。
CWATCH_STATE_VALUE='ALARM'
ステータスが変更になった理由を指定します。
CWATCH_STATE_REASON='test alarm.'
実際にステータスを変更してみましょう。
cat << ETX
CWATCH_ALARM_NAME: ${CWATCH_ALARM_NAME}
CWATCH_STATE_VALUE: ${CWATCH_STATE_VALUE}
CWATCH_STATE_REASON: "${CWATCH_STATE_REASON}"
ETX
aws cloudwatch set-alarm-state \
--alarm-name ${CWATCH_ALARM_NAME} \
--state-value ${CWATCH_STATE_VALUE} \
--state-reason "${CWATCH_STATE_REASON}"
(戻り値なし)
4.2. 受信確認
ステータス変更後すぐに、SNSトピック経由で以下のようなアラームメールが着信しているはずです。
no-reply@sns.amazonaws.com
ALARM: "visible-test" in APAC - Tokyo
You are receiving this email because your Amazon CloudWatch Alarm "visible-test" in the APAC - Tokyo region has entered the ALARM state, because "test alarm." at "Monday 05 January, 2015 03:05:31 UTC".
View this alarm in the AWS Management Console:
https://console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#s=Alarms&alarm=visible-test
Alarm Details:
- Name: visible-test
- Description: Test Alarm for SQS Visible
- State Change: OK -> ALARM
- Reason for State Change: test alarm.
- Timestamp: Monday 05 January, 2015 03:05:31 UTC
- AWS Account: XXXXXXXXXXXX
Threshold:
- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 60 seconds.
Monitored Metric:
- MetricNamespace: AWS/SQS
- MetricName: ApproximateNumberOfMessagesVisible
- Dimensions: [QueueName = example-queue]
- Period: 60 seconds
- Statistic: Average
- Unit: Count
State Change Actions:
- OK:
- ALARM: [arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-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:example-topic:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx&Endpoint=taro@example.jp
Please do not reply directly to this e-mail. If you have any questions or comments regarding this email, please contact us at https://aws.amazon.com/support
4.3. アラーム変更履歴の確認
アラーム変更履歴に、以下の3つのエントリがあることを確認します。
- "HistoryItemType"が"StateUpdate"で、HistorySummaryが"Alarm updated from OK to ALARM"のエントリ
- "HistoryItemType"が"Action"のエントリ
- "HistoryItemType"が"StateUpdate"で、HistorySummaryが"Alarm updated from ALARM to OK"のエントリ
CWATCH_MAX_ITEMS='3'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T03:05:32.619Z",
"HistoryItemType": "StateUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"test alarm.\"},\"newState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T03:05:32.607+0000\",\"startDate\":\"2015-01-05T03:00:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}}}",
"HistorySummary": "Alarm updated from ALARM to OK"
},
{
"Timestamp": "2015-01-05T03:05:31.786Z",
"HistoryItemType": "Action",
"AlarmName": "visible-test",
"HistoryData": "{\"actionState\":\"Succeeded\",\"notificationResource\":\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\",\"stateUpdateTimestamp\":\"2015-01-05T03:05:31.775+0000\",\"publishedMessage\":\"{\\\"default\\\":\\\"{\\\\\\\"AlarmName\\\\\\\":\\\\\\\"visible-test\\\\\\\",\\\\\\\"AlarmDescription\\\\\\\":\\\\\\\"Test Alert for SQS Visible\\\\\\\",\\\\\\\"AWSAccountId\\\\\\\":\\\\\\\"XXXXXXXXXXXX\\\\\\\",\\\\\\\"NewStateValue\\\\\\\":\\\\\\\"ALARM\\\\\\\",\\\\\\\"NewStateReason\\\\\\\":\\\\\\\"test alarm.\\\\\\\",\\\\\\\"StateChangeTime\\\\\\\":\\\\\\\"2015-01-05T03:05:31.775+0000\\\\\\\",\\\\\\\"Region\\\\\\\":\\\\\\\"APAC - Tokyo\\\\\\\",\\\\\\\"OldStateValue\\\\\\\":\\\\\\\"OK\\\\\\\",\\\\\\\"Trigger\\\\\\\":{\\\\\\\"MetricName\\\\\\\":\\\\\\\"ApproximateNumberOfMessagesVisible\\\\\\\",\\\\\\\"Namespace\\\\\\\":\\\\\\\"AWS/SQS\\\\\\\",\\\\\\\"Statistic\\\\\\\":\\\\\\\"AVERAGE\\\\\\\",\\\\\\\"Unit\\\\\\\":\\\\\\\"Count\\\\\\\",\\\\\\\"Dimensions\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"QueueName\\\\\\\",\\\\\\\"value\\\\\\\":\\\\\\\"example-queue\\\\\\\"}],\\\\\\\"Period\\\\\\\":60,\\\\\\\"EvaluationPeriods\\\\\\\":1,\\\\\\\"ComparisonOperator\\\\\\\":\\\\\\\"GreaterThanOrEqualToThreshold\\\\\\\",\\\\\\\"Threshold\\\\\\\":1.0}}\\\",\\\"sms\\\":\\\"ALARM: \\\\\\\"visible-test\\\\\\\" in APAC - Tokyo\\\",\\\"email\\\":\\\"You are receiving this email because your Amazon CloudWatch Alarm \\\\\\\"visible-test\\\\\\\" in the APAC - Tokyo region has entered the ALARM state, because \\\\\\\"test alarm.\\\\\\\" at \\\\\\\"Monday 05 January, 2015 03:05:31 UTC\\\\\\\".\\\\n\\\\nView this alarm in the AWS Management Console:\\\\nhttps://console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#s=Alarms&alarm=visible-test\\\\n\\\\nAlarm Details:\\\\n- Name: visible-test\\\\n- Description: Test Alert for SQS Visible\\\\n- State Change: OK -> ALARM\\\\n- Reason for State Change: test alarm.\\\\n- Timestamp: Monday 05 January, 2015 03:05:31 UTC\\\\n- AWS Account: XXXXXXXXXXXX\\\\n\\\\nThreshold:\\\\n- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 60 seconds. \\\\n\\\\nMonitored Metric:\\\\n- MetricNamespace: AWS/SQS\\\\n- MetricName: ApproximateNumberOfMessagesVisible\\\\n- Dimensions: [QueueName = example-queue]\\\\n- Period: 60 seconds\\\\n- Statistic: Average\\\\n- Unit: Count\\\\n\\\\nState Change Actions:\\\\n- OK: \\\\n- ALARM: [arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic]\\\\n- INSUFFICIENT_DATA: \\\\n\\\"}\"}",
"HistorySummary": "Successfully executed action arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
},
{
"Timestamp": "2015-01-05T03:05:31.775Z",
"HistoryItemType": "StateUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T02:24:05.571+0000\",\"startDate\":\"2015-01-05T02:20:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}},\"newState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"test alarm.\"}}",
"HistorySummary": "Alarm updated from OK to ALARM"
}
],
"NextToken": "None___3"
}
- 統計情報の範囲、周期の決定
=============================
5.1. 統計周期の決定
統計情報の取得単位(周期)を秒単位で指定します。
ここでは5分(300秒)周期にしてみます。
CWATCH_STAT_PERIOD='300'
Note: 最小値とデフォルト値は60 (60の倍数である必要があります。)
5.2. 統計期間の決定
統計情報の対象期間を指定します。
ここでは、24時間(1440分)前から現在を対象にしてみます。
CWATCH_END_TIME=$( \
date -u '+%FT%TZ' \
) \
&& echo ${CWATCH_END_TIME}
TIME_BEFORE_MIN='1440'
CWATCH_START_TIME=$( \
date -u -v-${TIME_BEFORE_MIN}M '+%FT%TZ' \
) \
&& echo ${CWATCH_START_TIME}
CWATCH_START_TIME=`date -u -d "${TIME_BEFORE_MIN} mins ago" '+%FT%TZ'` \
&& echo ${CWATCH_START_TIME}
5.3. 統計情報の確認
実際のメトリックの値を確認してみましょう。
CWATCH_METRIC_NAME='ApproximateNumberOfMessagesVisible'
CWATCH_METRIC_UNIT='Count'
aws cloudwatch get-metric-statistics \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--start-time ${CWATCH_START_TIME} \
--end-time ${CWATCH_END_TIME} \
--period ${CWATCH_STAT_PERIOD} \
--statistics ${CWATCH_STATISTICS} \
--dimensions ${CWATCH_DIMENSIONS} \
--output text | \
sort -k3
DATAPOINTS 0.0 2015-01-05T02:04:00Z Count
DATAPOINTS 0.0 2015-01-05T02:09:00Z Count
DATAPOINTS 0.0 2015-01-05T02:14:00Z Count
DATAPOINTS 0.0 2015-01-05T02:19:00Z Count
DATAPOINTS 0.0 2015-01-05T02:24:00Z Count
DATAPOINTS 0.0 2015-01-05T02:29:00Z Count
DATAPOINTS 0.0 2015-01-05T02:34:00Z Count
DATAPOINTS 0.0 2015-01-05T02:39:00Z Count
DATAPOINTS 0.0 2015-01-05T02:44:00Z Count
DATAPOINTS 0.0 2015-01-05T02:49:00Z Count
DATAPOINTS 0.0 2015-01-05T02:54:00Z Count
DATAPOINTS 0.0 2015-01-05T02:59:00Z Count
まだ、キューにはジョブがはいっていないので、countは0.0になっています。
- キューへの送信
=================
6.1. メッセージの送信 (依頼側)
6.2. アラームの状態確認
アラームのステータスが変化するのを待ちます。
(最大5分程度かかります)
aws cloudwatch describe-alarms \
--alarm-names ${CWATCH_ALARM_NAME}
{
"MetricAlarms": [
{
"EvaluationPeriods": 1,
"AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test",
"StateUpdatedTimestamp": "2015-01-05T03:31:52.171Z",
"AlarmConfigurationUpdatedTimestamp": "2015-01-05T02:24:04.968Z",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [
"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
],
"Namespace": "AWS/SQS",
"AlarmDescription": "Test Alert for SQS Visible",
"StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T03:31:52.163+0000\",\"startDate\":\"2015-01-05T03:30:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[1.0],\"threshold\":1.0}",
"Period": 60,
"StateValue": "ALARM",
"Threshold": 1.0,
"AlarmName": "visible-test",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"Unit": "Count",
"Statistic": "Average",
"StateReason": "Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).",
"InsufficientDataActions": [],
"OKActions": [],
"ActionsEnabled": true,
"MetricName": "ApproximateNumberOfMessagesVisible"
}
]
}
"StateValue"が"ALARM"になるはずです。
6.3. アラーム着信確認
先程と同様に、SNSトピック経由でアラームメールが着信しているはずです。
6.4. アラーム変更履歴の確認
アラーム変更履歴に、以下の2つのエントリがあることを確認します。
- "HistoryItemType"が"StateUpdate"で、HistorySummaryが"Alarm updated from OK to ALARM"のエントリ
- "HistoryItemType"が"Action"のエントリ
今回は、キューのジョブを削除するまで、"StateValue"は"OK"にはなりません。
CWATCH_MAX_ITEMS='2'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T03:31:52.181Z",
"HistoryItemType": "Action",
"AlarmName": "visible-test",
"HistoryData": "{\"actionState\":\"Succeeded\",\"notificationResource\":\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\",\"stateUpdateTimestamp\":\"2015-01-05T03:31:52.171+0000\",\"publishedMessage\":\"{\\\"default\\\":\\\"{\\\\\\\"AlarmName\\\\\\\":\\\\\\\"visible-test\\\\\\\",\\\\\\\"AlarmDescription\\\\\\\":\\\\\\\"Test Alert for SQS Visible\\\\\\\",\\\\\\\"AWSAccountId\\\\\\\":\\\\\\\"XXXXXXXXXXXX\\\\\\\",\\\\\\\"NewStateValue\\\\\\\":\\\\\\\"ALARM\\\\\\\",\\\\\\\"NewStateReason\\\\\\\":\\\\\\\"Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).\\\\\\\",\\\\\\\"StateChangeTime\\\\\\\":\\\\\\\"2015-01-05T03:31:52.171+0000\\\\\\\",\\\\\\\"Region\\\\\\\":\\\\\\\"APAC - Tokyo\\\\\\\",\\\\\\\"OldStateValue\\\\\\\":\\\\\\\"OK\\\\\\\",\\\\\\\"Trigger\\\\\\\":{\\\\\\\"MetricName\\\\\\\":\\\\\\\"ApproximateNumberOfMessagesVisible\\\\\\\",\\\\\\\"Namespace\\\\\\\":\\\\\\\"AWS/SQS\\\\\\\",\\\\\\\"Statistic\\\\\\\":\\\\\\\"AVERAGE\\\\\\\",\\\\\\\"Unit\\\\\\\":\\\\\\\"Count\\\\\\\",\\\\\\\"Dimensions\\\\\\\":[{\\\\\\\"name\\\\\\\":\\\\\\\"QueueName\\\\\\\",\\\\\\\"value\\\\\\\":\\\\\\\"example-queue\\\\\\\"}],\\\\\\\"Period\\\\\\\":60,\\\\\\\"EvaluationPeriods\\\\\\\":1,\\\\\\\"ComparisonOperator\\\\\\\":\\\\\\\"GreaterThanOrEqualToThreshold\\\\\\\",\\\\\\\"Threshold\\\\\\\":1.0}}\\\",\\\"sms\\\":\\\"ALARM: \\\\\\\"visible-test\\\\\\\" in APAC - Tokyo\\\",\\\"email\\\":\\\"You are receiving this email because your Amazon CloudWatch Alarm \\\\\\\"visible-test\\\\\\\" in the APAC - Tokyo region has entered the ALARM state, because \\\\\\\"Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).\\\\\\\" at \\\\\\\"Monday 05 January, 2015 03:31:52 UTC\\\\\\\".\\\\n\\\\nView this alarm in the AWS Management Console:\\\\nhttps://console.aws.amazon.com/cloudwatch/home?region=ap-northeast-1#s=Alarms&alarm=visible-test\\\\n\\\\nAlarm Details:\\\\n- Name: visible-test\\\\n- Description: Test Alert for SQS Visible\\\\n- State Change: OK -> ALARM\\\\n- Reason for State Change: Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).\\\\n- Timestamp: Monday 05 January, 2015 03:31:52 UTC\\\\n- AWS Account: XXXXXXXXXXXX\\\\n\\\\nThreshold:\\\\n- The alarm is in the ALARM state when the metric is GreaterThanOrEqualToThreshold 1.0 for 60 seconds. \\\\n\\\\nMonitored Metric:\\\\n- MetricNamespace: AWS/SQS\\\\n- MetricName: ApproximateNumberOfMessagesVisible\\\\n- Dimensions: [QueueName = example-queue]\\\\n- Period: 60 seconds\\\\n- Statistic: Average\\\\n- Unit: Count\\\\n\\\\nState Change Actions:\\\\n- OK: \\\\n- ALARM: [arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic]\\\\n- INSUFFICIENT_DATA: \\\\n\\\"}\"}",
"HistorySummary": "Successfully executed action arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
},
{
"Timestamp": "2015-01-05T03:31:52.171Z",
"HistoryItemType": "StateUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T03:05:32.607+0000\",\"startDate\":\"2015-01-05T03:00:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}},\"newState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T03:31:52.163+0000\",\"startDate\":\"2015-01-05T03:30:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[1.0],\"threshold\":1.0}}}",
"HistorySummary": "Alarm updated from OK to ALARM"
}
],
"NextToken": "None___2"
}
6.5. 統計情報の確認
実際のメトリックの値を確認してみましょう。
まず、現在時刻を更新しておきます。
CWATCH_END_TIME=$( date -u '+%FT%TZ') \
&& echo ${CWATCH_END_TIME}
aws cloudwatch get-metric-statistics \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--start-time ${CWATCH_START_TIME} \
--end-time ${CWATCH_END_TIME} \
--period ${CWATCH_STAT_PERIOD} \
--statistics ${CWATCH_STATISTICS} \
--dimensions ${CWATCH_DIMENSIONS} \
--output text | \
sort -k3
DATAPOINTS 0.0 2015-01-05T02:04:00Z Count
DATAPOINTS 0.0 2015-01-05T02:09:00Z Count
DATAPOINTS 0.0 2015-01-05T02:14:00Z Count
DATAPOINTS 0.0 2015-01-05T02:19:00Z Count
DATAPOINTS 0.0 2015-01-05T02:24:00Z Count
DATAPOINTS 0.0 2015-01-05T02:29:00Z Count
DATAPOINTS 0.0 2015-01-05T02:34:00Z Count
DATAPOINTS 0.0 2015-01-05T02:39:00Z Count
DATAPOINTS 0.0 2015-01-05T02:44:00Z Count
DATAPOINTS 0.0 2015-01-05T02:49:00Z Count
DATAPOINTS 0.0 2015-01-05T02:54:00Z Count
DATAPOINTS 0.0 2015-01-05T02:59:00Z Count
DATAPOINTS 1.0 2015-01-05T03:04:00Z Count
最新のCountが1.0になっているはずです。
- キューの受信
===============
キューのメッセージを受信し、削除してみます。
7.1. メッセージの受信 (処理側)
7.2. アラームの状態確認
アラームのステータスが変化するのを待ちます。
(最大5分程度かかります)
aws cloudwatch describe-alarms \
--alarm-names ${CWATCH_ALARM_NAME}
{
"MetricAlarms": [
{
"EvaluationPeriods": 1,
"AlarmArn": "arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test",
"StateUpdatedTimestamp": "2015-01-05T02:24:05.584Z",
"AlarmConfigurationUpdatedTimestamp": "2015-01-05T02:24:04.968Z",
"ComparisonOperator": "GreaterThanOrEqualToThreshold",
"AlarmActions": [
"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic"
],
"Namespace": "AWS/SQS",
"AlarmDescription": "Test Alert for SQS Visible",
"StateReasonData": "{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T02:24:05.571+0000\",\"startDate\":\"2015-01-05T02:20:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}",
"Period": 60,
"StateValue": "OK",
"Threshold": 1.0,
"AlarmName": "visible-test",
"Dimensions": [
{
"Name": "QueueName",
"Value": "example-queue"
}
],
"Unit": "Count",
"Statistic": "Average",
"StateReason": "Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).",
"InsufficientDataActions": [],
"OKActions": [],
"ActionsEnabled": true,
"MetricName": "ApproximateNumberOfMessagesVisible"
}
]
}
7.3. アラーム変更履歴の確認
アラーム変更履歴に、以下の1つのエントリがあることを確認します。
- "HistoryItemType"が"StateUpdate"で、HistorySummaryが"Alarm updated from ALARM to OK"のエントリ
CWATCH_MAX_ITEMS='1'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T05:11:52.179Z",
"HistoryItemType": "StateUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"oldState\":{\"stateValue\":\"ALARM\",\"stateReason\":\"Threshold Crossed: 1 datapoint (1.0) was greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T03:31:52.163+0000\",\"startDate\":\"2015-01-05T03:30:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[1.0],\"threshold\":1.0}},\"newState\":{\"stateValue\":\"OK\",\"stateReason\":\"Threshold Crossed: 1 datapoint (0.0) was not greater than or equal to the threshold (1.0).\",\"stateReasonData\":{\"version\":\"1.0\",\"queryDate\":\"2015-01-05T05:11:52.174+0000\",\"startDate\":\"2015-01-05T05:10:00.000+0000\",\"unit\":\"Count\",\"statistic\":\"Average\",\"period\":60,\"recentDatapoints\":[0.0],\"threshold\":1.0}}}",
"HistorySummary": "Alarm updated from ALARM to OK"
}
],
"NextToken": "None___1"
}
7.4. 統計情報の確認
実際のメトリックの値を確認してみましょう。
まず、現在時刻を更新しておきます。
CWATCH_END_TIME=$(date -u '+%FT%TZ' \
) \
&& echo ${CWATCH_END_TIME}
aws cloudwatch get-metric-statistics \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--start-time ${CWATCH_START_TIME} \
--end-time ${CWATCH_END_TIME} \
--period ${CWATCH_STAT_PERIOD} \
--statistics ${CWATCH_STATISTICS} \
--dimensions ${CWATCH_DIMENSIONS} \
--output text | \
sort -k3
DATAPOINTS 0.0 2015-01-05T02:04:00Z Count
DATAPOINTS 0.0 2015-01-05T02:09:00Z Count
DATAPOINTS 0.0 2015-01-05T02:14:00Z Count
DATAPOINTS 0.0 2015-01-05T02:19:00Z Count
DATAPOINTS 0.0 2015-01-05T02:24:00Z Count
DATAPOINTS 0.0 2015-01-05T02:29:00Z Count
DATAPOINTS 0.0 2015-01-05T02:34:00Z Count
DATAPOINTS 0.0 2015-01-05T02:39:00Z Count
DATAPOINTS 0.0 2015-01-05T02:44:00Z Count
DATAPOINTS 0.0 2015-01-05T02:49:00Z Count
DATAPOINTS 0.0 2015-01-05T02:54:00Z Count
DATAPOINTS 0.0 2015-01-05T02:59:00Z Count
DATAPOINTS 1.0 2015-01-05T03:04:00Z Count
DATAPOINTS 0.0 2015-01-05T03:09:00Z Count
- アラームアクションの停止 (おまけ)
===================================
8.1. 現状の確認
aws cloudwatch describe-alarms-for-metric \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--dimensions ${CWATCH_DIMENSIONS} \
--query 'MetricAlarms[].ActionsEnabled' \
--output text
True
8.2. アラームアクションの停止
aws cloudwatch disable-alarm-actions \
--alarm-names ${CWATCH_ALARM_NAME}
(戻り値なし)
8.3. 変更後の確認
aws cloudwatch describe-alarms-for-metric \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--dimensions ${CWATCH_DIMENSIONS} \
--query 'MetricAlarms[].ActionsEnabled' \
--output text
False
8.4. アラーム変更履歴の確認
CWATCH_MAX_ITEMS='1'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T05:42:30.909Z",
"HistoryItemType": "ConfigurationUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"type\":\"Update\",\"updatedAlarm\":{\"namespace\":\"AWS/SQS\",\"unit\":\"Count\",\"stateValue\":\"OK\",\"metricName\":\"ApproximateNumberOfMessagesVisible\",\"period\":60,\"dimensions\":[{\"name\":\"QueueName\",\"value\":\"example-queue\"}],\"threshold\":1.0,\"alarmName\":\"visible-test\",\"evaluationPeriods\":1,\"comparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"actionsEnabled\":false,\"okactions\":[],\"alarmActions\":[\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\"],\"alarmArn\":\"arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test\",\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T05:42:30.909+0000\",\"stateUpdatedTimestamp\":\"2015-01-05T05:11:52.179+0000\",\"alarmDescription\":\"Test Alert for SQS Visible\",\"insufficientDataActions\":[],\"statistic\":\"Average\"},\"originalUpdatedFields\":{\"actionsEnabled\":true,\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T02:24:04.968+0000\"}}",
"HistorySummary": "Alarm \"visible-test\" updated"
}
],
"NextToken": "None___1"
}
- アラームアクションの再開 (おまけ)
===================================
9.1. アラームアクションの再開
aws cloudwatch enable-alarm-actions \
--alarm-names ${CWATCH_ALARM_NAME}
(戻り値なし)
9.2. 変更後の確認
aws cloudwatch describe-alarms-for-metric \
--namespace ${CWATCH_NAMESPACE} \
--metric-name ${CWATCH_METRIC_NAME} \
--dimensions ${CWATCH_DIMENSIONS} \
--query 'MetricAlarms[].ActionsEnabled' \
--output text
True
9.3. アラーム変更履歴の確認
CWATCH_MAX_ITEMS='1'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T06:11:30.824Z",
"HistoryItemType": "ConfigurationUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"type\":\"Update\",\"updatedAlarm\":{\"namespace\":\"AWS/SQS\",\"threshold\":1.0,\"alarmActions\":[\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\"],\"actionsEnabled\":true,\"okactions\":[],\"alarmName\":\"visible-test\",\"evaluationPeriods\":1,\"comparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"unit\":\"Count\",\"stateValue\":\"OK\",\"metricName\":\"ApproximateNumberOfMessagesVisible\",\"period\":60,\"dimensions\":[{\"name\":\"QueueName\",\"value\":\"example-queue\"}],\"alarmArn\":\"arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test\",\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T06:11:30.824+0000\",\"insufficientDataActions\":[],\"statistic\":\"Average\",\"alarmDescription\":\"Test Alert for SQS Visible\",\"stateUpdatedTimestamp\":\"2015-01-05T05:11:52.179+0000\"},\"originalUpdatedFields\":{\"actionsEnabled\":false,\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T05:42:30.909+0000\"}}",
"HistorySummary": "Alarm \"visible-test\" updated"
}
],
"NextToken": "None___1"
}
- アラームの削除
==================
10.1. アラームの削除
アラームを削除します。
aws cloudwatch delete-alarms \
--alarm-names ${CWATCH_ALARM_NAME}
(戻り値なし)
10.2. アラーム変更履歴の確認
アラーム変更履歴に、以下の1つのエントリがあることを確認します。
- "HistoryItemType"が"ConfigurationUpdate"で、HistorySummaryが"HistorySummary": "Alarm "visible-test" deleted"のエントリ
CWATCH_MAX_ITEMS='1'
aws cloudwatch describe-alarm-history \
--alarm-name ${CWATCH_ALARM_NAME} \
--max-items ${CWATCH_MAX_ITEMS}
{
"AlarmHistoryItems": [
{
"Timestamp": "2015-01-05T06:16:01.319Z",
"HistoryItemType": "ConfigurationUpdate",
"AlarmName": "visible-test",
"HistoryData": "{\"version\":\"1.0\",\"type\":\"Delete\",\"deletedAlarm\":{\"threshold\":1.0,\"namespace\":\"AWS/SQS\",\"unit\":\"Count\",\"stateValue\":\"OK\",\"metricName\":\"ApproximateNumberOfMessagesVisible\",\"period\":60,\"dimensions\":[{\"name\":\"QueueName\",\"value\":\"example-queue\"}],\"okactions\":[],\"alarmActions\":[\"arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:example-topic\"],\"evaluationPeriods\":1,\"comparisonOperator\":\"GreaterThanOrEqualToThreshold\",\"alarmName\":\"visible-test\",\"actionsEnabled\":true,\"statistic\":\"Average\",\"insufficientDataActions\":[],\"stateUpdatedTimestamp\":\"2015-01-05T05:11:52.179+0000\",\"alarmArn\":\"arn:aws:cloudwatch:ap-northeast-1:XXXXXXXXXXXX:alarm:visible-test\",\"alarmConfigurationUpdatedTimestamp\":\"2015-01-05T06:11:30.824+0000\",\"alarmDescription\":\"Test Alert for SQS Visible\"}}",
"HistorySummary": "Alarm \"visible-test\" deleted"
}
],
"NextToken": "None___1"
}