PHP 7.4.33
Preview: file_presence.py Size: 5.07 KB
//lib/fm-agent/plugins/file_presence.py

import logging
import agent_util
import glob
import os
import datetime
import re
import time
import sys
from agent_util import float


class FilePresencePlugin(agent_util.Plugin):
    textkey = "files"
    label = "Filesystem"
    # this is here because it's used in 2 different places
    newer_than_regex = re.compile(
        r"(?P<op>(a|c|m)time)<(?P<delta>\d+)(?P<metric>d|h|m|s)"
    )

    @classmethod
    def get_metadata(self, config):
        data = {
            "file.exists": {
                "label": "File exists",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "boolean",
                "option_string": True,
            },
            "file.count": {
                "label": "File count",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "files",
                "option_string": True,
            },
            "file.created": {
                "label": "Creation age",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "minutes",
                "option_string": True,
            },
            "file.modified": {
                "label": "Modification age",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "minutes",
                "option_string": True,
            },
            "directory.size": {
                "label": "Directory size (KB)",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "kilobytes",
                "option_string": True,
            },
            "file.size": {
                "label": "File size (KB)",
                "options": None,
                "status": agent_util.SUPPORTED,
                "error_message": None,
                "unit": "kilobytes",
                "option_string": True,
            },
        }
        return data

    def check(self, textkey, path, config):
        path = path.strip()

        for date in re.findall(r"\$.[^$]+", path):
            date_format = (
                date.replace("YYYY", "%Y")
                .replace("YY", "%y")
                .replace("MM", "%m")
                .replace("DD", "%d")
                .replace("D", "%w")
                .replace("ww", "%W")
                .replace("hh", "%H")
                .replace("mm", "%M")
                .replace("ss", "%S")
                .replace("Z", "%z")
                .replace("$", "")
                .strip()
            )
            date_string = datetime.datetime.now().strftime(date_format)
            path = path.replace(date, date_string)

        self.log.debug("File path for file_presence plugin: %s" % path)

        if textkey == "file.exists":
            glob_results = glob.glob(path)
            has_results = len(glob_results) > 0
            return_val = has_results
            self.log.debug("Return value for file.exists textkey: %s" % str(return_val))
            return return_val
        elif textkey == "file.count":
            if os.path.isdir(path):
                glob_results = glob.glob(path + "/*")
                return_val = len(glob_results)
                self.log.debug(
                    "Return value for file.count textkey: %s" % str(return_val)
                )
            else:
                glob_results = glob.glob(path)
                return_val = len(glob_results)
                self.log.debug(
                    "Return value for file.count textkey: %s" % str(return_val)
                )
            return return_val
        elif textkey == "file.created":
            if not os.path.exists(path):
                return 0.0
            age = time.time() - os.path.getctime(path)
            return_val = float(age) / 60.0
            self.log.debug(
                "Return value for file.created textkey: %s" % str(return_val)
            )
            return return_val
        elif textkey == "file.modified":
            if not os.path.exists(path):
                return 0.0
            age = time.time() - os.path.getmtime(path)
            return_val = float(age) / 60.0
            self.log.debug(
                "Return value for file.modified textkey: %s" % str(return_val)
            )
            return return_val
        elif textkey == "file.size":
            if not os.path.exists(path):
                return 0.0
            return_val = float(os.path.getsize(path)) / 1024.0
            self.log.debug("Return value for file.size textkey: %s" % str(return_val))
            return return_val
        elif textkey == "directory.size":
            if not os.path.exists(path):
                return 0.0
            ret, output = agent_util.execute_command("du -sk " + path)
            return_val = float(output.split()[0])
            self.log.debug("Return value for file.size textkey: %s" % str(return_val))
            return return_val

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).