def get_latest_stat_multiple(target, token, uuids, key): if not isinstance(uuids, list): print( "Error in get multiple: uuids must be a list with multiple entries" ) return False # vrops can not handle more than 1000 uuids uuids_chunked = list(chunk_list(uuids, 1000)) return_list = list() url = "https://" + target + "/suite-api/api/resources/stats/latest/query" headers = { 'Content-Type': "application/json", 'Accept': "application/json", 'Authorization': "vRealizeOpsToken " + token } import queue q = queue.Queue() thread_list = list() chunk_iteration = 0 for uuid_list in uuids_chunked: chunk_iteration += 1 t = Thread(target=Resources.get_stat_chunk, args=(q, uuid_list, url, headers, key, target, chunk_iteration)) thread_list.append(t) t.start() for t in thread_list: t.join() while not q.empty(): return_list += q.get() return return_list
def get_vms(self, target, token, parent_uuids, vcenter_uuid): amount_vms, api_responding, _ = self.get_latest_stats_multiple( target, token, [vcenter_uuid], ['summary|total_number_vms'], 'Inventory') number_of_vms = amount_vms[0].get('stat-list', {}).get('stat', [])[0].get('data', [0])[0] if \ api_responding == 200 else 0 # vrops cannot handle more than 10000 uuids in a single request split_factor = int(number_of_vms / 10000) if split_factor >= 1: uuids_chunked = list( chunk_list(parent_uuids, int(len(parent_uuids) / (split_factor * 2)))) logger.debug( f'Chunking VM requests into {len(uuids_chunked)} chunks') vms = list() for uuid_list in uuids_chunked: vms.extend( self.get_resources(target, token, uuid_list, adapterkind="VMWARE", resourcekinds=["VirtualMachine"], resource_obj=VirtualMachine, data_receiving=True)) logger.debug(f'Number of VMs collected: {len(vms)}') return vms return self.get_resources(target, token, parent_uuids, adapterkind="VMWARE", resourcekinds=["VirtualMachine"], resource_obj=VirtualMachine, data_receiving=True)
def get_latest_values_multiple(self, target: str, token: str, uuids: list, keys: list, collector: str, kind: str = None) -> (list, int, float): # vrops can not handle more than 1000 uuids for stats uuids_chunked = list(chunk_list(uuids, 1000)) if kind == 'stats' else [uuids] url = f"https://{target}/suite-api/api/resources/stats/latest/query" if kind == 'stats' else \ f"https://{target}/suite-api/api/resources/properties/latest/query" headers = { 'Content-Type': "application/json", 'Accept': "application/json", 'Authorization': f"vRealizeOpsToken {token}" } q = queue.Queue() thread_list = list() chunk_iteration = 0 logger.debug('>----------------------- get_latest_values_multiple') logger.debug(f'target : {target}') logger.debug(f'collector: {collector}') logger.debug(f'Amount keys : {len(keys)}') for k in keys: logger.debug(f'key : {k}') for uuid_list in uuids_chunked: chunk_iteration += 1 t = Thread(target=Vrops._get_chunk, args=(q, uuid_list, url, headers, keys, target, kind, collector, chunk_iteration)) thread_list.append(t) t.start() for t in thread_list: t.join() return_list = list() response_status_codes = list() response_time_elapsed = list() while not q.empty(): returned_chunks = q.get() response_time_elapsed.append(returned_chunks[2]) response_status_codes.append(returned_chunks[1]) return_list.extend(returned_chunks[0]) logger.debug(f'Amount uuids: {len(uuids)}') logger.debug( f'Fetched : {len({r.get("resourceId") for r in return_list})}') logger.debug('<--------------------------------------------------') return return_list, max(response_status_codes), sum( response_time_elapsed) / len(response_time_elapsed)
def get_latest_stat_multiple(target, token, uuids, key, collector): if not isinstance(uuids, list): logger.error( 'Error in get project_ids: uuids must be a list with multiple entries' ) return False # vrops can not handle more than 1000 uuids uuids_chunked = list(chunk_list(uuids, 1000)) return_list = list() url = "https://" + target + "/suite-api/api/resources/stats/latest/query" headers = { 'Content-Type': "application/json", 'Accept': "application/json", 'Authorization': "vRealizeOpsToken " + token } import queue q = queue.Queue() thread_list = list() chunk_iteration = 0 logger.debug('>------------------------ get_latest_stats_multiple') logger.debug(f'key : {key}') logger.debug(f'target : {target}') logger.debug(f'collector: {collector}') for uuid_list in uuids_chunked: chunk_iteration += 1 t = Thread(target=Vrops.get_stat_chunk, args=(q, uuid_list, url, headers, key, target, chunk_iteration)) thread_list.append(t) t.start() for t in thread_list: t.join() while not q.empty(): return_list += q.get() logger.debug(f'Amount uuids: {len(uuids)}') logger.debug(f'Fetched : {len(return_list)}') logger.debug('<--------------------------------------------------') return return_list
def get_vms(self, target, token, parent_uuids, vcenter_uuid, query_specs): resourcekind = 'VirtualMachine' q_specs = self._set_query_specs(query_specs, resourcekind) amount_vms, api_responding, _ = self.get_latest_stats_multiple( target, token, [vcenter_uuid], ['summary|total_number_vms'], 'Inventory') number_of_vms = amount_vms[0].get('stat-list', {}).get('stat', [])[0].get('data', [0])[0] if \ api_responding == 200 and amount_vms else 0 # vrops cannot handle more than 10000 uuids in a single request split_factor = int(number_of_vms / 10000) if split_factor >= 1: uuids_chunked = list( chunk_list(parent_uuids, int(len(parent_uuids) / (split_factor + 1)))) logger.debug( f'Chunking VM requests into {len(uuids_chunked)} chunks') vms = list() api_responding = list() for uuid_list in uuids_chunked: vm_chunks, api_chunk_responding = self.get_resources( target, token, adapterkind="VMWARE", resourcekinds=[resourcekind], uuids=uuid_list, query_specs=q_specs) vms.extend(vm_chunks) api_responding.append(api_chunk_responding) logger.debug(f'Number of VMs collected: {len(vms)}') return vms, max(api_responding) return self.get_resources(target, token, adapterkind="VMWARE", resourcekinds=[resourcekind], uuids=parent_uuids, query_specs=q_specs)