I've recently started using pyvmomi to get a detailed inventory of vmware servers prior to migrating instances into AWS.
In the vcenter web interface, or vsphere client, I can inspect an instance and see its disks, and it'll tell me the disk size (provisioned), and how much of it is in use (used storage).
From the samples github repo (https://github.com/vmware/pyvmomi-community-samples) I could quickly learn how to get information on the instances, so getting the disk size is trivial (there's even a question in SO that shows an easy way to get the drives - How to get sizes of VMWare VM disks using PyVMomi), but I can't figure out how to get the actual used storage the web/client can show.
So, how do I get the used space for a given instance disks?
For getting the freespace from the VM via PyVMomi first you have to check if the VMware tools for VM's is installed on your system or not. For checking if its installed, check from your VM's guest information from its summary page (via MOB) if it shows:
toolsStatus - VirtualMachineToolsStatus - "toolsNotInstalled": This means that you have to install the VMware tools to your respective VM, you can refer following links to install: a)https://my.vmware.com/web/vmware/details?productId=491&downloadGroup=VMTOOLS1000 or, b)https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1018377
toolsStatus - VirtualMachineToolsStatus - "toolsOk": This means that your VM already has VMware tools installed, and you can get the diskPath, capacity and freeSpace properties values from vim.vm.GuestInfo.DiskInfo. (If you install VMware tools manually as mentioned above, following should be true)
Once, the above environment is set, you can get the respective information from your VM via following code:
service_instance = None
vcenter_host = "HOSTNAME"
vcenter_port = NUMERIC_PORT
vcenter_username = "USERNAME"
vcenter_password = "PASSWORD"
vmName = "VM_NAME";
try:
#For trying to connect to VM
service_instance = connect.SmartConnect(host=vcenter_host, user=vcenter_username, pwd=vcenter_password, port=vcenter_port, sslContext=context)
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
container = content.rootFolder # starting point to look into
viewType = [vim.VirtualMachine] # object types to look for
recursive = True # whether we should look into it recursively
containerView = content.viewManager.CreateContainerView(
container, viewType, recursive)
#getting all the VM's from the connection
children = containerView.view
#going 1 by 1 to every VM
for child in children:
vm = child.summary.config.name
#check for the VM
if(vm == vmName):
vmSummary = child.summary
#get the diskInfo of the selected VM
info = vmSummary.vm.guest.disk
#check for the freeSpace property of each disk
for each in info:
#To get the freeSPace in GB's
diskFreeSpace = each.freeSpace/1024/1024/1024
Hope it resolves your issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With