我通过VMware的sdk pyvmomi来获取VSphere平台虚拟机的信息,但是整体获取非常慢,现在想通过gevent来改造下,但是不知道该如何下手,请大神们指教下,下面是代码:
main程序:
def vm_info_dump(): # Periodic tasks - get all the vms in VCenter and dump to json file for vc in VC_POOL.keys(): conn = VCenterInfo(vc) vm_info_list = conn.get_vc_vm(vc) file_name = vc json.dump( vm_info_list, open(BASE_DIR + '/vmserver/data/{0}.json'.format(file_name), 'w'), ensure_ascii=False, indent=4 )
main调用的类方法
class VCenterInfo(object): def __init__(self, ip, user=USER, passwd=PASSWORD): # init connection self.ip = ip self.user = user self.password = passwd self.si = connect.SmartConnect( host=self.ip, user=self.user, pwd=self.password ) def vms_info(self): # vm obj list atexit.register(connect.Disconnect, self.si) content = self.si.RetrieveContent() container = content.rootFolder VmViewType = [vim.VirtualMachine] recursive = True VmContainerView = content.viewManager.CreateContainerView( container, VmViewType, recursive) vm_children = VmContainerView.view return vm_children def get_vc_vm(self, vc): vms = self.vms_info() data_list = [] for vm in vms: data = {} summary = vm.summary guest = vm.guest config = vm.config runtime = vm.runtime devices = vm.config.hardware.device # if hasattr(summary, 'config') disk_kb = 0 for device in devices: if isinstance(device, vim.vm.device.VirtualDisk): disk_kb_one = device.capacityInKB disk_kb += disk_kb_one total_disk_gb = disk_kb / 1024 / 1024 data['instance_uuid'] = config.instanceUuid data['list_name'] = config.name data['hostname'] = guest.hostName data['ip'] = get_public_ip(vm) data['os'] = guest.guestFamily data['os_version'] = guest.guestFullName if hasattr(config, 'hardware'): data['cpu'] = config.hardware.numCPU data['mem'] = config.hardware.memoryMB / 1024 data['total_hard_disk'] = total_disk_gb data['tools_status'] = guest.toolsStatus data['guest_status'] = guest.guestState data['power_status'] = runtime.powerState data['vc'] = vc data['esxi_host'] = runtime.host.name data['template'] = summary.config.template data_list.append(data) return data_list
看了下貌似也就只有
get_vc_vm
这个方法可以用多线程搞一搞