Created
February 9, 2017 21:52
-
-
Save aleks-mariusz/3c9ea482504967d70732228ef3bb65e6 to your computer and use it in GitHub Desktop.
This utiilty helps to find metrics in graphite whisper format that haven't been updated in a preset number of days
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import optparse # argparse not available since most systems still have python 2.6 (argparse is in 2.7) | |
import os | |
import sys | |
import time | |
# helper function to return the parent dir given a dir | |
parent_of = lambda p: os.path.sep.join( p.split( os.path.sep )[:-1] ) | |
def main(): | |
parser = optparse.OptionParser(usage='%prog [-d <whisper-dir>] [-a age-in-days]') | |
parser.add_option('-d', action='store', dest='dir', default='/opt/graphite/storage/whisper', \ | |
help='which directory to search from') | |
parser.add_option('-a', action='store', dest='age', default=30, type='int', \ | |
help='min staleness age (in days) to report') | |
parser.add_option('-m', action='store', dest='metric', \ | |
help='optional pattern to limit search of stale metrics to') | |
opts,args = parser.parse_args() | |
if not os.path.exists(opts.dir): | |
print parser.error('Directory {0} does not exist!'.format(opts.dir)) | |
return 1 | |
elif not os.access(opts.dir, os.R_OK): | |
print parser.error('Directory {0} not readable!'.format(opts.dir)) | |
return 1 | |
opts.dir += os.path.sep if not opts.dir.endswith( os.path.sep ) else '' | |
t = time.time() # current seconds since epoch | |
a = opts.age * 86400 # age in seconds | |
walker = os.walk(opts.dir, topdown=False) # get generator object, work depth-first | |
paths_walked = list(walker) # consume returned generator | |
is_old = dict((p, True) for p,_,_ in paths_walked) # assume they are all old unless we see otherwise | |
for this_dir,any_dirs,any_files in paths_walked: | |
if any_files: | |
is_old[this_dir] = False if any( t - os.stat( os.path.join(this_dir, this_file) ).st_mtime < a for this_file in any_files ) else True | |
elif any_dirs: | |
is_old[this_dir] = False if any( not is_old[ os.path.join(this_dir, d) ] for d in any_dirs ) else True | |
print '\n'.join(sorted([ p.replace(opts.dir,'') for p,_,_ in paths_walked if is_old[ p ] and parent_of(p) in is_old and not is_old[ parent_of(p) ] ])) | |
return 0 # successful return code | |
if __name__ == '__main__': | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment