Created
October 16, 2025 10:27
-
-
Save GuyBarros/3ce2eb1299f1a0e27cb500a6c8ed7b75 to your computer and use it in GitHub Desktop.
A Script to Query a consul cluster then output a summary of services instances and a CSV files containing services names and service instance IDs
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 | |
| ################################ | |
| # Requirements: curl, jq | |
| ################################ | |
| ## avoid the last trailling forward slash / | |
| usage="$(basename "$0") [-a consul_http_address] [-t acl_token] [-e IS_ENTERPRISE] [-l LIST_INSTACES] | |
| where | |
| -a Consul HTTP(s) Address. | |
| -t Consul ACL token with permissions to read nodes and services across DCs(if applicable). | |
| -e If the target is an enterprise Consul, look over namespaces and count services across namespaces. | |
| -l If provided, list all service instances found into a csv file. | |
| " | |
| while getopts :a:t:e:l:h: flag | |
| do | |
| case "$flag" in | |
| a) ADDRESS=$OPTARG;; | |
| t) ACL_TOKEN=$OPTARG;; | |
| e) IS_ENTERPRISE=$OPTARG;; | |
| l) LIST_INSTANCES=$OPTARG;; | |
| h) echo "$usage" | |
| exit | |
| ;; | |
| :) printf "missing argument for -%s\n" "$OPTARG" >&2 | |
| echo "$usage" >&2 | |
| exit 1 | |
| ;; | |
| esac | |
| done | |
| IS_ENTERPRISE=${IS_ENTERPRISE:-false} | |
| LIST_INSTANCES=${LIST_INSTANCES:-false} | |
| echo "Is Enterprise: $IS_ENTERPRISE" | |
| echo "List instances: $LIST_INSTANCES" | |
| if $LIST_INSTANCES;then | |
| SERVICES_LIST_FILE_TABLE="services_list_tabled_$(date +"%Y.%m.%d").txt" | |
| SERVICES_LIST_FILE_CSV="services_list_tabled_$(date +"%Y.%m.%d").csv" | |
| echo "Service, ID" >> $SERVICES_LIST_FILE_CSV | |
| fi | |
| if [ ! "$ADDRESS" ] ; then | |
| echo "Argument -a must be provided" | |
| echo "$usage" >&2; exit 1 | |
| fi | |
| if [ "$ACL_TOKEN" = "" ] | |
| then | |
| HEADER="" | |
| else | |
| HEADER="X-Consul-Token: $ACL_TOKEN" | |
| fi | |
| total_service_instances=0 | |
| total_nodes=0 | |
| DCS=$(curl -ks \ | |
| -H "$HEADER" \ | |
| $ADDRESS/v1/catalog/datacenters | jq -r '[ .[] ]| join (" ")' ) | |
| for DC in $DCS; do | |
| echo "--------------------------------" | |
| echo "Countin Services in dc $DC" | |
| NODES=$(curl -ks \ | |
| -H "$HEADER" \ | |
| $ADDRESS/v1/catalog/nodes?dc=$DC | jq -r '[ .[].Node ]| join (" ")' ) | |
| for node in $NODES; do | |
| if [[ $node =~ .*consul-server.* ]]; then | |
| continue | |
| fi | |
| total_nodes=$(($total_nodes+1)) | |
| if $IS_ENTERPRISE;then | |
| query="ns=*&dc=$DC" | |
| else | |
| query="dc=$DC" | |
| fi | |
| echo "Getting services from $node" | |
| if $LIST_INSTANCES;then | |
| # echo "Getting services from $node" >> $SERVICES_LIST_FILE_TABLE | |
| curl -s "$CONSUL_HTTP_ADDR/v1/catalog/node-services/$node?$query" | jq -r '.Services[] | select(.Service | contains("consul") | not) | "\(.Service)\t\(.ID)"' | column -t >> $SERVICES_LIST_FILE_TABLE | |
| ######## | |
| curl -s "$CONSUL_HTTP_ADDR/v1/catalog/node-services/$node?$query" | jq -r '.Services[]| select(.Service | contains("consul") | not)| [.Service, .ID]| @csv' >> $SERVICES_LIST_FILE_CSV | |
| ######## | |
| fi | |
| service_instances=$(curl -ks \ | |
| -H "$HEADER" \ | |
| $ADDRESS/v1/catalog/node-services/$node?$query | jq 'try (.Services | map(select(.ID | contains("consul") | not) ) ) catch 0 | length') | |
| echo " - found $service_instances" | |
| total_service_instances=$(($total_service_instances+$service_instances)) | |
| done | |
| done | |
| echo "--------------------------------" | |
| echo "Total service instances found: $total_service_instances" | |
| echo "Total number of nodes: $total_nodes" | |
| echo "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment