Example #1
0
 def __init__(self, instance_paths, testcase_parameters):
     self.__instance_paths = instance_paths
     self.__console_log_reader = ConsoleLogReader(instance_paths)
     self.__syslog_ng_executor = SyslogNgExecutor(instance_paths)
     self.__syslog_ng_ctl = SyslogNgCtl(instance_paths)
     self.__valgrind_usage = testcase_parameters.get_valgrind_usage()
     self.__process = None
    def __init__(self, group_type, driver_name):
        if group_type == "destination":
            statement_short_name = "dst"
        elif group_type == "source":
            statement_short_name = "src"
        else:
            raise Exception("Unknown group_type: {}".format(group_type))

        self.component = "{}.{}".format(statement_short_name, driver_name)
        self.syslog_ng_ctl = SyslogNgCtl(tc_parameters.INSTANCE_PATH)
Example #3
0
 def __init__(self, instance_paths, testcase_parameters):
     self.__instance_paths = instance_paths
     self.__console_log_reader = ConsoleLogReader(instance_paths)
     self.__syslog_ng_executor = SyslogNgExecutor(instance_paths)
     self.__syslog_ng_ctl = SyslogNgCtl(instance_paths)
     self.__valgrind_usage = testcase_parameters.get_valgrind_usage()
     self.__process = None
Example #4
0
 def __init__(self, instance_paths, testcase_parameters):
     self.__instance_paths = instance_paths
     self.__console_log_reader = ConsoleLogReader(instance_paths)
     self.__syslog_ng_executor = SyslogNgExecutor(instance_paths)
     self.__syslog_ng_ctl = SyslogNgCtl(instance_paths)
     self.__external_tool = testcase_parameters.get_external_tool()
     self.__process = None
     self.__stderr = None
     self.__debug = None
     self.__trace = None
     self.__verbose = None
     self.__startup_debug = None
     self.__no_caps = None
     self.__config_path = None
     self.__persist_path = None
     self.__pid_path = None
     self.__control_socket_path = None
Example #5
0
class DriverStatsHandler(object):
    def __init__(self, group_type, driver_name):
        if group_type == "destination":
            statement_short_name = "dst"
            self.component = "{}.{}".format(statement_short_name, driver_name)
        elif group_type == "source":
            statement_short_name = "src"
            self.component = "{}.{}".format(statement_short_name, driver_name)
        elif group_type == "parser":
            self.component = "{}".format(group_type)
        else:
            raise Exception("Unknown group_type: {}".format(group_type))

        self.syslog_ng_ctl = SyslogNgCtl(tc_parameters.INSTANCE_PATH)

    def build_query_pattern(self):
        return self.build_pattern(delimiter=".")

    def build_stats_pattern(self):
        return self.build_pattern(delimiter=";")

    def build_pattern(self, delimiter):
        return "{}{}".format(
            self.component,
            delimiter,
        )

    def parse_result(self, result, result_type):
        statistical_elements = [
            "memory_usage", "written", "processed", "dropped", "queued",
            "stamp", "discarded"
        ]
        parsed_output = {}
        for stat_element in statistical_elements:
            for line in result:
                if stat_element in line:
                    if result_type == "query":
                        parsed_output.update({
                            stat_element:
                            int(line.split(".")[-1].split("=")[-1])
                        })
                    elif result_type == "stats":
                        parsed_output.update({
                            stat_element:
                            int(line.split(".")[-1].split(";")[-1])
                        })
                    else:
                        raise Exception(
                            "Unknown result_type: {}".format(result_type))
        return parsed_output

    def filter_stats_result(self, stats_result, stats_pattern):
        filtered_list = []
        for stats_line in stats_result:
            if stats_pattern in stats_line:
                filtered_list.append(stats_line)
        return filtered_list

    def get_query(self):
        query_pattern = self.build_query_pattern()
        query_result = self.syslog_ng_ctl.query(
            pattern="*{}*".format(query_pattern))['stdout'].splitlines()

        return self.parse_result(result=query_result, result_type="query")

    def get_stats(self):
        stats_pattern = self.build_stats_pattern()
        stats_result = self.syslog_ng_ctl.stats()['stdout'].splitlines()

        filtered_stats_result = self.filter_stats_result(
            stats_result, stats_pattern)
        return self.parse_result(result=filtered_stats_result,
                                 result_type="stats")
Example #6
0
def syslog_ng_ctl(syslog_ng):
    return SyslogNgCtl(syslog_ng.instance_paths)
Example #7
0
class SyslogNgCli(object):
    def __init__(self, logger_factory, instance_paths, testcase_parameters):
        self.__instance_paths = instance_paths
        self.__console_log_reader = ConsoleLogReader(logger_factory,
                                                     instance_paths)
        self.__logger = logger_factory.create_logger("SyslogNgCli")
        self.__syslog_ng_executor = SyslogNgExecutor(logger_factory,
                                                     instance_paths)
        self.__syslog_ng_ctl = SyslogNgCtl(logger_factory, instance_paths)
        self.__valgrind_usage = testcase_parameters.get_valgrind_usage()
        self.__process = None

    # Application commands
    def get_version(self):
        version_output = self.__syslog_ng_executor.run_command(
            command_short_name="version", command=["--version"])["stdout"]
        for version_output_line in version_output.splitlines():
            if "Config version:" in version_output_line:
                return version_output_line.split()[2]
        raise Exception(
            "Can not parse 'Config version' from ./syslog-ng --version")

    def __syntax_only(self, config_path=None):
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        return self.__syslog_ng_executor.run_command(
            command_short_name="syntax_only",
            command=["--syntax-only", "--cfgfile={}".format(config_path)])

    def __syntax_check(self):
        result = self.__syntax_only()
        if result["exit_code"] != 0:
            self.__logger.error(result["stderr"])
            raise Exception("syslog-ng can not started")

    def __is_process_running(self):
        return self.__process.poll() == None

    def __wait_for_control_socket_alive(self):
        def is_alive(s):
            if not s.__is_process_running():
                raise Exception("syslog-ng could not start")
            return s.__syslog_ng_ctl.is_control_socket_alive()

        return wait_until_true(is_alive, self)

    def __wait_for_start(self):
        # wait for start and check start result
        if not self.__wait_for_control_socket_alive():
            self.__error_handling()
            raise Exception("Control socket not alive")
        if not self.__console_log_reader.wait_for_start_message():
            self.__error_handling()
            raise Exception("Start message not arrived")

    def __start_syslog_ng(self):
        if self.__valgrind_usage:
            self.__process = self.__syslog_ng_executor.run_process_with_valgrind(
            )
        else:
            self.__process = self.__syslog_ng_executor.run_process()
        self.__wait_for_start()

    # Process commands
    def start(self, config):
        self.__logger.info("Beginning of syslog-ng start")
        config.write_config_content()

        self.__syntax_check()
        self.__start_syslog_ng()

        self.__logger.info("End of syslog-ng start")

    def reload(self, config):
        self.__logger.info("Beginning of syslog-ng reload")
        config.write_config_content()

        # effective reload
        self.__syslog_ng_ctl.reload()

        # wait for reload and check reload result
        if not self.__wait_for_control_socket_alive():
            self.__error_handling()
            raise Exception("Control socket not alive")
        if not self.__console_log_reader.wait_for_reload_message():
            self.__error_handling()
            raise Exception("Reload message not arrived")
        self.__logger.info("End of syslog-ng reload")

    def stop(self, unexpected_messages=None):
        self.__logger.info("Beginning of syslog-ng stop")
        if self.__process:
            # effective stop
            result = self.__syslog_ng_ctl.stop()

            # wait for stop and check stop result
            if result["exit_code"] != 0:
                self.__error_handling()
            if not wait_until_false(self.__is_process_running):
                self.__error_handling()
                raise Exception("syslog-ng did not stop")
            if not self.__console_log_reader.wait_for_stop_message():
                self.__error_handling()
                raise Exception("Stop message not arrived")
            self.__console_log_reader.check_for_unexpected_messages(
                unexpected_messages)
            if self.__valgrind_usage:
                self.__console_log_reader.handle_valgrind_log(
                    self.__instance_paths.get_valgrind_log_path())
            self.__process = None
            self.__logger.info("End of syslog-ng stop")

    # Helper functions
    def __error_handling(self):
        self.__console_log_reader.dump_stderr()
        self.__handle_core_file()

    def __handle_core_file(self):
        if self.__process.wait(1) != 0:
            core_file_found = False
            for core_file in Path(".").glob("*core*"):
                core_file_found = True
                self.__process = None
                self.__syslog_ng_executor.get_backtrace_from_core(
                    core_file=str(core_file))
                core_file.replace(
                    Path(self.__instance_paths.get_working_dir(), core_file))
            if core_file_found:
                raise Exception("syslog-ng core file was found and processed")
Example #8
0
class SyslogNgCli(object):
    def __init__(self, instance_paths, testcase_parameters):
        self.__instance_paths = instance_paths
        self.__console_log_reader = ConsoleLogReader(instance_paths)
        self.__syslog_ng_executor = SyslogNgExecutor(instance_paths)
        self.__syslog_ng_ctl = SyslogNgCtl(instance_paths)
        self.__external_tool = testcase_parameters.get_external_tool()
        self.__process = None
        self.__stderr = None
        self.__debug = None
        self.__trace = None
        self.__verbose = None
        self.__startup_debug = None
        self.__no_caps = None
        self.__config_path = None
        self.__persist_path = None
        self.__pid_path = None
        self.__control_socket_path = None

    # Application commands
    def get_version(self):
        version_output = self.__syslog_ng_executor.run_command(
            command_short_name="version", command=["--version"])["stdout"]
        for version_output_line in version_output.splitlines():
            if "Config version:" in version_output_line:
                return version_output_line.split()[2]
        raise Exception(
            "Can not parse 'Config version' from ./syslog-ng --version")

    def __syntax_only(self, config_path=None):
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        return self.__syslog_ng_executor.run_command(
            command_short_name="syntax_only",
            command=["--syntax-only", "--cfgfile={}".format(config_path)],
        )

    def __syntax_check(self):
        result = self.__syntax_only()
        if result["exit_code"] != 0:
            logger.error(result["stderr"])
            raise Exception("syslog-ng can not started exit_code={}".format(
                result["exit_code"]))

    def is_process_running(self):
        return self.__process.poll() is None

    def __wait_for_control_socket_alive(self):
        def is_alive(s):
            if not s.is_process_running():
                self.__process = None
                self.__error_handling("syslog-ng is not running")
            return s.__syslog_ng_ctl.is_control_socket_alive()

        return wait_until_true(is_alive, self)

    def __wait_for_start(self):
        # wait for start and check start result
        if not self.__wait_for_control_socket_alive():
            self.__error_handling("Control socket not alive")
        if not self.__console_log_reader.wait_for_start_message():
            self.__error_handling("Start message not arrived")

    def __start_syslog_ng(self):
        if self.__external_tool:
            self.__process = self.__syslog_ng_executor.run_process_with_external_tool(
                self.__external_tool)
        else:
            self.__process = self.__syslog_ng_executor.run_process(
                self.__stderr, self.__debug, self.__trace, self.__verbose,
                self.__startup_debug, self.__no_caps, self.__config_path,
                self.__persist_path, self.__pid_path,
                self.__control_socket_path)
        if self.__stderr and self.__debug and self.__verbose:
            self.__wait_for_start()

    # Process commands
    def start(self, config, stderr, debug, trace, verbose, startup_debug,
              no_caps, config_path, persist_path, pid_path,
              control_socket_path):
        self.set_start_parameters(stderr, debug, trace, verbose, startup_debug,
                                  no_caps, config_path, persist_path, pid_path,
                                  control_socket_path)
        if self.__process:
            raise Exception("syslog-ng has been already started")

        config.write_config(self.__instance_paths.get_config_path())

        self.__syntax_check()
        self.__start_syslog_ng()

        logger.info("syslog-ng process has been started with PID: {}\n".format(
            self.__process.pid))

        return self.__process

    def reload(self, config):
        config.write_config(self.__instance_paths.get_config_path())

        # effective reload
        result = self.__syslog_ng_ctl.reload()

        # wait for reload and check reload result
        if result["exit_code"] != 0:
            self.__error_handling("Control socket fails to reload syslog-ng")
        if not self.__wait_for_control_socket_alive():
            self.__error_handling("Control socket not alive")
        if self.__stderr and self.__debug and self.__verbose:
            if not self.__console_log_reader.wait_for_reload_message():
                self.__error_handling("Reload message not arrived")
        logger.info(
            "syslog-ng process has been reloaded with PID: {}\n".format(
                self.__process.pid))

    def stop(self, unexpected_messages=None):
        if self.__process:
            saved_pid = self.__process.pid
            # effective stop
            result = self.__syslog_ng_ctl.stop()

            # wait for stop and check stop result
            if result["exit_code"] != 0:
                self.__error_handling("Control socket fails to stop syslog-ng")
            if not wait_until_false(self.is_process_running):
                self.__error_handling("syslog-ng did not stop")
            if self.__stderr and self.__debug and self.__verbose:
                if not self.__console_log_reader.wait_for_stop_message():
                    self.__error_handling("Stop message not arrived")
            self.__console_log_reader.check_for_unexpected_messages(
                unexpected_messages)
            if self.__external_tool == "valgrind":
                self.__console_log_reader.handle_valgrind_log(
                    self.__instance_paths.get_external_tool_output_path(
                        self.__external_tool))
            self.__process = None
            logger.info(
                "syslog-ng process has been stopped with PID: {}\n".format(
                    saved_pid))

    # Helper functions
    def __error_handling(self, error_message):
        self.__console_log_reader.dump_stderr()
        self.__handle_core_file()
        raise Exception(error_message)

    def __handle_core_file(self):
        if not self.is_process_running():
            core_file_found = False
            for core_file in Path(".").glob("*core*"):
                core_file_found = True
                self.__process = None
                self.__syslog_ng_executor.get_backtrace_from_core(
                    core_file=str(core_file))
                core_file.replace(Path(tc_parameters.WORKING_DIR, core_file))
            if core_file_found:
                raise Exception("syslog-ng core file was found and processed")

    def set_start_parameters(self, stderr, debug, trace, verbose,
                             startup_debug, no_caps, config_path, persist_path,
                             pid_path, control_socket_path):
        self.__stderr = stderr
        self.__debug = debug
        self.__trace = trace
        self.__verbose = verbose
        self.__startup_debug = startup_debug
        self.__no_caps = no_caps
        self.__config_path = config_path
        self.__persist_path = persist_path
        self.__pid_path = pid_path
        self.__control_socket_path = control_socket_path
Example #9
0
class SyslogNgCli(object):
    def __init__(self, instance_paths, testcase_parameters):
        self.__instance_paths = instance_paths
        self.__console_log_reader = ConsoleLogReader(instance_paths)
        self.__syslog_ng_executor = SyslogNgExecutor(instance_paths)
        self.__syslog_ng_ctl = SyslogNgCtl(instance_paths)
        self.__valgrind_usage = testcase_parameters.get_valgrind_usage()
        self.__process = None

    # Application commands
    def get_version(self):
        version_output = self.__syslog_ng_executor.run_command(command_short_name="version", command=["--version"])[
            "stdout"
        ]
        for version_output_line in version_output.splitlines():
            if "Config version:" in version_output_line:
                return version_output_line.split()[2]
        raise Exception("Can not parse 'Config version' from ./syslog-ng --version")

    def __syntax_only(self, config_path=None):
        if config_path is None:
            config_path = self.__instance_paths.get_config_path()
        return self.__syslog_ng_executor.run_command(
            command_short_name="syntax_only", command=["--syntax-only", "--cfgfile={}".format(config_path)]
        )

    def __syntax_check(self):
        result = self.__syntax_only()
        if result["exit_code"] != 0:
            logger.error(result["stderr"])
            raise Exception("syslog-ng can not started")

    def is_process_running(self):
        return self.__process.poll() is None

    def __wait_for_control_socket_alive(self):
        def is_alive(s):
            if not s.is_process_running():
                self.__process = None
                raise Exception("syslog-ng is not running")
            return s.__syslog_ng_ctl.is_control_socket_alive()
        return wait_until_true(is_alive, self)

    def __wait_for_start(self):
        # wait for start and check start result
        if not self.__wait_for_control_socket_alive():
            self.__error_handling()
            raise Exception("Control socket not alive")
        if not self.__console_log_reader.wait_for_start_message():
            self.__error_handling()
            raise Exception("Start message not arrived")

    def __start_syslog_ng(self):
        if self.__valgrind_usage:
            self.__process = self.__syslog_ng_executor.run_process_with_valgrind()
        else:
            self.__process = self.__syslog_ng_executor.run_process()
        self.__wait_for_start()

    # Process commands
    def start(self, config):
        config.write_content(self.__instance_paths.get_config_path())

        self.__syntax_check()
        self.__start_syslog_ng()

        logger.info("syslog-ng process has been started with PID: {}\n".format(self.__process.pid))

    def reload(self, config):
        config.write_content(self.__instance_paths.get_config_path())

        # effective reload
        self.__syslog_ng_ctl.reload()

        # wait for reload and check reload result
        if not self.__wait_for_control_socket_alive():
            self.__error_handling()
            raise Exception("Control socket not alive")
        if not self.__console_log_reader.wait_for_reload_message():
            self.__error_handling()
            raise Exception("Reload message not arrived")
        logger.info("syslog-ng process has been reloaded with PID: {}\n".format(self.__process.pid))

    def stop(self, unexpected_messages=None):
        if self.__process:
            saved_pid = self.__process.pid
            # effective stop
            result = self.__syslog_ng_ctl.stop()

            # wait for stop and check stop result
            if result["exit_code"] != 0:
                self.__error_handling()
            if not wait_until_false(self.is_process_running):
                self.__error_handling()
                raise Exception("syslog-ng did not stop")
            if not self.__console_log_reader.wait_for_stop_message():
                self.__error_handling()
                raise Exception("Stop message not arrived")
            self.__console_log_reader.check_for_unexpected_messages(unexpected_messages)
            if self.__valgrind_usage:
                self.__console_log_reader.handle_valgrind_log(self.__instance_paths.get_valgrind_log_path())
            self.__process = None
            logger.info("syslog-ng process has been stopped with PID: {}\n".format(saved_pid))

    # Helper functions
    def __error_handling(self):
        self.__console_log_reader.dump_stderr()
        self.__handle_core_file()

    def __handle_core_file(self):
        if not self.is_process_running():
            core_file_found = False
            for core_file in Path(".").glob("*core*"):
                core_file_found = True
                self.__process = None
                self.__syslog_ng_executor.get_backtrace_from_core(core_file=str(core_file))
                core_file.replace(Path(self.__instance_paths.get_working_dir(), core_file))
            if core_file_found:
                raise Exception("syslog-ng core file was found and processed")
Example #10
0
class DriverStatsHandler(object):
    def __init__(self, group_type, driver_name):
        if group_type == "destination":
            statement_short_name = "dst"
            self.component = "{}.{}".format(statement_short_name, driver_name)
        elif group_type == "source":
            statement_short_name = "src"
            self.component = "{}.{}".format(statement_short_name, driver_name)
        elif group_type == "parser":
            self.component = "{}".format(group_type)
        elif group_type == "filter":
            self.component = "{}".format(group_type)
        else:
            raise Exception("Unknown group_type: {}".format(group_type))

        self.syslog_ng_ctl = SyslogNgCtl(tc_parameters.INSTANCE_PATH)

    def build_query_pattern(self):
        return self.build_pattern(delimiter=".")

    def build_stats_pattern(self):
        return self.build_pattern(delimiter=";")

    def build_pattern(self, delimiter):
        return "{}{}".format(
            self.component,
            delimiter,
        )

    def __parse_query_result(self, line: str):
        split = re.split(r"\.|=", line)
        if len(split) < 2:
            return dict()

        key, value = split[-2:]
        return {key: int(value)}

    def __parse_stats_result(self, line):
        split = line.split(";")
        if len(split) < 2:
            return dict()

        key, value = split[-2:]
        return {key: int(value)}

    def parse_result(self, result, result_type):
        parsed_output = {}
        for line in result:
            if result_type == "query":
                parsed_output.update(self.__parse_query_result(line))
            elif result_type == "stats":
                parsed_output.update(self.__parse_stats_result(line))
            else:
                raise Exception("Unknown result_type: {}".format(result_type))
        return parsed_output

    def filter_stats_result(self, stats_result, stats_pattern):
        filtered_list = []
        for stats_line in stats_result:
            if stats_pattern in stats_line:
                filtered_list.append(stats_line)
        return filtered_list

    def get_query(self):
        query_pattern = self.build_query_pattern()
        query_result = self.syslog_ng_ctl.query(
            pattern="*{}*".format(query_pattern))['stdout'].splitlines()

        return self.parse_result(result=query_result, result_type="query")

    def get_stats(self):
        stats_pattern = self.build_stats_pattern()
        stats_result = self.syslog_ng_ctl.stats()['stdout'].splitlines()

        filtered_stats_result = self.filter_stats_result(
            stats_result, stats_pattern)
        return self.parse_result(result=filtered_stats_result,
                                 result_type="stats")
Example #11
0
 def new_syslog_ng_ctl(self, syslog_ng):
     return SyslogNgCtl(syslog_ng.instance_paths)
Example #12
0
 def new_syslog_ng_ctl(self, syslog_ng):
     return SyslogNgCtl(self.__logger_factory, syslog_ng.instance_paths)