我最近开始使用 pyvmomi 在将实例迁移到 AWS 之前获取 vmware 服务器的详细清单。
在vcenter Web界面或vsphere客户端中,我可以检查实例并查看其磁盘,它会告诉我磁盘大小(已配置)以及正在使用的磁盘量(已用存储)。
来自示例 github 存储库 (https://github.com/vmware/pyvmomi-community-samples https://github.com/vmware/pyvmomi-community-samples)我可以快速学习如何获取有关实例的信息,因此获取磁盘大小很简单(SO 中甚至有一个问题显示了获取驱动器的简单方法 -如何使用 PyVMomi 获取 VMWare VM 磁盘的大小 https://stackoverflow.com/questions/36026470/how-to-get-sizes-of-vmware-vm-disks-using-pyvmomi),但我不知道如何获取网络/客户端可以显示的实际使用的存储空间。
那么,如何获取给定实例磁盘的已用空间?
为了得到可用空间从虚拟机通过PyVMomi首先你必须检查是否虚拟机工具虚拟机是否安装在您的系统上。要检查是否已安装,请从您的VM 摘要页面中的访客信息(通过 MOB)如果显示:
工具状态 - VirtualMachineToolsStatus - “工具未安装”:
这意味着您必须将VMware工具安装到您各自的虚拟机上,您可以参考以下链接进行安装:a) or, b)
工具状态 - VirtualMachineToolsStatus - “toolsOk”:这意味着您的虚拟机已经安装了VMware工具,您可以获得diskPath, capacity and 可用空间属性值来自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
希望它能解决您的问题。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)