使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘 space

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

我最近开始使用 pyvmomi 在将实例迁移到 AWS 之前获取 vmware 服务器的详细清单。

在 vcenter 网络界面或 vsphere 客户端中,我可以检查一个实例并查看它的磁盘,它会告诉我磁盘大小(已配置),以及有多少正在使用(已用存储) .

来自示例 github 存储库 (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),但我不知道如何获取 web/client 可以显示的实际使用的存储空间。

那么,如何获取给定实例磁盘的已用 space?

为了通过 PyVMomi 从 VM 获取 freespace 首先你必须检查是否您的系统上是否安装了用于 VM 的 VMware 工具。要检查它是否已安装,请从其摘要页面(通过 MOB)检查 VM 的访客信息 如果它显示:

  1. toolsStatus - VirtualMachineToolsStatus - "toolsNotInstalled": 这意味着您必须将 VMware 工具安装到您各自的 VM,您可以参考以下链接进行安装: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": 这意味着你的虚拟机已经安装了 VMware 工具,你可以得到 diskPathcapacityfreeSpace 属性值来自 vim.vm.GuestInfo.DiskInfo。 (如果你像上面提到的那样手动安装VMware工具,下面应该是正确的)

设置好上述环境后,您可以通过以下代码从您的虚拟机获取相应信息:

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

希望它能解决您的问题。

如果您使用的是 vSphere API 4.0 以上版本, 请看这个vmware documentation

这是我的虚拟机配置

json转储

vm = <find your vm> 
jsonify(vm.summary.storage)

# dump output
(vim.vm.Summary.StorageSummary) { 
    dynamicType = <unset>, 
    dynamicProperty = (vmodl.DynamicProperty) [], 
    committed = 8591326612, # 8GB (first commmited hdd)
    uncommitted = 54964532611, # 20GB + 30GB (two added hdd)
    unshared = 8589934592, 
    timestamp = 2018-07-03T07:23:26.715559Z }

python代码

...
vm = <find your vm> 
#disk size in byte
disk_size = vm.summary.storage.committed + vm.summary.storage.uncommitted