Skip to content

Instantly share code, notes, and snippets.

@doerodney
Created June 14, 2019 21:09
Show Gist options
  • Save doerodney/69aee1f080dbcf7d913e73b5b9d79192 to your computer and use it in GitHub Desktop.
Save doerodney/69aee1f080dbcf7d913e73b5b9d79192 to your computer and use it in GitHub Desktop.
Executes a query of Datadog to identify hosts that have a specified metric over the previous day.
# Executes a query of Datadog to identify hosts that have a specified metric
# over the previous day.
# Assumes that first parameter is the name of a metric.
# Example: ./get_metric_hosts.sh inf.backup.sizeScanned
# Requires that Datadog credentials be implemented in environment variables
# DATADOG_API_KEY and DATADOG_API_KEY.
# Requires that jq is installed.
metric=inf.backup.errCode # by default
if [ "$1" != "" ]; then
metric=$1
fi
outfile=metric-hosts-${metric}.csv
echo "host" > ${outfile}
curl -G \
"https://api.datadoghq.com/api/v1/query" \
-d "api_key=${DATADOG_API_KEY}" -d "application_key=${DATADOG_APP_KEY}" \
-d "from=$(date -v -1d +%s)" -d "to=$(date +%s)" \
-d "query=${metric}{*}by{host}" \
| jq '.' \
| grep "scope" \
| sed -e 's/"//g' -e 's/^[ \t]*//g' -e 's/scope: host://g' -e 's/,//g' \
| sort | uniq \
>> ${outfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment