Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting an instance's actual used (allocated) disk space in vmware with pyvmomi

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?

like image 550
Argais Avatar asked Oct 18 '22 04:10

Argais


1 Answers

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:

  1. 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

  2. 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.

like image 109
justjais Avatar answered Oct 21 '22 02:10

justjais