使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间 [英] Getting an instance's actual used (allocated) disk space in vmware with pyvmomi

查看:15
本文介绍了使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've recently started using pyvmomi to get a detailed inventory of vmware servers prior to migrating instances into AWS.

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

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

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

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?

推荐答案

要通过 PyVMomi 从 VM 获得 freespace,首先你有检查您的系统上是否安装了用于 VM 的 VMware 工具.要检查其是否已安装,请从 VM 摘要页面(通过 MOB) 中检查您的虚拟机访客信息,是否显示:

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":这意味着您必须在各自的 VM 上安装 VMware 工具,您可以参考以下链接进行安装:a)https://my.vmware.com/web/vmware/details?productId=491&down​​loadGroup=VMTOOLS1000 或 b)https://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1018377

  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

toolsStatus - VirtualMachineToolsStatus - "toolsOk":这意味着您的 VM 已经安装了 VMware 工具,您可以获得diskPath容量freeSpace 属性值来自 vim.vm.GuestInfo.DiskInfo.(如果您如上所述手动安装 VMware 工具,则以下内容应正确)

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.

这篇关于使用 pyvmomi 在 vmware 中获取实例的实际使用(分配)磁盘空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆