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