Skip to content

Instantly share code, notes, and snippets.

@jradmacher
Last active May 19, 2020 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jradmacher/39401b3b3ed594cd237c0eb1dfae9a5e to your computer and use it in GitHub Desktop.
Save jradmacher/39401b3b3ed594cd237c0eb1dfae9a5e to your computer and use it in GitHub Desktop.
find outdated Graphite whisper files and (optionally) delete them
#!/usr/bin/env python3
import os
import sys
import signal
import argparse
import json
import time
from logging import debug,info,warning,error
import logging
try:
import whisper
except ImportError:
raise SystemExit('[ERROR] Please make sure whisper is installed properly')
# Ignore SIGPIPE
try:
signal.signal(signal.SIGPIPE, signal.SIG_DFL)
except AttributeError:
#OS=windows
pass
# Argument parser
parser = argparse.ArgumentParser()
parser.add_argument('--retention_offset', '-r', default=10, action='store', type=int,
help="Offset to add to retention time")
parser.add_argument('--delete', default=False, action='store_true',
help="Delete Files, instead of only outputting them")
parser.add_argument(
'--debug', '-d',
help="Print information on why a file is/is not considered for deletion",
action="store_const", dest="loglevel", const=logging.DEBUG,
default=logging.WARNING,
)
parser.add_argument(
'--verbose', '-v',
help="Be verbose and output, when a file is deleted.",
action="store_const", dest="loglevel", const=logging.INFO,
)
parser.add_argument('path',
help="Directory tree to search for outdated files")
args = parser.parse_args()
# transfer args to global variables
retention_offset = 86400*args.retention_offset
path = args.path
logging.basicConfig(level=args.loglevel)
do_delete = args.delete
#now in unixtime
now = int(time.time())
# main program
#
# go through all subdirs of "path", and check if the file age is old than than the retention tim (+offset)
for root, dirs, files in os.walk(path, topdown=False):
for name in files:
filename = os.path.join(root, name)
st_ctime = os.stat(filename).st_ctime
age = now - int(st_ctime)
if filename.endswith(".wsp") and age > retention_offset:
try:
whisper_info = whisper.info(filename)
except whisper.WhisperException as exc:
raise SystemExit('[ERROR] %s' % str(exc))
retention = whisper_info['maxRetention'] + retention_offset
age_day = age / (3600*24.0)
retention_day = retention / (3600*24.0)
if age > retention:
debug("%s is outdated (%.2f > %.2f)" % (filename,age_day, retention_day))
if do_delete:
info("Removing '%s'" % filename)
# Try to delete the file #
try:
os.remove(filename)
except OSError as e: # if failed, report it back to the user #
error("%s - %s." % (e.filename, e.strerror))
else:
print(filename)
else:
debug("%s is NOT outdated (%.2f <= %.2f)" % (filename, age_day, retention_day))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment