5

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

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

从示例 github 存储库(https://github.com/vmware/pyvmomi-community-samples)中,我可以快速了解如何获取有关实例的信息,因此获取磁盘大小是微不足道的(SO 中甚至有一个问题显示获取驱动器的简单方法 -如何使用 PyVMomi 获取 VMWare VM 磁盘的大小),但我不知道如何获取 Web/客户端可以显示的实际使用的存储。

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

4

2 回答 2

3

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

  1. toolsStatus - VirtualMachineToolsStatus - "toolsNotInstalled":这意味着您必须将 VMware 工具安装到各自的 VM,您可以参考以下链接进行安装:a) https://my.vmware.com/web/vmware/details?productId =491&downloadGroup=VMTOOLS1000或,b) https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1018377

  2. toolsStatus - VirtualMachineToolsStatus - "toolsOk":这意味着您的 VM 已经安装了 VMware 工具,您可以从 vim.vm.GuestInfo.DiskInfo 获取diskPath capacityfreeSpace属性值。(如果您如上所述手动安装 VMware 工具,以下应该是正确的)

一旦设置了上述环境,您可以通过以下代码从您的 VM 获取相应的信息:

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

希望它能解决你的问题。

于 2016-08-10T08:39:39.857 回答
0

如果您使用的是 vSphere API 4.0 以上版本,请查看此vmware 文档

这是我的虚拟机配置

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 }

蟒蛇代码

...
vm = <find your vm> 
#disk size in byte
disk_size = vm.summary.storage.committed + vm.summary.storage.uncommitted
于 2018-07-03T07:48:30.647 回答