Last active
August 29, 2015 14:09
-
-
Save cholin/3378b204ab7513bc6024 to your computer and use it in GitHub Desktop.
monitor_filter_non_reporting_nodes.py
This file contains 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/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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See http://pastebin.com/SHf6FCZV for example output.