Use a lossy timeseries to predict when a filesystem will be full (PoC)

This commit is contained in:
Peter J. Holzer 2022-08-20 17:40:16 +02:00 committed by Peter J. Holzer
parent 5630f43e62
commit 1a4d8ef471
3 changed files with 72 additions and 0 deletions

64
predict_disk_full Executable file
View File

@ -0,0 +1,64 @@
#!/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]))

4
predict_disk_full_home_bytes Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
set -e
cd $(dirname $0)
./predict_disk_full /home

4
predict_disk_full_var_bytes Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
set -e
cd $(dirname $0)
./predict_disk_full /var