Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
631 views
in Technique[技术] by (71.8m points)

google cloud platform - 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
question from:https://stackoverflow.com/questions/65888736/gcloud-command-to-display-vcpus-and-memory-assigned-to-instances

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
" ${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
" ${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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...