def __init__(self): self.ceilo_accessor = CeiloAccessor() self.redis_accessor = RedisAccessor()
class MongoServiceWrapper: def __init__(self): self.ceilo_accessor = CeiloAccessor() self.redis_accessor = RedisAccessor() def get_all_matrix_ts(self, vm_ids, counter_name_list, time_start, time_end): vms = self.redis_accessor.get_item( RedisKeyConstants.API_RAW_VMS)['servers'] all_vms = {} for vm in vms: all_vms[vm['id']] = vm all_ts_matrix = {} counter_names = [] t = time.time() retrieve_cpu_or_memory = False if 'CPU' in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_CPU) if 'Memory' in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_MEMORY) if "Disk" in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_DISK_READ) counter_names.append(CeiloAccessor.COUNTER_NAME_DISK_WRITE) if retrieve_cpu_or_memory: all_ts_matrix = self.ceilo_accessor.batch_get_resource_ts( vm_ids, time_start, time_end, counter_names) print('cpu, memory and disk: ' + str(time.time() - t)) if 'Volume' in counter_name_list: self.__get_volume_ts_series(all_ts_matrix, vm_ids, time_start, time_end) if 'Network' in counter_name_list: self.__get_network_ts_series(all_ts_matrix, vm_ids, time_start, time_end) return all_ts_matrix def __get_volume_ts_series(self, all_ts_matrix, vm_ids, time_start, time_end): vm_volumes = self.redis_accessor.get_item( RedisKeyConstants.API_PROCESSED_VM_VOLUMES) volume_ids = [] volume_vm_index = {} for vm_id in vm_ids: if vm_volumes.get(vm_id): volumes = vm_volumes.get(vm_id)['volumes'] if volumes: for volume in volumes: volume_ids.append(volume['id']) volume_vm_index.__setitem__(volume['id'], vm_id) else: print("vm: " + vm_id + " has no volume.") volume_points = self.ceilo_accessor.batch_get_resource_ts( volume_ids, time_start, time_end, [ CeiloAccessor.COUNTER_NAME_VOLUME_READ, CeiloAccessor.COUNTER_NAME_VOLUME_WRITE ]) if volume_points: for vm_id in vm_ids: if not all_ts_matrix.get(vm_id): all_ts_matrix.__setitem__(vm_id, {}) all_ts_matrix.get(vm_id).__setitem__( CeiloAccessor.COUNTER_NAME_VOLUME_READ, {}) all_ts_matrix.get(vm_id).__setitem__( CeiloAccessor.COUNTER_NAME_VOLUME_WRITE, {}) for r_id in volume_points.keys(): disk_read_points = volume_points.get(r_id).get( CeiloAccessor.COUNTER_NAME_VOLUME_READ) disk_write_points = volume_points.get(r_id).get( CeiloAccessor.COUNTER_NAME_VOLUME_WRITE) if disk_read_points: all_ts_matrix.get(volume_vm_index.get(r_id))[ CeiloAccessor.COUNTER_NAME_VOLUME_READ].__setitem__( r_id, disk_read_points) if disk_write_points: all_ts_matrix.get(volume_vm_index.get(r_id))[ CeiloAccessor.COUNTER_NAME_VOLUME_WRITE].__setitem__( r_id, disk_write_points) def __get_network_ts_series(self, all_ts_matrix, vm_ids, time_start, time_end): vm_taps = self.redis_accessor.get_item( RedisKeyConstants.API_PROCESSED_VM_PORTS) tap_ids = [] tap_vm_index = {} for vm_id in vm_ids: taps = vm_taps.get(vm_id) if taps: for tap in taps: tap_ids.append(tap) tap_vm_index.__setitem__(tap, vm_id) t = time.time() net_work_points = self.ceilo_accessor.batch_get_resource_ts( tap_ids, time_start, time_end, [ CeiloAccessor.COUNTER_NAME_NETWORK_IN, CeiloAccessor.COUNTER_NAME_NETWORK_OUT ]) print('network-search:' + str(time.time() - t)) t = time.time() if net_work_points: for vm_id in vm_ids: if vm_id not in all_ts_matrix: all_ts_matrix.__setitem__(vm_id, {}) all_ts_matrix.get(vm_id).__setitem__( CeiloAccessor.COUNTER_NAME_NETWORK_IN, {}) all_ts_matrix.get(vm_id).__setitem__( CeiloAccessor.COUNTER_NAME_NETWORK_OUT, {}) for resource_id in net_work_points.keys(): network_in_points = net_work_points.get(resource_id).get( CeiloAccessor.COUNTER_NAME_NETWORK_IN) network_out_points = net_work_points.get(resource_id).get( CeiloAccessor.COUNTER_NAME_NETWORK_OUT) if network_in_points: all_ts_matrix.get(tap_vm_index.get(resource_id))[ CeiloAccessor.COUNTER_NAME_NETWORK_IN].__setitem__( resource_id, network_in_points) if network_out_points: all_ts_matrix.get(tap_vm_index.get(resource_id))[ CeiloAccessor.COUNTER_NAME_NETWORK_OUT].__setitem__( resource_id, network_out_points) print('network-post-search:' + str(time.time() - t)) def get_vm_trend(self, time_range, counter_names, vm_ids): """ get the time series points for assigned vm_ids, counter_names between the time range""" time_range_boundary = time_range.split('-') time_start = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(int(time_range_boundary[0][:10]))) time_end = time.strftime( '%Y-%m-%d %H:%M:%S', time.localtime(int(time_range_boundary[1][:10]))) l = vm_ids.split('|') vm_list = l[:l.__len__() - 1] c = counter_names.split('|') counter_name_list = c[:c.__len__() - 1] all_points = self.get_all_matrix_ts(vm_list, counter_name_list, time_start, time_end) import pdb pdb.set_trace() print(json.dumps(all_points, indent=2)) return all_points
class MongoServiceWrapper: def __init__(self): self.ceilo_accessor = CeiloAccessor() self.redis_accessor = RedisAccessor() def get_all_matrix_ts(self, vm_ids, counter_name_list, time_start, time_end): vms = self.redis_accessor.get_item(RedisKeyConstants.API_RAW_VMS)["servers"] all_vms = {} for vm in vms: all_vms[vm["id"]] = vm all_ts_matrix = {} counter_names = [] t = time.time() retrieve_cpu_or_memory = False if "CPU" in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_CPU) if "Memory" in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_MEMORY) if "Disk" in counter_name_list: retrieve_cpu_or_memory = True counter_names.append(CeiloAccessor.COUNTER_NAME_DISK_READ) counter_names.append(CeiloAccessor.COUNTER_NAME_DISK_WRITE) if retrieve_cpu_or_memory: all_ts_matrix = self.ceilo_accessor.batch_get_resource_ts(vm_ids, time_start, time_end, counter_names) print("cpu, memory and disk: " + str(time.time() - t)) if "Volume" in counter_name_list: self.__get_volume_ts_series(all_ts_matrix, vm_ids, time_start, time_end) if "Network" in counter_name_list: self.__get_network_ts_series(all_ts_matrix, vm_ids, time_start, time_end) return all_ts_matrix def __get_volume_ts_series(self, all_ts_matrix, vm_ids, time_start, time_end): vm_volumes = self.redis_accessor.get_item(RedisKeyConstants.API_PROCESSED_VM_VOLUMES) volume_ids = [] volume_vm_index = {} for vm_id in vm_ids: if vm_volumes.get(vm_id): volumes = vm_volumes.get(vm_id)["volumes"] if volumes: for volume in volumes: volume_ids.append(volume["id"]) volume_vm_index.__setitem__(volume["id"], vm_id) else: print("vm: " + vm_id + " has no volume.") volume_points = self.ceilo_accessor.batch_get_resource_ts( volume_ids, time_start, time_end, [CeiloAccessor.COUNTER_NAME_VOLUME_READ, CeiloAccessor.COUNTER_NAME_VOLUME_WRITE], ) if volume_points: for vm_id in vm_ids: if not all_ts_matrix.get(vm_id): all_ts_matrix.__setitem__(vm_id, {}) all_ts_matrix.get(vm_id).__setitem__(CeiloAccessor.COUNTER_NAME_VOLUME_READ, {}) all_ts_matrix.get(vm_id).__setitem__(CeiloAccessor.COUNTER_NAME_VOLUME_WRITE, {}) for r_id in volume_points.keys(): disk_read_points = volume_points.get(r_id).get(CeiloAccessor.COUNTER_NAME_VOLUME_READ) disk_write_points = volume_points.get(r_id).get(CeiloAccessor.COUNTER_NAME_VOLUME_WRITE) if disk_read_points: all_ts_matrix.get(volume_vm_index.get(r_id))[CeiloAccessor.COUNTER_NAME_VOLUME_READ].__setitem__( r_id, disk_read_points ) if disk_write_points: all_ts_matrix.get(volume_vm_index.get(r_id))[CeiloAccessor.COUNTER_NAME_VOLUME_WRITE].__setitem__( r_id, disk_write_points ) def __get_network_ts_series(self, all_ts_matrix, vm_ids, time_start, time_end): vm_taps = self.redis_accessor.get_item(RedisKeyConstants.API_PROCESSED_VM_PORTS) tap_ids = [] tap_vm_index = {} for vm_id in vm_ids: taps = vm_taps.get(vm_id) if taps: for tap in taps: tap_ids.append(tap) tap_vm_index.__setitem__(tap, vm_id) t = time.time() net_work_points = self.ceilo_accessor.batch_get_resource_ts( tap_ids, time_start, time_end, [CeiloAccessor.COUNTER_NAME_NETWORK_IN, CeiloAccessor.COUNTER_NAME_NETWORK_OUT], ) print("network-search:" + str(time.time() - t)) t = time.time() if net_work_points: for vm_id in vm_ids: if vm_id not in all_ts_matrix: all_ts_matrix.__setitem__(vm_id, {}) all_ts_matrix.get(vm_id).__setitem__(CeiloAccessor.COUNTER_NAME_NETWORK_IN, {}) all_ts_matrix.get(vm_id).__setitem__(CeiloAccessor.COUNTER_NAME_NETWORK_OUT, {}) for resource_id in net_work_points.keys(): network_in_points = net_work_points.get(resource_id).get(CeiloAccessor.COUNTER_NAME_NETWORK_IN) network_out_points = net_work_points.get(resource_id).get(CeiloAccessor.COUNTER_NAME_NETWORK_OUT) if network_in_points: all_ts_matrix.get(tap_vm_index.get(resource_id))[CeiloAccessor.COUNTER_NAME_NETWORK_IN].__setitem__( resource_id, network_in_points ) if network_out_points: all_ts_matrix.get(tap_vm_index.get(resource_id))[CeiloAccessor.COUNTER_NAME_NETWORK_OUT].__setitem__( resource_id, network_out_points ) print("network-post-search:" + str(time.time() - t)) def get_vm_trend(self, time_range, counter_names, vm_ids): """ get the time series points for assigned vm_ids, counter_names between the time range""" time_range_boundary = time_range.split("-") time_start = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time_range_boundary[0][:10]))) time_end = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time_range_boundary[1][:10]))) l = vm_ids.split("|") vm_list = l[: l.__len__() - 1] c = counter_names.split("|") counter_name_list = c[: c.__len__() - 1] all_points = self.get_all_matrix_ts(vm_list, counter_name_list, time_start, time_end) import pdb pdb.set_trace() print(json.dumps(all_points, indent=2)) return all_points