コード例 #1
0
class SNMPtrapd(object):
    def __init__(self, port):
        self.snmptrapd_proc = None
        self.port = port

        self.snmptrapd_log = Path(tc_parameters.WORKING_DIR, "snmptrapd_log")
        self.snmptrapd_stdout_path = Path(tc_parameters.WORKING_DIR,
                                          "snmptrapd_stdout")
        self.snmptrapd_stderr_path = Path(tc_parameters.WORKING_DIR,
                                          "snmptrapd_stderr")

    def wait_for_snmptrapd_log_creation(self):
        return self.snmptrapd_log.exists()

    def wait_for_snmptrapd_startup(self):
        return "NET-SNMP version" in self.snmptrapd_log.read_text()

    def start(self):
        if self.snmptrapd_proc is not None:
            return

        self.snmptrapd_proc = ProcessExecutor().start(
            [
                "snmptrapd",
                "-f",
                "--disableAuthorization=yes",
                "-C",
                "-On",
                "--doNotLogTraps=no",
                self.port,
                "-LF",
                "6-6",
                os.path.relpath(str(self.snmptrapd_log)),
            ],
            self.snmptrapd_stdout_path,
            self.snmptrapd_stderr_path,
        )
        wait_until_true(self.wait_for_snmptrapd_log_creation)
        wait_until_true(self.wait_for_snmptrapd_startup)
        return self.snmptrapd_proc.is_running()

    def stop(self):
        if self.snmptrapd_proc is None:
            return

        self.snmptrapd_proc.terminate()
        try:
            self.snmptrapd_proc.wait(4)
        except TimeoutExpired:
            self.snmptrapd_proc.kill()

        self.snmptrapd_proc = None

    def get_port(self):
        return self.port

    def get_logs(self):
        file_reader = DestinationReader(FileIO)
        return file_reader.read_all_logs(self.snmptrapd_log)
コード例 #2
0
ファイル: loggen.py プロジェクト: anjinwoong/syslog-ng
class Loggen(object):

    instanceIndex = -1

    @staticmethod
    def __get_new_instance_index():
        Loggen.instanceIndex += 1
        return Loggen.instanceIndex

    def __init__(self):
        self.loggen_proc = None
        self.loggen_bin_path = tc_parameters.INSTANCE_PATH.get_loggen_bin()

    def __decode_start_parameters(
        self,
        inet,
        unix,
        stream,
        dgram,
        use_ssl,
        dont_parse,
        read_file,
        skip_tokens,
        loop_reading,
        rate,
        interval,
        permanent,
        syslog_proto,
        proxied,
        sdata,
        no_framing,
        active_connections,
        idle_connections,
        ipv6,
        debug,
        number,
        csv,
        quiet,
        size,
        reconnect,
        proxied_tls_passthrough,
        proxy_src_ip,
        proxy_dst_ip,
        proxy_src_port,
        proxy_dst_port,
    ):

        start_parameters = []

        if inet is True:
            start_parameters.append("--inet")

        if unix is True:
            start_parameters.append("--unix")

        if stream is True:
            start_parameters.append("--stream")

        if dgram is True:
            start_parameters.append("--dgram")

        if use_ssl is True:
            start_parameters.append("--use-ssl")

        if dont_parse is True:
            start_parameters.append("--dont-parse")

        if read_file is not None:
            start_parameters.append("--read-file={}".format(read_file))

        if skip_tokens is not None:
            start_parameters.append("--skip-tokens={}".format(skip_tokens))

        if loop_reading is True:
            start_parameters.append("--loop-reading")

        if rate is not None:
            start_parameters.append("--rate={}".format(rate))

        if interval is not None:
            start_parameters.append("--interval={}".format(interval))

        if permanent is True:
            start_parameters.append("--permanent")

        if syslog_proto is True:
            start_parameters.append("--syslog-proto")

        if proxied is True:
            start_parameters.append("--proxied")

        if proxy_src_ip is not None:
            start_parameters.append("--proxy-src-ip={}".format(proxy_src_ip))

        if proxy_dst_ip is not None:
            start_parameters.append("--proxy-dst-ip={}".format(proxy_dst_ip))

        if proxy_src_port is not None:
            start_parameters.append(
                "--proxy-src-port={}".format(proxy_src_port))

        if proxy_dst_port is not None:
            start_parameters.append(
                "--proxy-dst-port={}".format(proxy_dst_port))

        if proxied_tls_passthrough is True:
            start_parameters.append("--proxied-tls-passthrough")

        if sdata is True:
            start_parameters.append("--sdata")

        if no_framing is True:
            start_parameters.append("--no-framing")

        if active_connections is not None:
            start_parameters.append(
                "--active-connections={}".format(active_connections))

        if idle_connections is not None:
            start_parameters.append(
                "--idle-connections={}".format(idle_connections))

        if ipv6 is True:
            start_parameters.append("--ipv6")

        if debug is True:
            start_parameters.append("--debug")

        if number is not None:
            start_parameters.append("--number={}".format(number))

        if csv is True:
            start_parameters.append("--csv")

        if quiet is True:
            start_parameters.append("--quiet")

        if size is not None:
            start_parameters.append("--size={}".format(size))

        if reconnect is True:
            start_parameters.append("--reconnect")

        return start_parameters

    def start(
        self,
        target,
        port,
        inet=None,
        unix=None,
        stream=None,
        dgram=None,
        use_ssl=None,
        dont_parse=None,
        read_file=None,
        skip_tokens=None,
        loop_reading=None,
        rate=None,
        interval=None,
        permanent=None,
        syslog_proto=None,
        proxied=None,
        sdata=None,
        no_framing=None,
        active_connections=None,
        idle_connections=None,
        ipv6=None,
        debug=None,
        number=None,
        csv=None,
        quiet=None,
        size=None,
        reconnect=None,
        proxied_tls_passthrough=None,
        proxy_src_ip=None,
        proxy_dst_ip=None,
        proxy_src_port=None,
        proxy_dst_port=None,
    ):

        if self.loggen_proc is not None and self.loggen_proc.is_running():
            raise Exception(
                "Loggen is already running, you shouldn't call start")

        instanceIndex = Loggen.__get_new_instance_index()
        self.loggen_stdout_path = Path(
            tc_parameters.WORKING_DIR,
            "loggen_stdout_{}".format(instanceIndex))
        self.loggen_stderr_path = Path(
            tc_parameters.WORKING_DIR,
            "loggen_stderr_{}".format(instanceIndex))

        self.parameters = self.__decode_start_parameters(
            inet,
            unix,
            stream,
            dgram,
            use_ssl,
            dont_parse,
            read_file,
            skip_tokens,
            loop_reading,
            rate,
            interval,
            permanent,
            syslog_proto,
            proxied,
            sdata,
            no_framing,
            active_connections,
            idle_connections,
            ipv6,
            debug,
            number,
            csv,
            quiet,
            size,
            reconnect,
            proxied_tls_passthrough,
            proxy_src_ip,
            proxy_dst_ip,
            proxy_src_port,
            proxy_dst_port,
        )

        self.loggen_proc = ProcessExecutor().start(
            [self.loggen_bin_path] + self.parameters + [target, port],
            self.loggen_stdout_path,
            self.loggen_stderr_path,
        )

        return self.loggen_proc

    def stop(self):
        if self.loggen_proc is None:
            return

        self.loggen_proc.terminate()
        try:
            self.loggen_proc.wait(4)
        except TimeoutExpired:
            self.loggen_proc.kill()

        self.loggen_proc = None

    def get_sent_message_count(self):
        if not self.loggen_stderr_path.exists():
            return 0

        # loggen puts the count= messages to the stderr
        f = open(str(self.loggen_stderr_path), "r")
        content = f.read()
        f.close()

        start_pattern = "count="
        if start_pattern not in content:
            return 0
        index_start = content.rindex(start_pattern) + len(start_pattern)

        end_pattern = ", "
        if end_pattern not in content:
            return 0
        index_end = content.find(end_pattern, index_start)

        return int(content[index_start:index_end])
コード例 #3
0
class SNMPtrapd(object):
    TRAP_LOG_PREFIX = 'LIGHT_TEST_SNMP_TRAP_RECEIVED:'

    def __init__(self, port):
        self.snmptrapd_proc = None
        self.port = port

        self.snmptrapd_log = Path(tc_parameters.WORKING_DIR, "snmptrapd_log")
        self.snmptrapd_stdout_path = Path(tc_parameters.WORKING_DIR,
                                          "snmptrapd_stdout")
        self.snmptrapd_stderr_path = Path(tc_parameters.WORKING_DIR,
                                          "snmptrapd_stderr")

    def wait_for_snmptrapd_log_creation(self):
        return self.snmptrapd_log.exists()

    def wait_for_snmptrapd_startup(self):
        return "NET-SNMP version" in self.snmptrapd_log.read_text()

    def start(self):
        if self.snmptrapd_proc is not None:
            return

        self.snmptrapd_proc = ProcessExecutor().start(
            [
                "snmptrapd",
                "-f",
                "--disableAuthorization=yes",
                "-C",
                "-m ALL",
                "-A",
                "-Ddump",
                "-On",
                "--doNotLogTraps=no",
                "--authCommunity=log public",
                self.port,
                "-d",
                "-Lf",
                os.path.relpath(str(self.snmptrapd_log)),
                "-F",
                "{}%v\n".format(self.TRAP_LOG_PREFIX),
            ],
            self.snmptrapd_stdout_path,
            self.snmptrapd_stderr_path,
        )
        wait_until_true(self.wait_for_snmptrapd_log_creation)
        wait_until_true(self.wait_for_snmptrapd_startup)
        return self.snmptrapd_proc.is_running()

    def stop(self):
        if self.snmptrapd_proc is None:
            return

        self.snmptrapd_proc.terminate()
        try:
            self.snmptrapd_proc.wait(4)
        except TimeoutExpired:
            self.snmptrapd_proc.kill()

        self.snmptrapd_proc = None

    def get_port(self):
        return self.port

    def get_traps(self):
        file_reader = DestinationReader(FileIO)
        logs = file_reader.read_all_logs(self.snmptrapd_log)
        trap_list = []
        for log_line in logs:
            res = re.match('({})(.*)'.format(self.TRAP_LOG_PREFIX), log_line)
            if (res):
                trap_list += res.group(2).rstrip().split("\t")
        return sorted(trap_list)

    def get_raw_traps(self):
        file_reader = DestinationReader(FileIO)
        return file_reader.read_all_logs(self.snmptrapd_log)