PyVmomi: VMware vSphere Python SDK
一、OverView
重点知识:
1、view_type = [vim.VirtualMachine]
2、content.viewManager.CreateContainerView()
3、child.summary.config
def print_vm_info(virtual_machine): """ Print information for a particular virtual machine or recurse into a folder with depth protection """ summary = virtual_machine.summary print("Name : ", summary.config.name) print("Template : ", summary.config.template) print("Path : ", summary.config.vmPathName) print("Guest : ", summary.config.guestFullName) print("Instance UUID : ", summary.config.instanceUuid) print("Bios UUID : ", summary.config.uuid) print("State : ", summary.runtime.powerState) # 判断是否有注释 annotation = summary.config.annotation if annotation: print("Annotation : ", annotation) # 打印Guest OS内的信息 if summary.guest is not None: ip_address = summary.guest.ipAddress tools_version = summary.guest.toolsStatus if tools_version is not None: print("VM-tools: ", tools_version) else: print("V-tools: None") if ip_address: print("IP : ", ip_address) else: print("IP : None") content = service_instance.RetrieveContent() # 拿到vCenter的内容对象 container = content.rootFolder # starting point to look into view_type = [vim.VirtualMachine] # object types to look for recursive = True # whether we should look into it recursively container_view = content.viewManager.CreateContainerView(container, view_type, recursive) children = container_view.view for child in children: print_vm_info(child)
二、虚拟机状态、配置信息
1、runtime 虚拟机的运行状态
runtime = (vim.vm.RuntimeInfo) { dynamicType =, host = 'vim.HostSystem:host-34', connectionState = 'connected', powerState = 'poweredOn', // 虚拟机电源状态 faultToleranceState = 'notConfigured', // 是否配置FT dasVmProtection = , toolsInstallerMounted = false, suspendTime = , bootTime = 2017-08-26T06:31:27.543474Z, suspendInterval = 0, question = , memoryOverhead = , maxCpuUsage = 2808, maxMemoryUsage = 891, numMksConnections = 0, recordReplayState = 'inactive', cleanPowerOff = , needSecondaryReason = , onlineStandby = false, minRequiredEVCModeKey = , consolidationNeeded = false,
}
2、Guest操作系统信息(不建议使用,应为受到VMtools影响)
guest = (vim.vm.Summary.GuestSummary) { dynamicType =, dynamicProperty = (vmodl.DynamicProperty) [], guestId = 'ubuntu64Guest', guestFullName = 'Ubuntu Linux (64-bit)', toolsStatus = 'toolsOk', // VMtools状态 toolsVersionStatus = 'guestToolsUnmanaged', toolsVersionStatus2 = 'guestToolsUnmanaged', toolsRunningStatus = 'guestToolsRunning', hostName = 'ubuntu001', // hostname ipAddress = '172.16.65.146' // ipaddress
}
3、虚拟机配置
config = (vim.vm.Summary.ConfigSummary) { dynamicType =, dynamicProperty = (vmodl.DynamicProperty) [], name = 'Ubuntu16.04', template = false, vmPathName = '[datastore1] Ubuntu16.04/Ubuntu16.04.vmx', memorySizeMB = 1024, cpuReservation = 0, memoryReservation = 0, numCpu = 1, numEthernetCards = 1, numVirtualDisks = 1, uuid = '4239b0ea-cbb8-c0b2-56a1-0b98bdbf01dd', instanceUuid = '5039f07c-47c6-d77d-e793-bf1b7aee17e2', guestId = 'ubuntu64Guest', guestFullName = 'Ubuntu Linux (64-bit)', annotation = 'Ubuntu Server', product = , installBootRequired = false, ftInfo = , managedBy = },
4、虚拟机磁盘信息
storage = (vim.vm.Summary.StorageSummary) { dynamicType =, dynamicProperty = (vmodl.DynamicProperty) [], committed = 18424777995, uncommitted = 505, unshared = 17179869184, timestamp = 2017-08-26T08:37:37.764585Z },
。。。。