Skip to content

Commit

Permalink
Add file checks module
Browse files Browse the repository at this point in the history
  • Loading branch information
vvuksan committed Jun 24, 2014
1 parent 6122a45 commit f7183d7
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 0 deletions.
50 changes: 50 additions & 0 deletions file_checks/README.mkdn
Original file line number Diff line number Diff line change
@@ -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
104 changes: 104 additions & 0 deletions file_checks/file_checks.py
Original file line number Diff line number Diff line change
@@ -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)
17 changes: 17 additions & 0 deletions file_checks/file_checks.pyconf
Original file line number Diff line number Diff line change
@@ -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
}

}

0 comments on commit f7183d7

Please sign in to comment.