Skip to content

Instantly share code, notes, and snippets.

@cmonaghan
Last active September 22, 2022 19:14
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 cmonaghan/ac3dfd504955bde814cb8a7bd3693fbb to your computer and use it in GitHub Desktop.
Save cmonaghan/ac3dfd504955bde814cb8a7bd3693fbb to your computer and use it in GitHub Desktop.
Query idle terraform cloud agent count and send as metric to datadog
#!/bin/bash
# Before running this script, be sure to set the secrets
# export TFC_TOKEN=secret_token
# export DD_API_KEY=another_secret
# export AGENT_POOL_ID=some_id
idle_agent_count=$(curl \
--header "Authorization: Bearer $TFC_TOKEN" \
--header "Content-Type: application/vnd.api+json" \
--request GET \
https://app.terraform.io/api/v2/agent-pools/${AGENT_POOL_ID}/agents | grep -o -i idle | wc -l | xargs)
## Dynamic Points
# Post time-series data that can be graphed on Datadog’s dashboards.
# Template variables
now="$(date +%s)"
# Curl command
curl -X POST "https://api.datadoghq.com/api/v2/series" \
-H "Accept: application/json" \
-H "Content-Type: application/json" \
-H "DD-API-KEY: ${DD_API_KEY}" \
-d @- << EOF
{
"series": [
{
"metric": "tfc_christian.idle_agent_count",
"type": 3,
"points": [
{
"timestamp": ${now},
"value": ${idle_agent_count}
}
]
}
]
}
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment