def request_data(api, bam_ip):
    """
    Request data
    :param api: API
    :param bam_ip: IP of BAM -- IPv4
    :return:
    [Result] -- String
    """
    result = None
    try:
        user_name, password, gateway_url, _ = common.get_configuration()
        if not user_name or not password:
            return
        bam_url = "http://{}/Services/API?wsdl".format(bam_ip)
        login_session = login(user_name, password, gateway_url, bam_url)
        if not login_session:
            return
        url = "{}/{}".format(gateway_url, api)
        logger.info("Gateway_access-request_data - {}".format(url))
        ts_request = login_session.get(url)
        logout(login_session, gateway_url)
        if ts_request.status_code != 200:
            return
        result = json.loads(ts_request.text)
    except Exception as exception:
        logger.error("Gateway_access-request_data - {}".format(exception))
        logger.error(traceback.format_exc())
    return result
示例#2
0
def get_server_statistic(server, call_k1=True):
    """
    :param server: Json object
    :param call_k1: bool
    :return: template statistic object -- Json

    """
    result_object = None
    try:
        if server['server_type'] == ServerType.BAM or server[
                'server_type'] == ServerType.VM_HOST:
            memory_usage, cpu_usage = server_bam_statistic(
                server['address'], server['snmp_config_data'])
            result_object = make_template_statistic_object(
                server['address'], server['server_type'],
                server['server_name'], memory_usage, cpu_usage)
        elif server['server_type'] == ServerType.BDDS:
            memory_usage, cpu_usage, dns_oper_state, queries = server_bdds_statistic(
                server['address'], server['snmp_config_data'])
            result_object = make_template_statistic_object(
                server['address'], server['server_type'],
                server['server_name'], memory_usage, cpu_usage, queries,
                server['udf'], dns_oper_state)
    except Exception as ex:
        logger.error("{}: {}-{}".format(server['server_name'], type(ex), ex))
        logger.error(traceback.format_exc())

    logger.info(f'get_server_statistic: {result_object}')

    if call_k1:
        k1.call_k1_api(result_object, 5)

    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 server_number_queries(var_binds):
    """
    :param var_binds:
    :return:
    """
    if var_binds:
        try:
            success = var_binds[2][-1]
            referral = var_binds[3][-1]
            nxrrset = var_binds[4][-1]
            nxdomain = var_binds[5][-1]
            failure = var_binds[6][-1]
            return int(success + referral + nxrrset + nxdomain + failure)
        except IndexError as exception:
            logger.error(traceback.format_exc())
            return None
示例#5
0
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())
示例#6
0
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