PHP 7.4.33
Preview: linux_logs.py Size: 3.34 KB
/home/godevadmin/public_html/upload_images/home/000~ROOT~000/lib/fm-agent/plugins/linux_logs.py

# -*- coding: utf-8 -*-
import agent_util
import logging
from glob import glob
from library.log_matcher import LogMatcher


class LinuxLogsPlugin(agent_util.Plugin):
    textkey = "linux_log"
    label = "Event log"

    @classmethod
    def get_metadata(self, config):
        data = {
            "count": {
                "label": "Event entry count",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "count",
                "option_string": True,
            }
        }
        return data

    def check(self, textkey, data, config={}):
        # Passed data from the check schedule
        log_source = data.get("log_source")
        timescale = data.get("timescale")
        expression = data.get("filter")
        log_source = log_source.strip(" ")

        if "*" in log_source or "?" in log_source:
            # Handle multiple files
            files = glob(log_source)
        else:
            files = [log_source]

        file_inodes = {}
        total_metrics = 0

        expression = expression or "*"
        expression = expression.replace("*", ".*")
        expression = expression.replace('""', ".*")
        for target in files:
            # Extract the file current inode
            try:
                file_inodes[target] = LogMatcher.get_file_inode(target)
            except OSError:
                import sys

                _, error, _ = sys.exc_info()
                logging.error("Error opening %s file." % (target))
                logging.error(error)
                continue

            # Extract data from the agent cache about the check
            log_data = self.get_cache_results(
                textkey, "%s/%s" % (self.schedule.id, target)
            )
            if log_data:
                log_data = log_data[0][-1]
            else:
                log_data = dict()

            last_line_number = log_data.get("last_known_line")
            stored_inode = log_data.get("inode")
            results = log_data.get("results", [])

            # Extract the lines of the file.
            try:
                total_lines, current_lines = LogMatcher.get_file_lines(
                    last_line_number, target, file_inodes[target], stored_inode
                )
            except IOError:
                import sys

                _, e, _ = sys.exc_info()
                logging.error("Could not open file: %s" % str(e))
                return None

            logging.info(
                "Stored line %s Current line %s Looking at %s lines"
                % (str(last_line_number), str(total_lines), str(len(current_lines)))
            )
            # Perform the matching of the expression in the lines
            log_matcher = LogMatcher(stored_inode)
            results = log_matcher.match(current_lines, expression)
            metric, results = log_matcher.calculate_metric(results, timescale)
            total_metrics += metric and metric or 0

            cache_data = dict(
                inode=file_inodes[target], last_known_line=total_lines, results=results
            )
            self.cache_result(textkey, "%s/%s" % (self.schedule.id, target), cache_data)
            logging.info(
                'Found %s instances of "%s" in %s'
                % (str(metric or 0), expression, target)
            )
        return total_metrics

Directory Contents

Dirs: 1 × Files: 55

Name Size Perms Modified Actions
- drwxr-xr-x 2025-06-19 05:08:01
Edit Download
20.07 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
13.66 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
6.34 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
22.59 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
9.46 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.89 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
9.56 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
32.09 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
7.33 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
16.80 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
38.95 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.90 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
930 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.00 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
5.07 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.68 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
13.46 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
13.21 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
13.58 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
8.39 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.34 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.54 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.64 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
6.04 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
26.56 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
16.61 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
20.40 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
5.58 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
12.33 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
6.44 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.98 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.30 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
15.51 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
8.88 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
5.61 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.60 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.99 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
21.37 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
17.23 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
19.85 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
11.31 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
2.42 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.42 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
6.59 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.28 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
7.09 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
19.11 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.62 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
3.61 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
1.09 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.73 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
4.91 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
13.84 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
19.20 KB lrw-r--r-- 2025-06-18 20:44:50
Edit Download
0 B lrw-r--r-- 2025-06-18 20:44:50
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).