Last active
September 22, 2022 19:14
-
-
Save cmonaghan/ac3dfd504955bde814cb8a7bd3693fbb to your computer and use it in GitHub Desktop.
Query idle terraform cloud agent count and send as metric to datadog
This file contains 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 | |
# 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