CloudWatch


GetMetricStatisticsとGetMetricDataの違い

基本的には GetMetricData が新しいAPIなので、こちらを使えば良い


logsを見る

AWS CLI v2以降のみ。
AWS CLI v1だとawslogsが便利だったが、不用になりそう。


logsの検索

awscliの場合


awslogs: CloudWatch Logsを検索できるCUIツール

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日間や、複数メトリクスの同時表示できて、直感的に分かり安い。


DynamoDBの値の取得


RDSのCloudWatchメトリクスを設定し、閾値を超えたらメールを送る

  1. 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
  2. 確認メールが届くので "Confirm subscription" をクリック
  3. AWS SNS 設定確認
    aws --profile $AWS_PROFILE --region $AWS_REGION \
      sns list-subscriptions-by-topic --topic-arn $TopicArn
  4. 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
  5. 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
  6. 作成したメトリクスを確認
    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

添付ファイル: filedynamodb_capacity.TestTable.png 1513件 [詳細] filedynamodb_capacity.20181116.zip 1729件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2020-12-25 (金) 11:18:00