Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
monitor_filter_non_reporting_nodes.py
#!/usr/bin/env python2
import argparse
import os
import shutil
from datetime import datetime, timedelta
RRD_PATH='/var/lib/collectd/rrd/'
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)
files.append(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 = datetime.today() - 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)
else:
print("\t* %s - %s" % (mtime.strftime('%d.%m.%Y'), name))
@cholin
Copy link
Author

cholin commented Nov 10, 2014

See http://pastebin.com/SHf6FCZV for example output.

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