Created
January 30, 2019 16:00
-
-
Save kwilczynski/19541d2d2cb40f6116d267becaeebdd3 to your computer and use it in GitHub Desktop.
Print metrics for DynamoDB table and secondary indexes to show provisioned capacity usage.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -Eeuf | |
set -o pipefail | |
PERIOD=${PERIOD-'3600'} | |
DAYS_AGO=${DAYS_AGO-'30'} | |
AWS_DEFAULT_REGION="$1" | |
shift | |
export PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin' | |
export AWS_DEFAULT_REGION | |
raw_jq() { | |
jq -r "$@" | |
} | |
days_ago() { | |
local day_count="$1" | |
date -u -j -v "-${day_count}d" +'%Y-%m-%dT%H:%M:%SZ' | |
} | |
time_now() { | |
days_ago 0 | |
} | |
list_tables() { | |
aws dynamodb list-tables | \ | |
raw_jq '.TableNames[] | @text' | |
} | |
list_indexes() { | |
local table="$1" | |
aws dynamodb describe-table \ | |
--table-name="${table}" | \ | |
raw_jq '.Table | .GlobalSecondaryIndexes[] | .IndexName' \ | |
2>/dev/null || true | |
} | |
retrieve_metric() { | |
local filter="$1" | |
local metric="$2" | |
aws cloudwatch get-metric-statistics \ | |
--namespace='AWS/DynamoDB' \ | |
--period="${PERIOD}" \ | |
--start-time="$(days_ago $DAYS_AGO)" \ | |
--end-time="$(time_now)" \ | |
--dimensions="${filter}" \ | |
--metric-name="${metric}" \ | |
--statistics='Sum' | \ | |
raw_jq '.Datapoints | map(.Sum) | add // 0' | |
} | |
table_metric() { | |
local table="$1" | |
local metric="$2" | |
retrieve_metric "Name=TableName,Value=${table}" "$metric" | |
} | |
index_metric() { | |
local table="$1" | |
local index="$2" | |
local metric="$3" | |
retrieve_metric "Name=TableName,Value=${table} Name=GlobalSecondaryIndexName,Value=${index}" "$metric" | |
} | |
metrics=( | |
'ConsumedReadCapacityUnits' | |
'ConsumedWriteCapacityUnits' | |
) | |
tables=( $(list_tables) ) | |
for table in "${tables[@]}"; do | |
indexes=( $(list_indexes "$table") ) | |
for metric in "${metrics[@]}"; do | |
echo "${table}:__table__.${metric} = $(table_metric "$table" "$metric")" | |
for index in "${indexes[@]}"; do | |
echo "${table}:${index}.${metric} = $(index_metric "$table" "$index" "$metric")" | |
done | |
done | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment