From f7183d720eced85dc3360be968dbed67886d4ef9 Mon Sep 17 00:00:00 2001 From: vuksan Date: Tue, 24 Jun 2014 10:14:43 -0700 Subject: [PATCH] Add file checks module --- file_checks/README.mkdn | 50 ++++++++++++++++ file_checks/file_checks.py | 104 +++++++++++++++++++++++++++++++++ file_checks/file_checks.pyconf | 17 ++++++ 3 files changed, 171 insertions(+) create mode 100644 file_checks/README.mkdn create mode 100644 file_checks/file_checks.py create mode 100644 file_checks/file_checks.pyconf diff --git a/file_checks/README.mkdn b/file_checks/README.mkdn new file mode 100644 index 00000000..fe5ecc1c --- /dev/null +++ b/file_checks/README.mkdn @@ -0,0 +1,50 @@ +Purpose +=============== + +This module allows you to + + * Keep track of a presence of a particular file (0 for file not present and 1 for file present) + * Size of a particular file + + +Install +=============== + +Copy file_checks.py from python_modules to your python modules directory e.g. + +/usr/lib64/ganglia/python_modules + +and file_checks.pyconf to + +/etc/ganglia/conf.d/ + +Restart Gmond and you are done. + + +Configure +=============== + +Please see file_checks.py. Under metric_init you will need to add any metrics you want added e.g. + + descriptors.append(create_desc(Desc_Skel, { + "name" : NAME_PREFIX + "etc_chef_disabled_present", + "call_back" : get_is_file_present, + "description" : "/etc/chef/disabled present" + })) + +Replace slashes (/) with underscores and drop initial slash and append _present. For file sizes add +following segment + + descriptors.append(create_desc(Desc_Skel, { + "name" : NAME_PREFIX + "var_log_syslog_size", + "call_back" : get_file_size, + "units" : "bytes", + "value_type" : "float", + "description" : "Size of /var/log/syslog" + })) + +Append _size to the metric name + +## AUTHOR + +Author: Vladimir Vuksan https://github.com/vvuksan diff --git a/file_checks/file_checks.py b/file_checks/file_checks.py new file mode 100644 index 00000000..f69c0173 --- /dev/null +++ b/file_checks/file_checks.py @@ -0,0 +1,104 @@ +import sys +import traceback +import os +import re +import time +import copy +import urllib2 + +METRICS = { + 'time' : 0, + 'data' : {} +} + +LAST_METRICS = copy.deepcopy(METRICS) +METRICS_CACHE_MAX = 5 + +NAME_PREFIX="filechecks_" + +############################################################################### +# Misc file checks +############################################################################### +def get_is_file_present(name): + """Find whether file exists""" + + global NAME_PREFIX + + name = name.replace(NAME_PREFIX,"") # remove prefix from name + + filename = "/" + name.replace("_present","").replace("_","/") + + if os.path.isfile(filename): + return 1 + else: + return 0 + + +def get_file_size(name): + + global NAME_PREFIX + + name = name.replace(NAME_PREFIX,"") # remove prefix from name + + filename = "/" + name.replace("_size","").replace("_","/") + + try: + return os.stat(filename).st_size + except OSError: + return 0 + +def create_desc(skel, prop): + d = skel.copy() + for k,v in prop.iteritems(): + d[k] = v + return d + +def metric_init(params): + global descriptors, metric_map, Desc_Skel, NAME_PREFIX + + descriptors = [] + + Desc_Skel = { + 'name' : 'XXX', + 'orig_name' : 'XXX', + 'call_back' : get_is_file_present, + 'time_max' : 60, + 'value_type' : 'uint', + 'format' : '%d', + 'slope' : 'both', # zero|positive|negative|both + 'description' : '', + 'units' : 'boolean', + 'groups' : 'file_checks', + } + + + descriptors.append(create_desc(Desc_Skel, { + "name" : NAME_PREFIX + "etc_chef_disabled_present", + "call_back" : get_is_file_present, + "description" : "/etc/chef/disabled present" + })) + + descriptors.append(create_desc(Desc_Skel, { + "name" : NAME_PREFIX + "var_log_syslog_size", + "call_back" : get_file_size, + "units" : "bytes", + "value_type" : "float", + "description" : "Size of /var/log/syslog" + })) + + + return descriptors + +def metric_cleanup(): + '''Clean up the metric module.''' + pass + +#This code is for debugging and unit testing +if __name__ == '__main__': + metric_init({}) + while True: + for d in descriptors: + v = d['call_back'](d['name']) + print '%s = %s' % (d['name'], v) + print 'Sleeping 5 seconds' + time.sleep(5) diff --git a/file_checks/file_checks.pyconf b/file_checks/file_checks.pyconf new file mode 100644 index 00000000..2086b1c3 --- /dev/null +++ b/file_checks/file_checks.pyconf @@ -0,0 +1,17 @@ +modules { + module { + name = "filechecks" + language = "python" + } +} + +collection_group { + collect_every = 10 + time_threshold = 45 + + metric { + name_match = "filechecks_(.+)" + value_threshold = 1.0 + } + +}