def do_audit_log(self, audit_name): # TODO: control audits with error at start
        """
        Implementation of: /audit/log

        :param audit_name: Name of the audit to query.
        :type audit_name: str

        :returns: List of tuples.
            Each tuple contains the following elements:
             - Plugin ID.
             - Data object ID (plugin instance).
             - Log line text. May contain newline characters.
             - Log level.
             - True if the message is an error, False otherwise.
             - Timestamp.
        :rtype: list( tuple(str, str, str, int, bool, float) )
        """
        if audit_name in self.audit_error:
            return "error"

        try:
            if self.is_audit_running(audit_name):
                return get_audit_log_lines(audit_name)
            else:
                # XXX TODO open the database manually here
                raise NotImplementedError(
                    "Querying finished audits is not implemented yet!")
        except Exception:
            Logger.log_error(traceback.format_exc())
Beispiel #2
0
    def generate_report(self, output_file):
        Logger.log_verbose("Writing audit logs to file: %s" % output_file)

        # Open the output file.
        with open(output_file, "w") as f:

            # Paginate the log lines to limit the memory usage.
            page_num = 0
            while True:
                lines = get_audit_log_lines(page_num=page_num, per_page=20)
                if not lines:
                    break
                page_num += 1

                # For each log line...
                for line in lines:

                    # Split the line into its components.
                    n = LogLine(*line)
                    d = n._asdict()
                    k = list(n._fields)

                    # Fix the log levels.
                    d["level"] = {
                        0: "INFO",
                        1: "LOW",
                        2: "MED",
                        3: "HIGH",
                    }.get(d["level"], "HIGH")
                    if d["is_err"]:
                        d["level"] = "ERR_" + d["level"]
                    del d["is_err"]
                    k.remove("is_err")

                    # Fix the timestamp.
                    d["time"] = asctime(gmtime(d["time"]))

                    # We can't have tab characters in the text.
                    # Replace them with spaces.
                    d["text"] = d["text"].replace("\t", " " * 8)

                    # The text may contain newlines, so we'll have to
                    # split it into multiple log lines if that happens.
                    if "\n" not in d["text"]:
                        sub_lines = [d]
                    else:
                        sub_lines = []
                        for x in d["text"].split("\n"):
                            x = x.rstrip()
                            d["text"] = x
                            sub_lines.append(d.copy())

                    # Write the log in LTSV format.
                    for d in sub_lines:
                        l = "\t".join("%s:%s" % (x, d[x]) for x in k) + "\n"
                        f.write(l.encode("utf-8"))

        # Launch the build command, if any.
        self.launch_command(output_file)
Beispiel #3
0
    def generate_report(self, output_file):
        Logger.log_verbose("Writing audit logs to file: %s" % output_file)

        # Open the output file.
        with open(output_file, "w") as f:

            # Paginate the log lines to limit the memory usage.
            page_num = 0
            while True:
                lines = get_audit_log_lines(page_num = page_num,
                                            per_page = 20)
                if not lines:
                    break
                page_num += 1

                # For each log line...
                for line in lines:

                    # Split the line into its components.
                    n = LogLine(*line)
                    d = n._asdict()
                    k = list(n._fields)

                    # Fix the log levels.
                    d["level"] = {
                        0: "INFO",
                        1: "LOW",
                        2: "MED",
                        3: "HIGH",
                    }.get(d["level"], "HIGH")
                    if d["is_err"]:
                        d["level"] = "ERR_" + d["level"]
                    del d["is_err"]
                    k.remove("is_err")

                    # Fix the timestamp.
                    d["time"] = asctime(gmtime(d["time"]))

                    # We can't have tab characters in the text.
                    # Replace them with spaces.
                    d["text"] = d["text"].replace("\t", " " * 8)

                    # The text may contain newlines, so we'll have to
                    # split it into multiple log lines if that happens.
                    if "\n" not in d["text"]:
                        sub_lines = [d]
                    else:
                        sub_lines = []
                        for x in d["text"].split("\n"):
                            x = x.rstrip()
                            d["text"] = x
                            sub_lines.append(d.copy())

                    # Write the log in LTSV format.
                    for d in sub_lines:
                        l = "\t".join(
                            "%s:%s" % (x, d[x])
                            for x in k
                        ) + "\n"
                        f.write(l.encode("utf-8"))

        # Launch the build command, if any.
        self.launch_command(output_file)
Beispiel #4
0
    def generate_report(self, output_file):
        Logger.log_verbose("Writing audit logs to file: %s" % output_file)

        # plugin_id -> plugin_name
        self.plugin_names = dict()

        # plugin_id -> ack_identity -> simple_id
        self.current_plugins = defaultdict(dict)

        # Open the output file.
        with open(output_file, "w") as f:

            # Paginate the log lines to limit the memory usage.
            page_num = 0
            while True:
                lines = get_audit_log_lines(page_num=page_num, per_page=100)
                if not lines:
                    break
                page_num += 1

                # For each log line...
                for line in lines:

                    # Split the line into its components.
                    d = LogLine(*line)._asdict()

                    # Fix the log levels.
                    d["level"] = {
                        0: "INFO",
                        1: "LOW",
                        2: "MED",
                        3: "HIGH",
                    }.get(d["level"], "HIGH")
                    if d["is_err"]:
                        d["level"] = "ERR_" + d["level"]

                    # Fix the timestamp.
                    d["time"] = asctime(gmtime(d["time"]))

                    # Fix the plugin name.
                    d["plugin"] = self.get_plugin_name(d["plugin_id"],
                                                       d["identity"])

                    # We can't have tab characters in the text.
                    # Replace them with spaces.
                    d["text"] = d["text"].replace("\t", " " * 8)

                    # The text may contain newlines, so we'll have to
                    # split it into multiple log lines if that happens.
                    if "\n" not in d["text"]:
                        sub_lines = [d]
                    else:
                        sub_lines = []
                        for x in d["text"].split("\n"):
                            x = x.rstrip()
                            d["text"] = x
                            sub_lines.append(d.copy())

                    # Write the log lines.
                    for d in sub_lines:
                        l = "%(time)s : %(level)s : %(plugin)s : %(text)s\n"
                        l %= d
                        f.write(l.encode("utf-8"))

        # Launch the build command, if any.
        self.launch_command(output_file)
Beispiel #5
0
    def generate_report(self, output_file):
        Logger.log_verbose("Writing audit logs to file: %s" % output_file)

        # plugin_id -> plugin_name
        self.plugin_names = dict()

        # plugin_id -> ack_identity -> simple_id
        self.current_plugins = defaultdict(dict)

        # Open the output file.
        with open(output_file, "w") as f:

            # Paginate the log lines to limit the memory usage.
            page_num = 0
            while True:
                lines = get_audit_log_lines(page_num = page_num,
                                            per_page = 100)
                if not lines:
                    break
                page_num += 1

                # For each log line...
                for line in lines:

                    # Split the line into its components.
                    d = LogLine(*line)._asdict()

                    # Fix the log levels.
                    d["level"] = {
                        0: "INFO",
                        1: "LOW",
                        2: "MED",
                        3: "HIGH",
                    }.get(d["level"], "HIGH")
                    if d["is_err"]:
                        d["level"] = "ERR_" + d["level"]

                    # Fix the timestamp.
                    d["time"] = asctime(gmtime(d["time"]))

                    # Fix the plugin name.
                    d["plugin"] = self.get_plugin_name(
                        d["plugin_id"], d["identity"])

                    # We can't have tab characters in the text.
                    # Replace them with spaces.
                    d["text"] = d["text"].replace("\t", " " * 8)

                    # The text may contain newlines, so we'll have to
                    # split it into multiple log lines if that happens.
                    if "\n" not in d["text"]:
                        sub_lines = [d]
                    else:
                        sub_lines = []
                        for x in d["text"].split("\n"):
                            x = x.rstrip()
                            d["text"] = x
                            sub_lines.append(d.copy())

                    # Write the log lines.
                    for d in sub_lines:
                        l = "%(time)s : %(level)s : %(plugin)s : %(text)s\n"
                        l %= d
                        f.write(l.encode("utf-8"))

        # Launch the build command, if any.
        self.launch_command(output_file)