Print the size of a VM in Bytes, KiloBytes, MegaBytes and GigaBytes

  • 28 August 2020
  • 0 replies
  • 80 views

Nutanix customers using AHV as their Hypervisor often need to evaluate resources being used/consumed on their infrastructure.  One of those factors involved is in calculating how much space has been consumed by one or more Virtual Machines.  It is possible to get granular in this scenario and be able to calculate the amount of space taken up by a VM.  Depending on their environment, it should be possible or feasible for one to print out the space usage in terms of Bytes, KiloBytes, MegaBytes or GigaBytes.

I have developed the following string manipulation to be able to get those numbers.  Here is a sample from one of the checks I performed:

nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$ acli vm.list | grep 120-Windows-Invent
120-Windows-Invent 99021293-4ea5-440a-a894-846ed07de132
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$ acli vm.get 120-Windows-Invent | grep vmdisk_size | awk '{sum+=$2} END {print sum}' | awk '{ byte =$1 ; echo; print byte " Bytes" }'
107374182400 Bytes
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$ acli vm.get 120-Windows-Invent | grep vmdisk_size | awk '{sum+=$2} END {print sum}' | awk '{ byte =$1 /1024; echo; print byte " KiloBytes" }'
104857600 KiloBytes
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$ acli vm.get 120-Windows-Invent | grep vmdisk_size | awk '{sum+=$2} END {print sum}' | awk '{ byte =$1 /1024/1024; echo; print byte " MegaBytes" }'
102400 MegaBytes
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$ acli vm.get 120-Windows-Invent | grep vmdisk_size | awk '{sum+=$2} END {print sum}' | awk '{ byte =$1 /1024/1024/1024; echo; print byte " GigaBytes" }'
100 GigaBytes
nutanix@NTNX-20FM6L56789-A-CVM:172.31.1.11:~$

As you can see above, using the string manipulation in conjunction with the standard Linux operations like grep and awk, and involving a bit of mathematics, we can produce the output on how much disk space a VM has taken up.

 

The next time you use these formulae, thank me.


0 replies

Be the first to reply!

Reply