Skip to content

Instantly share code, notes, and snippets.

@random-ua
Created September 9, 2020 01:10
Show Gist options
  • Select an option

  • Save random-ua/9cf736ad41d5ec04b5fb42b71f90d5ab to your computer and use it in GitHub Desktop.

Select an option

Save random-ua/9cf736ad41d5ec04b5fb42b71f90d5ab to your computer and use it in GitHub Desktop.

Revisions

  1. random-ua created this gist Sep 9, 2020.
    55 changes: 55 additions & 0 deletions dynamo-stats.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    #!/bin/bash

    PREFIX="arn:aws:dynamodb:us-east-1:$(aws sts get-caller-identity --query 'Arn' | awk -F: '{print $5}'):table"

    tables() {
    aws dynamodb list-tables --query 'TableNames' --output text | tr '[:space:]' "\n"
    }

    get_env() {
    aws dynamodb list-tags-of-resource --resource-arn $PREFIX/$1 --query 'Tags[?Key==`env`].Value|[0]' --output text
    }

    get_metric() {
    local TABLE=$1
    local METRIC=$2
    local STAT=$3
    local EXTRA_DIM=$4

    local TODAY=$(date +%F)
    #local MONTH_AGO=$(date -j -v -1m +%F)
    local MONTH_AGO=$(python -c 'from datetime import timedelta,datetime; print( (datetime.now()-timedelta(days=30)).strftime("%F") )')

    aws cloudwatch get-metric-statistics --namespace AWS/DynamoDB --metric-name $METRIC --statistics $STAT \
    --period 2592000 --start-time $MONTH_AGO --end-time $TODAY --dimensions Name=TableName,Value=$TABLE $EXTRA_DIM \
    --query "Datapoints[0].$STAT"
    }

    for table in $(tables); do
    env=$(get_env $table)
    if [[ "$env" == "None" ]]; then
    echo "Table $table has no env, skipping" >&2
    continue
    fi
    info=$(aws dynamodb describe-table --table-name $table --query 'Table.[TableSizeBytes,ItemCount]' --output text)
    size=$(echo $info | awk '{print $1}')
    items=$(echo $info | awk '{print $2}')

    max_prov_rcu=$(get_metric $table ProvisionedReadCapacityUnits Maximum)
    max_cons_rcu=$(get_metric $table ConsumedReadCapacityUnits Maximum)

    avg_prov_rcu=$(get_metric $table ProvisionedReadCapacityUnits Average)
    avg_cons_rcu=$(get_metric $table ConsumedReadCapacityUnits Average)

    max_prov_wcu=$(get_metric $table ProvisionedWriteCapacityUnits Maximum)
    max_cons_wcu=$(get_metric $table ConsumedWriteCapacityUnits Maximum)

    avg_prov_wcu=$(get_metric $table ProvisionedWriteCapacityUnits Average)
    avg_cons_wcu=$(get_metric $table ConsumedWriteCapacityUnits Average)

    get_lat=$(get_metric $table SuccessfulRequestLatency Average Name=Operation,Value=GetItem)
    put_lat=$(get_metric $table SuccessfulRequestLatency Average Name=Operation,Value=PutItem)
    upd_lat=$(get_metric $table SuccessfulRequestLatency Average Name=Operation,Value=UpdateItem)

    echo "$table,$env,$items,$size,$max_prov_rcu,$max_cons_rcu,$avg_prov_rcu,$avg_cons_rcu,$max_prov_wcu,$max_cons_wcu,$avg_prov_wcu,$avg_cons_wcu,$get_lat,$put_lat,$upd_lat"
    done