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

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

概述我最近开始使用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 /客户端可以显示的实际使用的存储空间.

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

最佳答案要通过pyvmomi从VM获取自由空间,首先必须检查系统上是否安装了适用于VM的VMware工具.要检查其是否已安装,请从其摘要页面(通过MOB)检查VM的访客信息是否显示:

> 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 = Nonevcenter_host = "HOSTname"vcenter_port = NUMERIC_PORTvcenter_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中获取实例的实际使用(分配)磁盘空间所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/langs/1206353.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存