python - 使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间

标签 python vmware pyvmomi

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

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

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

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

最佳答案

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

  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 工具,并且您可以获得磁盘路径容量freeSpace 属性值来自 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

希望它能解决您的问题。

关于python - 使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38666195/

相关文章:

python - 搜索并替换 HTML 文本,而不是标签

vmware - Amazon EC2 可以用作 VMware 测试平台吗?

python - 如何使用 pyvmomi 将 ESXi 主机置于维护模式?

python - 如何使用 python 在 vcenter 上获取过滤的 VM 列表

Python装饰器按名称访问参数

python - 为什么在表达式字段中不允许生成器中未加括号的元组?

java - 树库式树解析器 python

Mac 上的 .NET 开发技巧

powershell - 需要帮助结合两个Powershell命令以在Docker中自动进行端口转发(在vmware上)

python - VMWare pyvmomi 6.0.0 : Operation not supported