我最近开始使用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 = 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
希望它能解决你的问题.