Esempio n. 1
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()

    subprocrunner.SubprocessRunner.is_save_history = True
    if options.tc_command_output != TcCoomandOutput.NOT_SET:
        subprocrunner.SubprocessRunner.is_dry_run = True

    tc_param = {}
    for device in options.device:
        try:
            verify_network_interface(device)
        except NetworkInterfaceNotFoundError as e:
            logger.debug(str(e))
            continue

        tc_param.update(TcShapingRuleParser(device, logger).get_tc_parameter())

    command_history = "\n".join(SubprocessRunner.get_history())

    if options.tc_command_output == TcCoomandOutput.STDOUT:
        print(command_history)
        return 0

    if options.tc_command_output == TcCoomandOutput.SCRIPT:
        write_tc_script("tcshow", command_history)
        return 0

    logger.debug("command history\n{}".format(command_history))
    print(json.dumps(tc_param, indent=4))

    return 0
Esempio n. 2
0
def main():
    options = parse_option()
    logger = logbook.Logger("tcdel")
    logger.level = options.log_level

    subprocrunner.logger.level = options.log_level
    if options.quiet:
        subprocrunner.logger.disable()
    else:
        subprocrunner.logger.enable()

    subprocrunner.Which("tc").verify()

    try:
        verify_network_interface(options.device)
    except NetworkInterfaceNotFoundError as e:
        logger.error(e)
        return 1

    tc = TrafficControl(options.device)

    try:
        return tc.delete_tc()
    except NetworkInterfaceNotFoundError as e:
        logger.debug(e)
        return 0

    return 1
Esempio n. 3
0
def main():
    options = parse_option()
    logger = logbook.Logger("tcshow")
    logger.level = options.log_level

    subprocrunner.logger.level = options.log_level
    if options.quiet:
        subprocrunner.logger.disable()
    else:
        subprocrunner.logger.enable()

    subprocrunner.Which("tc").verify()

    tc_param = {}
    for device in options.device:
        try:
            verify_network_interface(device)
        except NetworkInterfaceNotFoundError as e:
            logger.debug(str(e))
            continue

        tc_param.update(TcParamParser(device, logger).get_tc_parameter())

    six.print_(json.dumps(tc_param, indent=4))

    return 0
Esempio n. 4
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()
    try:
        verify_netem_module()
    except ModuleNotFoundError as e:
        logger.debug(str(e))
    except subprocrunner.CommandNotFoundError as e:
        logger.error(str(e))

    if dataproperty.is_not_empty_string(options.config_file):
        return set_tc_from_file(logger, options.config_file, options.overwrite)

    tc = TrafficControl(options.device,
                        direction=options.direction,
                        bandwidth_rate=options.bandwidth_rate,
                        latency_ms=options.network_latency,
                        latency_distro_ms=options.latency_distro_ms,
                        packet_loss_rate=options.packet_loss_rate,
                        corruption_rate=options.corruption_rate,
                        network=options.network,
                        src_network=options.src_network,
                        port=options.port,
                        is_add_shaper=options.is_add_shaper,
                        is_enable_iptables=options.is_enable_iptables,
                        shaping_algorithm=options.shaping_algorithm)

    try:
        tc.validate()
    except (NetworkInterfaceNotFoundError, ValueError) as e:
        logger.error(str(e))
        return 1

    if options.overwrite:
        if options.log_level == logbook.INFO:
            set_log_level(logbook.ERROR)

        try:
            tc.delete_tc()
        except NetworkInterfaceNotFoundError:
            pass

        set_log_level(options.log_level)

    tc.set_tc()

    return 0
Esempio n. 5
0
def find_bin_path(command):
    if command in _bin_path_cache:
        return _bin_path_cache.get(command)

    bin_path = spr.Which(command)
    if bin_path.is_exist():
        _bin_path_cache[command] = bin_path.abspath()
        return _bin_path_cache[command]

    for sbin_path in ("/sbin/{:s}".format(command), "/usr/sbin/{:s}".format(command)):
        if os.path.isfile(sbin_path):
            _bin_path_cache[command] = sbin_path
            return _bin_path_cache[command]

    return None
Esempio n. 6
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()

    try:
        verify_network_interface(options.device)
    except NetworkInterfaceNotFoundError as e:
        logger.error(e)
        return errno.EINVAL

    tc = TrafficControl(options.device)
    if options.log_level == logbook.INFO:
        subprocrunner.set_log_level(logbook.ERROR)

    subprocrunner.SubprocessRunner.is_save_history = True
    if options.tc_command_output != TcCoomandOutput.NOT_SET:
        subprocrunner.SubprocessRunner.default_is_dry_run = True

    if options.tc_command_output != TcCoomandOutput.NOT_SET:
        set_logger(False)

    try:
        return_code = tc.delete_tc()
    except NetworkInterfaceNotFoundError as e:
        logger.debug(e)
        return 0

    command_history = "\n".join(tc.get_command_history())

    if options.tc_command_output == TcCoomandOutput.STDOUT:
        print(command_history)
        return return_code

    if options.tc_command_output == TcCoomandOutput.SCRIPT:
        set_logger(True)
        write_tc_script("tcdel",
                        command_history,
                        filename_suffix=options.device)
        return return_code

    logger.debug("command history\n{}".format(command_history))

    return return_code
Esempio n. 7
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()

    tc_param = {}
    for device in options.device:
        try:
            verify_network_interface(device)
        except NetworkInterfaceNotFoundError as e:
            logger.debug(str(e))
            continue

        tc_param.update(TcShapingRuleParser(device, logger).get_tc_parameter())

    six.print_(json.dumps(tc_param, indent=4))

    return 0
Esempio n. 8
0
def find_bin_path(command):
    def _to_regular_bin_path(file_path):
        path_obj = Path(file_path)
        if path_obj.islink():
            return path_obj.readlinkabs()

        return file_path

    if command in _bin_path_cache:
        return _bin_path_cache.get(command)

    bin_path = spr.Which(command, follow_symlinks=True)
    if bin_path.is_exist():
        _bin_path_cache[command] = bin_path.abspath()
        return _bin_path_cache[command]

    for sbin_path in ("/sbin/{:s}".format(command), "/usr/sbin/{:s}".format(command)):
        if os.path.isfile(sbin_path):
            _bin_path_cache[command] = _to_regular_bin_path(sbin_path)
            return _bin_path_cache[command]

    # return the command as it is when binary file not found
    return command
Esempio n. 9
0
def find_bin_path(command):
    def _to_regular_bin_path(file_path):
        path_obj = Path(file_path)
        if path_obj.islink():
            return path_obj.readlinkabs()

        return file_path

    if command in _bin_path_cache:
        return _bin_path_cache.get(command)

    bin_path = spr.Which(command)
    if bin_path.is_exist():
        _bin_path_cache[command] = _to_regular_bin_path(bin_path.abspath())
        return _bin_path_cache[command]

    for sbin_path in ("/sbin/{:s}".format(command),
                      "/usr/sbin/{:s}".format(command)):
        if os.path.isfile(sbin_path):
            _bin_path_cache[command] = _to_regular_bin_path(sbin_path)
            return _bin_path_cache[command]

    return None
Esempio n. 10
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()

    try:
        verify_network_interface(options.device)
    except NetworkInterfaceNotFoundError as e:
        logger.error(e)
        return 1

    tc = TrafficControl(options.device)
    if options.log_level == logbook.INFO:
        subprocrunner.set_log_level(logbook.ERROR)

    try:
        return tc.delete_tc()
    except NetworkInterfaceNotFoundError as e:
        logger.debug(e)
        return 0

    return 1
Esempio n. 11
0
def check_tc_command_installation():
    try:
        spr.Which("tc").verify()
    except spr.CommandNotFoundError as e:
        logger.error(e)
        sys.exit(errno.ENOENT)
Esempio n. 12
0
def check_tc_command_installation():
    try:
        spr.Which("tc").verify()
    except spr.CommandNotFoundError as e:
        logger.error("{:s}: {}".format(e.__class__.__name__, e))
        sys.exit(errno.ENOENT)
Esempio n. 13
0
def main():
    options = parse_option()

    set_log_level(options.log_level)

    subprocrunner.Which("tc").verify()
    try:
        verify_netem_module()
    except ModuleNotFoundError as e:
        logger.debug(str(e))
    except subprocrunner.CommandNotFoundError as e:
        logger.error(str(e))

    if typepy.is_not_null_string(options.config_file):
        return set_tc_from_file(logger, options.config_file, options.overwrite)

    tc = TrafficControl(
        options.device,
        direction=options.direction,
        bandwidth_rate=options.bandwidth_rate,
        latency_ms=options.network_latency,
        latency_distro_ms=options.latency_distro_ms,
        packet_loss_rate=options.packet_loss_rate,
        corruption_rate=options.corruption_rate,
        network=options.network,
        src_network=options.src_network,
        port=options.port,
        is_add_shaper=options.is_add_shaper,
        is_enable_iptables=options.is_enable_iptables,
        shaping_algorithm=options.shaping_algorithm,
        tc_command_output=options.tc_command_output,
    )

    try:
        tc.validate()
    except (NetworkInterfaceNotFoundError, ValueError) as e:
        logger.error(str(e))
        return errno.EINVAL

    subprocrunner.SubprocessRunner.is_save_history = True
    if options.tc_command_output != TcCoomandOutput.NOT_SET:
        subprocrunner.SubprocessRunner.is_dry_run = True

    if options.overwrite:
        if options.log_level == logbook.INFO:
            set_log_level(logbook.ERROR)

        try:
            tc.delete_tc()
        except NetworkInterfaceNotFoundError:
            pass

        set_log_level(options.log_level)

    tc.set_tc()
    command_history = "\n".join(tc.get_command_history())

    if options.tc_command_output == TcCoomandOutput.STDOUT:
        print(command_history)
        return 0

    if options.tc_command_output == TcCoomandOutput.SCRIPT:
        write_tc_script(
            "tcset", command_history, filename_suffix=options.device)
        return 0

    logger.debug("command history\n{}".format(command_history))

    return 0
Esempio n. 14
0
def pager(text):
    if subprocrunner.Which("less").is_exist():
        pydoc.pipepager(text, cmd="less --chop-long-lines --CLEAR-SCREEN")
    else:
        pydoc.pager(text)