Skip to content

Instantly share code, notes, and snippets.

@kszarek
Last active February 18, 2021 03:09
Show Gist options
  • Save kszarek/6f0ac8a31b88726902ef2f03833c9fdf to your computer and use it in GitHub Desktop.
Save kszarek/6f0ac8a31b88726902ef2f03833c9fdf to your computer and use it in GitHub Desktop.
Bash script to get VM count and disk volume sizes from GCP projects
#!/usr/bin/env bash
envs='airhelp-staging airhelp-production ah-test-174206'
get_vm_count(){
ENV_NAME="$1"
count=$(gcloud --project="$ENV_NAME" compute instances list|grep -v NAME -c)
echo "VM count for $ENV_NAME: $count"
}
get_vm_storage(){
ENV_NAME="$1"
count=$(gcloud --project="$ENV_NAME" compute disks list|awk 'BEGIN {FS = " "} ; {sum+=$4} END {print sum}')
echo "VM storage for $ENV_NAME: $count"
}
get_db_count(){
ENV_NAME="$1"
count=$(gcloud --project="$ENV_NAME" sql instances list|grep -v Name|wc -l)
echo "DB count for $ENV_NAME: $count"
}
get_db_storage(){
ENV_NAME="$1"
echo -n "DB storage for $ENV_NAME: "
for x in $(gcloud --project="$ENV_NAME" sql instances list --format="value(Name)");
do
gcloud --project="$ENV_NAME" sql instances describe "$x" --format="value(settings.dataDiskSizeGb)";
done | awk '{s+=$1} END {printf "%.0f\n", s}'
}
for env in $envs
do
get_vm_count "$env"
done
echo
for env in $envs
do
get_vm_storage "$env"
done
echo
for env in $envs
do
get_db_count "$env"
done
echo
for env in $envs
do
get_db_storage "$env"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment