예제 #1
0
def get_interface_packet_output_rate(device,
                                     interface,
                                     seconds=60,
                                     field='out_pkts'):
    """ Get rate from out_pkts by taking average across the defined seconds

        Args:
            device ('obj'): Device object
            interface ('str'): Interface name
            seconds ('int'): Seconds to wait between show commands
            field ('str'): Used for get_interface_packet_input_rate

        Returns:
            Traffic rate

            if any error return None
            - to separate rate 0.0 and None value

        Raises:
            None
    """

    if seconds <= 0:
        return

    try:

        output_before = device.execute(
            "show interfaces {intf}".format(intf=interface))

        log.info("Waiting {secs} seconds".format(secs=seconds))
        time.sleep(seconds)

        output_after = device.execute(
            "show interfaces {intf}".format(intf=interface))

        parsed_output_before = device.parse(
            "show interfaces {intf}".format(intf=interface),
            output=output_before,
        )
        parsed_output_after = device.parse(
            "show interfaces {intf}".format(intf=interface),
            output=output_after,
        )

        delta_time = get_delta_time_from_outputs(output_before=output_before,
                                                 output_after=output_after)

        counter_before = get_interface_packet_counter(
            device=device,
            interface=interface,
            counter_field=field,
            output=parsed_output_before,
        )
        if counter_before is None:
            return

        counter_after = get_interface_packet_counter(
            device=device,
            interface=interface,
            counter_field=field,
            output=parsed_output_after,
        )
        if counter_after is None:
            return

        rate = round((counter_after - counter_before) / delta_time, 2)

    except SchemaEmptyParserError as e:
        return
    except ValueError as e:
        return

    if 'out_pkts' in field:
        log.info("Packet output rate for interface {intf} is {count}".format(
            intf=interface, count=rate))
    elif 'in_pkts' in field:
        log.info("Packet input rate for interface {intf} is {count}".format(
            intf=interface, count=rate))

    return rate
예제 #2
0
파일: get.py 프로젝트: vkoprivica/genielibs
def get_interface_packet_output_rate(device, interface, seconds=60):
    """ Get rate of output packet in interface seconds apart

        Args:
            device ('obj'): Device object
            interface ('str'): Interface name
            seconds ('int'): Seconds to wait between show commands

        Returns:
            Traffic rate

        Raises:
            None
    """

    if seconds <= 0:
        return

    try:

        output_before = device.execute(
            "show interfaces {intf}".format(intf=interface))

        log.info("Waiting {secs} seconds".format(secs=seconds))
        time.sleep(seconds)

        output_after = device.execute(
            "show interfaces {intf}".format(intf=interface))

        parsed_output_before = device.parse(
            "show interfaces {intf}".format(intf=interface),
            output=output_before,
        )
        parsed_output_after = device.parse(
            "show interfaces {intf}".format(intf=interface),
            output=output_after,
        )

        delta_time = get_delta_time_from_outputs(output_before=output_before,
                                                 output_after=output_after)

        counter_before = get_interface_packet_counter(
            device=device,
            interface=interface,
            counter_field="out_pkts",
            output=parsed_output_before,
        )
        if not counter_before:
            return

        counter_after = get_interface_packet_counter(
            device=device,
            interface=interface,
            counter_field="out_pkts",
            output=parsed_output_after,
        )
        if not counter_after:
            return

        output_rate = round((counter_after - counter_before) / delta_time, 2)

    except SchemaEmptyParserError as e:
        return
    except ValueError as e:
        return

    log.info("Packet output rate for interface {intf} is {count}".format(
        intf=interface, count=output_rate))

    return output_rate