基本的には GetMetricData が新しいAPIなので、こちらを使えば良い
AWS CLI v2以降のみ。
AWS CLI v1だとawslogsが便利だったが、不用になりそう。
aws logs tail --follow <LOG-GROUP-NAME>
awscliの場合
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" }, ... ] }
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
get-metric-statistics — AWS CLI Command Reference では1回で1,440データポイントの制限がある。(1分のメトリクスを1日分取得可能)
グラフなら14日間や、複数メトリクスの同時表示できて、直感的に分かり安い。
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 '[.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
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
aws --profile $AWS_PROFILE --region $AWS_REGION \ sns list-subscriptions-by-topic --topic-arn $TopicArn
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
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