Eva
Eva

Reputation: 593

gcloud command to display vCPU's and Memory assigned to Instances

How to list vCPU's and Memory assigned to instances using glcoud compute instances list command. I was able to frame the below command, but it's not showing any value. I know machine type has all the info required info to map, I am looking for a command which displays vCPU's and memory

gcloud compute instances list --format="value(name,machineType,items[].scheduling.minNodeCpus,zone,disks[].type,disks[].diskSizeGb.list())"

TestVM  custom-4-32768-ext              us-central1-a   PERSISTENT      200
TestVM1 custom-4-32768-ext              us-central1-a   PERSISTENT      400

I was looking for something like

TestVM  custom-4-32768-ext     8  32GB    us-central1-a   PERSISTENT      200
TestVM1 custom-4-32768-ext     4  16GB    us-central1-a   PERSISTENT      200

Upvotes: 2

Views: 3147

Answers (1)

DazWilkin
DazWilkin

Reputation: 40061

There are a couple of challenges:

  • instances describe works best with the instance name and zone
  • machine-types describe is needed (!?) to get CPU|RAM for non-custom
  • custom machine types are self-describing

On Linux|Bash, here's the basic info:

# Get instance name,zone for `${PROJECT}
for PAIR in $(\
  gcloud compute instances list \
  --project=${PROJECT} \
  --format="csv[no-heading](name,zone.scope(zones))")
do
  # Parse result from above into instance and zone vars
  IFS=, read INSTANCE ZONE <<< ${PAIR}
  # Get the machine type value only
  MACHINE_TYPE=$(\
    gcloud compute instances describe ${INSTANCE} \
    --project=${PROJECT} \
    --zone=${ZONE} \
    --format="value(machineType.scope(machineTypes))")
  # If it's custom-${vCPUs}-${RAM} we've sufficient info
  if [[ ${MACHINE_TYPE}} == custom* ]]
  then
    IFS=- read CUSTOM CPU MEM <<< ${MACHINE_TYPE}
    printf "%s: vCPUs: %s; Mem: %s\n" ${INSTANCE} ${CPU} ${MEM}
  else
    # Otherwise, we need to call `machine-types describe`
    CPU_MEMORY=$(\
      gcloud compute machine-types describe ${MACHINE_TYPE} \
      --project=${PROJECT} \
      --zone=${ZONE} \
      --format="csv[no-heading](guestCpus,memoryMb)")
    IFS=, read CPU MEM <<< ${CPU_MEMORY}
    printf "%s: vCPUs: %s; Mem: %s\n" ${INSTANCE} ${CPU} ${MEM}
  fi
done

I'll leave it to you to combine with the instances describe data as you wish.

There is undoubtedly another (and possibly better) way to do this.

Upvotes: 2

Related Questions