Skip to content

Instantly share code, notes, and snippets.

@rajathithan
Last active April 17, 2023 10:15
Show Gist options
  • Save rajathithan/3cd858198cafef22f684a9cb42ad4dcd to your computer and use it in GitHub Desktop.
Save rajathithan/3cd858198cafef22f684a9cb42ad4dcd to your computer and use it in GitHub Desktop.
compute-engine-details
#!/bin/bash
# Script to retrieve compute engine details.
# Author - Rajathithan Rajasekar - 03/03/2020
echo "PROJECT NAME, INSTANCE NAME , ZONE , MACHINE-TYPE , OPERATING SYSTEM , CPU , MEMORY , DISK SIZE" > compute-engine-details.csv
prjs=( $(gcloud projects list | tail -n +2 | awk {'print $1'}) )
for i in "${prjs[@]}"
do
echo "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@" >> list.txt
echo "Setting Project: $i" >> list.txt
echo $(gcloud config set project $i)
if [[ $(gcloud services list --enabled --filter="NAME:compute.googleapis.com" --format="value(NAME)" | grep compute) ]]; then
echo "Compute API is enabled is in project - $i"
echo $(gcloud compute instances list | awk '{print $1,$2}' | tail -n +2| while read line; do echo "$i $line"; done |xargs -n3 sh -c 'python3 retrieve-compute-engine-details.py $1 $2 $3 >> compute-engine-details.csv' sh)
else
echo "Compute API is not enabled in project - $i"
fi
done
@VishnuvardhanKrishnan
Copy link

VishnuvardhanKrishnan commented Dec 14, 2022

Hi,

I hope you are running it from cloud shell, there is a bug with gcloud SDK to display the output in rows instead of columnar format. update your gcloud sdk in cloud shell and rerun the script .

Thanks, Raj

@rajathithan Thank you. Can you let me know how to get the hostname and Private IP of the machines through this script (both Linux & Windows)?

Thanks, Vishnu

@ricguarnieri
Copy link

While trying to execute the script, I'm getting the below:

  File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
    request = service.instances().get(project=project, zone=zone, instance=instance)
  File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method
    raise TypeError(
TypeError: Parameter "zone" value "NAME:" does not match the pattern "[a-z](?:[-a-z0-9]{0,61}[a-z0-9])?"
Traceback (most recent call last):
  File "/home/xpto/compute_details/retrieve-compute-engine-details.py", line 19, in <module>
    request = service.instances().get(project=project, zone=zone, instance=instance)
  File "/usr/local/lib/python3.9/dist-packages/googleapiclient/discovery.py", line 1073, in method

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment