Skip to content

Instantly share code, notes, and snippets.

@slashk
Last active September 2, 2022 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashk/1409e14417d8b651095040cc9248f4fa to your computer and use it in GitHub Desktop.
Save slashk/1409e14417d8b651095040cc9248f4fa to your computer and use it in GitHub Desktop.
remove push arg
#!/bin/bash
# This script uses the Datadog API to pull down monthly costs by charge type (commit/on-demand) and product name into CSV
# run it like this (after you setup your datadog app / api keys as env variables as DD_APP_KEY and DD_API_KEY):
#
# % ./dd_monthly_cost_by_prod_charge.sh
#
# Output looks like this:
#
# product name, charge type, cost
# "logs_indexed_7day","committed",635
# "logs_indexed_7day","on_demand",587.19
# "lambda_function","committed",150
# "lambda_function","on_demand",0
# "infra_container","committed",0
# "infra_container","on_demand",0
# ...
#
# With the -t argument, you will get back just the total bill
#
# % ./dd_monthly_cost_by_prod_charge.sh -t
# Total: $77152.45
#
# NOTE: the API only lets you query up to 2-3 months back
#
# This script requires curl and jq to be installed. It may only work on MacOS due to date weirdness.
# $ brew install jq curl
#
# source available at https://gist.github.com/slashk/1409e14417d8b651095040cc9248f4fa
usage() { # Function: Print a help message.
echo "Usage: $0 [ -s START_DATE ] [ -e END_DATE ] [ -t ] [ -d ]" 1>&2
echo " START_DATE and END_DATE should be in YYYY-MM format"
echo " -t flag prints total cost without product details"
echo " -d prints debug messages"
exit 1
}
prereq() {
echo "This script requires jq and curl command line utilities"
echo "On a Mac, you can use homebrew ( https://brew.sh/ ) to install them with:"
echo ""
echo " $ brew install curl jq"
echo ""
exit 1
}
# check if prereqs jq and curl installed
if ! command -v "jq" &> /dev/null
then
prereq
fi
if ! command -v "curl" &> /dev/null
then
prereq
fi
# setup start and end date defaults
start_month=`date +%Y`-`date +%m`
end_month=`date +%Y`-`date -v+1m +%m`
TOTAL=0
# check for datadog API and APP keys to make API calls
if [[ -z DD_API_KEY ]]; then
echo "Datadog API KEY env variable DD_API_KEY not set "
usage
fi
if [[ -z DD_APP_KEY ]]; then
echo "Datadog APP KEY env variable DD_APP_KEY not set "
usage
fi
# Required DD query arguments
export view="sub-org"
while getopts s:e:td flag
do
case "${flag}" in
s) start_month=${OPTARG};;
e) end_month=${OPTARG};;
t) TOTAL=1;;
d) echo "start: ${start_month}, end: ${end_month}, totals: ${TOTAL}";;
*) usage;;
esac
done
#
if [[ TOTAL -eq 1 ]]
then
# jq parses and then sums up the costs
echo -n "Total: $"
curl -s -X GET "https://api.datadoghq.com/api/v2/usage/estimated_cost?view=${view}&start_month=${start_month}&end_month=${end_month}" \
-H "Accept: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq -r '.data[].attributes.total_cost'
else
# csv headers
echo '"product name","charge type","cost"'
# jq formats this into CSV output (which can be imported into Google Sheets and then graphed)
curl -s -X GET "https://api.datadoghq.com/api/v2/usage/estimated_cost?view=${view}&start_month=${start_month}&end_month=${end_month}" \
-H "Accept: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-H "DD-APPLICATION-KEY: ${DD_APP_KEY}" | jq -r '.data[].attributes.charges[] | [.product_name, .charge_type, .cost] | @csv'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment