您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:21:57 python 字数 4638 阅读 547 来源 www.jb51.cc/python

我最近开始使用pyvmomi在将实例迁移到AWS之前获取vmware服务器的详细清单.在vcenter Web界面或vsphere客户端中,我可以检查实例并查看其磁盘,它会告诉我磁盘大小(已配置)以及它的使用量(已使用的存储空间).从样本github repo(https://github.com/vmware/pyvmomi-community-sampl

概述

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

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

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

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

> 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
> toolsStatus – VirtualMachineToolsStatus – “toolsOk”:这意味着您的VM已经安装了VMware工具,您可以从vim.vm.GuestInfo.DiskInfo获取diskPath,capacity和freeSpace属性值. (如果您如上所述手动安装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

希望它能解决你的问题.

总结

以上是编程之家为你收集整理的使用pyvmomi在vmware中获取实例的实际使用(分配)磁盘空间全部内容,希望文章能够帮你解决使用pyvmomi在vmware中获取实例的实际使用(分配)磁盘空间所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶