def get_cpu_process(ip, snmp_config_data): """[Get cpu process by bulksnmp] Arguments: ip {[string]} -- [ip of server(ex: 192.168.88.88)] snmp_config_data {[dict]} -- [snmp config get from config file] Raises: Exception: [errorIndication: True value indicates SNMP engine error] Exception: [errorStatus: True value indicates SNMP PDU error] Returns: [float] -- [cpu process usage] """ # Call bulk get snmp data of cpu processor snmp_iter = get_bulk_snmp(processor_load_oid, ip, snmp_config_data) list_cpu_process = [] for errorIndication, errorStatus, errorIndex, varBinds in snmp_iter: # Check for errors and print out results if errorIndication: # SNMP engine errors raise Exception(errorIndication) elif errorStatus: # SNMP agent errors raise Exception( '%s at %s' % (errorStatus.prettyPrint(), varBinds[int(errorIndex) - 1] if errorIndex else '?')) for varBind in varBinds: logger.debug("{}-varBind-{}".format(ip, varBind)) list_cpu_process.append(int(varBind[1])) if len(list_cpu_process) > 0: cpu_process = sum(list_cpu_process) / len(list_cpu_process) else: cpu_process = 0 return cpu_process
def make_template_statistic_object(address, server_type, server_name, memory_usage, cpu_usage, queries=0, udf='', dns_oper_state=2): """[Make template statistic object] :param address: address of Server -- string :param server_type: Type of Server -- string :param server_name: Name of Server -- string :param memory_usage: Memory usage of Server -- int :param cpu_usage: CPU usage of Server -- int :param queries: default 0 :param udf: default '' :return: [result_object] -- dict """ result_object = dict(address=address, server_type=server_type, server_name=server_name, udf=udf, memory_usage=memory_usage, cpu_usage=cpu_usage) if server_type == ServerType.BDDS: result_object.update(queries=queries) app_status = 'success' if dns_oper_state == 1 else 'fail' result_object.update(app_status=app_status) logger.debug("Result object: {}".format(result_object)) return result_object
def get_memory_usage(var_binds): """ :param var_binds: :return: """ try: total_mem = var_binds[0][-1] total_mem_free = var_binds[1][-1] used_mem = total_mem - total_mem_free used_mem_percent = (float(used_mem) / float(total_mem)) * 100 logger.debug("Memory_usage: {}".format(used_mem_percent)) return float(used_mem_percent) except IndexError: logger.error(traceback.format_exc()) return None, None
def get_memcached_config(): """[Get memcached config from NFV_CONFIG_PATH] Raises: Exception -- [Can not get config memcached from NFV_CONFIG_PATH] Returns: [String] -- [memcached_host] [Int] -- [memcached_host] """ data_config = read_config_json_file(NFV_CONFIG_PATH) try: memcached_host = data_config['memcached_host'] memcached_port = data_config['memcached_port'] except KeyError: raise KeyError("Can not get config memcached from NFV_CONFIG_PATH") logger.debug("Memcached_host: {} - Memcached_port: {}".format( memcached_host, memcached_port)) return memcached_host, int(memcached_port)
def call_k1_api(result_object, timeout=1): """[Call K1 API] :param result_object: :param timeout: default 1 :return: """ if not result_object: logger.debug("Result_object is none") return payload = prepare_payload_for_k1(result_object) logger.info(f'Payload of K1: {payload}') if is_kpi_none(payload): logger.info(f'KPIs are none. Do not call api k1') return data_config = read_config_json_file(NFV_CONFIG_PATH) headers = { 'Content-Type': 'application/json;charset=UTF-8', } try: host = data_config['k1_api']['address'] port = data_config['k1_api']['port'] uri = data_config['k1_api']['uri'] response = requests.post(f"http://{host}:{port}{uri}", headers=headers, data=json.dumps(payload), timeout=timeout) result_call = { 'content': response.content.decode('utf-8'), 'status_code': response.status_code } logger.info(f'Result call api k1: {result_call}') except KeyError as key_error: logger.error("Cannot get {} in config file {}".format(key_error)) logger.debug(traceback.format_exc()) except requests.RequestException: logger.error("Cannot request api to {}".format( data_config['k1_api']['address'])) logger.error("Payload of the failed request: {}".format(payload)) logger.debug(traceback.format_exc())
def collect_statistics(mem_nfv): """ :return: Array of statistic results """ # Get server list from memcached list_bdds, list_bam, list_vmhosts = mem_nfv.get_list_servers() logger.debug("List_bdds: {}\nList_bam: {}\nList_vmhost: {}".format( list_bdds, list_bam, list_vmhosts)) snmp_config = read_config_json_file(SNMP_CONFIG_PATH) logger.debug("Snmp config: {}".format(snmp_config)) list_servers = [] logger.info(f'List BDDS Size: {len(list_bdds)}') logger.info(f'Begin loop through list_bdds') for bdds in list_bdds: logger.debug(f'BDDS: {bdds}') bdds_config_name = get_snmp_server_config_name(snmp_config, bdds.name) logger.debug(f'BDDS config name {bdds_config_name}') try: list_servers.append({ 'server_type': ServerType.BDDS, 'server_name': bdds.name, 'address': bdds.ipv4_address, 'snmp_config_data': snmp_config[bdds_config_name], 'udf': bdds.udf }) except KeyError as exception: logger.error(f'Exception Key Error {exception}') logger.error(traceback.format_exc()) continue logger.info(f'List BAM Size: {len(list_bam)}') logger.info(f'Begin loop through list_bam') for bam in list_bam: try: logger.debug(f'BAM: {bam}') logger.debug(f'bam_name: {bam.name}') bam_config_name = get_snmp_server_config_name( snmp_config, bam.name) logger.debug(f'Bam config name {bam_config_name}') try: logger.info( f'Begin Append BAM server list Server {list_servers} ') list_servers.append({ 'server_type': ServerType.BAM, 'server_name': bam.name, 'address': bam.ipv4_address, 'snmp_config_data': snmp_config[bam_config_name] }) logger.info(f'End append BAM ===> List Server {list_servers}') except KeyError as exception: logger.error(f'Exception Key Error {exception}') logger.error(traceback.format_exc()) continue except Exception as exception: logger.info(f'Cant get bam.ipv4_address: {exception}') logger.info(f'List VMHOST Size: {len(list_vmhosts)}') logger.info(f'Begin loop through list_vmhosts') for vm_host in list_vmhosts: try: logger.debug(f'VM_HOST: {vm_host}') logger.debug(f'vm_name: {vm_host.name}') vm_host_config_name = get_snmp_server_config_name( snmp_config, vm_host.name) logger.debug(f'VM_HOST config name {vm_host_config_name}') try: logger.info( f'Begin Append VM_HOST server list Server {list_servers} ' ) list_servers.append({ 'server_type': ServerType.VM_HOST, 'server_name': vm_host.name, 'address': vm_host.ipv4_address, 'snmp_config_data': snmp_config[vm_host_config_name] }) logger.info( f'End append VM_HOST ===> List Server {list_servers}') except KeyError as exception: logger.error(f'Exception Key Error {exception}') logger.error(traceback.format_exc()) continue except Exception as exception: logger.info(f'Can not get vm_host.ip_address: {exception}') logger.info(f'Begin get statistic with list server {list_servers}') result = [] with PoolExecutor(max_workers=10) as executor: for result_object in executor.map(get_server_statistic, list_servers): result.append(result_object) return result