Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
#!/usr/bin/env python2
import argparse
import os
import shutil
from datetime import datetime, timedelta
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--delete", help="delete entries", action="store_true")
args = parser.parse_args()
def get_last_mtime_recursively(root):
files = []
for dirname, dirnames, filenames in os.walk(root):
for filename in filenames:
path = os.path.join(dirname, filename)
mtime = datetime.fromtimestamp(os.stat(path).st_mtime)
return max(files)
# iterate over all nodes
nodes = {}
for entry in os.listdir(RRD_PATH):
path = os.path.join(RRD_PATH, entry)
nodes[entry] = get_last_mtime_recursively(path)
# filter all nodes which have not reported since 30 days
delta = - timedelta(days=30)
filtered = dict(((k,v) for (k,v) in nodes.items() if v < delta))
# Output
print("Outdated entries: %d of %d - first entry is last modification date" % \
(len(filtered), len(nodes)))
for name,mtime in sorted(filtered.items(), key=lambda x: x[1]):
line = "%s - %s" % (mtime.strftime('%d.%m.%Y'), name)
if args.delete:
path = os.path.join(RRD_PATH, name)
shutil.rmtree(os.path.join(RRD_PATH, name))
print("\t* %s...\tdeleted" % path)
print("\t* %s - %s" % (mtime.strftime('%d.%m.%Y'), name))
Copy link

cholin commented Nov 10, 2014

See for example output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment