CloudWatch †
GetMetricStatisticsとGetMetricDataの違い †
基本的には GetMetricData が新しいAPIなので、こちらを使えば良い
logsを見る †
AWS CLI v2以降のみ。
AWS CLI v1だとawslogsが便利だったが、不用になりそう。
- --since: 過去のログ指定。s, m, h, d, wで指定
- --format short: ログストリームのID等が省略されるので見やすい
logsの検索 †
- awscli v2でlogsが便利になったので、不用かもしれない
awscliの場合
- フィルタパターンを使用したログデータ検索 - Amazon CloudWatch Logs
LOG_GROUP_NAME=/example/app/access
START_TIME="1 hour ago"
END_TIME="5 min ago"
AWS_PROFILE=example
AWS_REGION=ap-northeast-1
aws logs filter-log-events \
--log-group-name "${LOG_GROUP_NAME}" \
--start-time $(($(date +%s -d "${START_TIME}")*1000)) \
--end-time $(($(date +%s -d "${END_TIME}")*1000)) \
--profile $AWS_PROFILE \
--region $AWS_REGION
{
"searchedLogStreams": [
...
{
"searchedCompletely": true,
"logStreamName": "1234567890abcdef1234567890abcdef"
},
...
],
"events": [
...
{
"ingestionTime": 1547190580739,
"timestamp": 1547190570276,
"message": "172.33.31.254 - - [10/Jan/2019:07:09:30 +0000] \"GET /version HTTP/1.1\" 200 76 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
"eventId": "********************************************************",
"logStreamName": "1234567890abcdef1234567890abcdef"
},
...
]
}
awslogs: CloudWatch Logsを検索できるCUIツール †
- awscli v2でlogsが便利になったので、不用かもしれない
awscliよりパラメータや結果が直感的で、grep等で検索しやすい。
LOG_GROUP_NAME=/example/app/access
START_TIME="1h"
AWS_PROFILE=example
AWS_REGION=ap-northeast-1
# install
pip install awslogs
# group一覧
awslogs groups \
--profile $AWS_PROFILE \
--aws-region $AWS_REGION
# log取得
awslogs get $LOG_GROUP_NAME \
--start="${START_TIME}" \
--profile $AWS_PROFILE \
--aws-region $AWS_REGION
...
/example/app/access 1234567890abcdef1234567890abcdef 172.33.31.254 - - [10/Jan/2019:07:09:30 +0000] "GET /version HTTP/1.1" 200 76 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
CLIでグラフ画像を取得 †
get-metric-statistics — AWS CLI Command Reference では1回で1,440データポイントの制限がある。(1分のメトリクスを1日分取得可能)
グラフなら14日間や、複数メトリクスの同時表示できて、直感的に分かり安い。
- aws-cli/1.14.44 で確認した。
- 例:AWS Console上のDynamoDBのConsumedReadCapacityUnitsグラフは1分間のSum/60を表示している。自分でグラフを作る場合は、「RATE(m1)」を使う。
- An error occurred (InternalFailure) when calling the GetMetricWidgetImage operation (reached max retries: 4): Unknown
- startが過去すぎると出る。"-P5D" 等に短くする
DynamoDBの値の取得 †
- 期間を長く、periodを短くすると、data pointの上限(1,440)を超えエラーになる。
- "Sum" の値/60が、秒間のReadCapacityUnits。これがAWS console上のキャパシティ。下の例では5 ReadCapacityUnits/sec
AWS_PROFILE=example
AWS_REGION=ap-northeast-1
DYNAMODB_TABLE=TestTable
START_TIME="5 min ago"
END_TIME="5 min ago"
STATISTICS="Sum"
PERIOD=60
aws cloudwatch get-metric-statistics \
--dimensions Name=TableName,Value=$DYNAMODB_TABLE \
--namespace AWS/DynamoDB \
--metric-name ConsumedReadCapacityUnits \
--statistics "${STATISTICS}" \
--start-time $(date --iso-8601=seconds -d "${START_TIME}") \
--end-time $(date --iso-8601=seconds -d "${END_TIME}") \
--period $PERIOD \
--profile $AWS_PROFILE \
--region $AWS_REGION
{
"Datapoints": [
{
"Timestamp": "2018-11-01T08:32:00Z",
"Sum": 0.0,
"Unit": "Count"
},
{
"Timestamp": "2018-11-01T08:31:00Z",
"Sum": 0.0,
"Unit": "Count"
},
{
"Timestamp": "2018-11-01T08:34:00Z",
"Sum": 300.0,
"Unit": "Count"
},
{
"Timestamp": "2018-11-01T08:30:00Z",
"Sum": 261.0,
"Unit": "Count"
},
{
"Timestamp": "2018-11-01T08:33:00Z",
"Sum": 0.0,
"Unit": "Count"
}
],
"Label": "ConsumedReadCapacityUnits"
}
- jqを使って計算できる
# 秒間の最大値
jq '[.Datapoints[].Sum] | max/60' ./dynamo.read.json
5
# 合計(add), 件数(length)
jq '[.Datapoints[].Sum] | add,length' ./dynamo.read.json
561
5
# 平均
jq '[.Datapoints[].Sum] | add/length' ./dynamo.read.json
112.2
RDSのCloudWatchメトリクスを設定し、閾値を超えたらメールを送る †
- AWS SNSトピックを作成
AWS_PROFILE=default
AWS_REGION=ap-northeast-1
RDS_ID=db01
EMAIL=alerts@example.com
aws --profile $AWS_PROFILE --region $AWS_REGION \
sns create-topic --name "AlertToMyemail"
{
"TopicArn": "arn:aws:sns:ap-northeast-1:1234567890:AlertToMyemail"
}
TopicArn="arn:aws:sns:ap-northeast-1:1234567890:AlertToMyemail"
aws --profile $AWS_PROFILE --region $AWS_REGION \
sns subscribe --topic-arn $TopicArn \
--protocol email \
--notification-endpoint $EMAIL
- 確認メールが届くので "Confirm subscription" をクリック
- AWS SNS 設定確認
aws --profile $AWS_PROFILE --region $AWS_REGION \
sns list-subscriptions-by-topic --topic-arn $TopicArn
- RDS:CPUUtilization >= 80% のメトリクスを作成
aws --profile $AWS_PROFILE --region $AWS_REGION \
cloudwatch put-metric-alarm \
--alarm-name awsrds-${RDS_ID}-High-CPU-Utilization \
--alarm-description "Alarm when CPU exceeds 80%" \
--metric-name CPUUtilization \
--namespace AWS/RDS \
--statistic Average \
--period 300 \
--threshold 80 \
--unit Percent \
--comparison-operator GreaterThanOrEqualToThreshold \
--dimensions Name=DBInstanceIdentifier,Value=$RDS_ID \
--evaluation-periods 1 \
--alarm-actions $TopicArn
- RDS:FreeStorageSpace <= 5GB のメトリクスを作成(--threshold 5 --unit Gigabyte の設定は無効。Management Console上では0と表示される)
aws --profile $AWS_PROFILE --region $AWS_REGION \
cloudwatch put-metric-alarm \
--alarm-name awsrds-${RDS_ID}-High-Free-Storage-Space \
--alarm-description "Alarm when Free-Storage-Space less than 5GB" \
--metric-name FreeStorageSpace \
--namespace AWS/RDS \
--statistic Average \
--period 300 \
--threshold 5242880000.0 \
--comparison-operator LessThanOrEqualToThreshold \
--dimensions Name=DBInstanceIdentifier,Value=$RDS_ID \
--evaluation-periods 1 \
--alarm-actions $TopicArn
- 作成したメトリクスを確認
aws --profile $AWS_PROFILE --region $AWS_REGION \
cloudwatch describe-alarms --alarm-names awsrds-${RDS_ID}-High-CPU-Utilization awsrds-${RDS_ID}-High-Free-Storage-Space