#!/usr/bin/python3 # PoC # add a new entry and remove a random old entry with a probability # depending on the target size import argparse import datetime import json import random import sys import time import os ap = argparse.ArgumentParser() ap.add_argument("--targetsize", type=int, default=1000) ap.add_argument("filesystem") args = ap.parse_args() st = os.statvfs(args.filesystem) blocks_reserved = st.f_bfree - st.f_bavail blocks_usable = st.f_blocks - blocks_reserved blocks_used = st.f_blocks - st.f_bfree # Just for readability bytes_usable = blocks_usable * st.f_bsize bytes_used = blocks_used * st.f_bsize filename = args.filesystem.replace("/", "_") + ".bytes.json" ts = [] try: with open(filename) as f: ts = json.load(f) except FileNotFoundError as e: pass # Ok, will create now = time.time() ts.append((now, bytes_used, bytes_usable)) if random.random() > 1 - len(ts) / args.targetsize: i = random.randrange(0, len(ts)) ts.pop(i) with open(filename, "w") as f: json.dump(ts, f) for d in reversed(ts): bytes_predicted = bytes_used * bytes_used / d[1] print("Predicted use", bytes_predicted, "in", now - d[0], "seconds", "at", datetime.datetime.fromtimestamp(now + now - d[0])) if bytes_predicted > bytes_usable: print("Predicted disk full", "in", now - d[0], "seconds", "at", datetime.datetime.fromtimestamp(now + now - d[0])) sys.exit(1) else: print("safe for", now - ts[0][0], "seconds", "at", datetime.datetime.fromtimestamp(now + now - ts[0][0]))