Record disk usage in LTsDb

This commit is contained in:
Peter J. Holzer 2022-08-21 12:00:07 +02:00
parent 6d689666e9
commit 01543d857b
2 changed files with 91 additions and 0 deletions

56
record_df Executable file
View File

@ -0,0 +1,56 @@
#!/usr/bin/python3
import os
import socket
import time
from ltsdb_json import LTS
# Use a whitelist since there are a lot of uninteresting file system
# types on modern linux systems
interesting_fstypes = {"ext4", "tmpfs", "vfat"}
# It's a bit weird that this works.
hostname = socket.gethostbyaddr(socket.gethostname())[0]
with open("/proc/mounts") as mfh:
for ln in mfh:
(dev, mountpoint, fstype, options, _, _) = ln.split()
if fstype not in interesting_fstypes:
continue
st = os.statvfs(mountpoint)
now = time.time()
# We record the capacity usable by unprivileged processes, not the
# total size of the filesystem. This is more relevant and also what df
# uses as the "100%" mark.
blocks_reserved = st.f_bfree - st.f_bavail
blocks_usable = st.f_blocks - blocks_reserved
blocks_used = st.f_blocks - st.f_bfree
bytes_usable = blocks_usable * st.f_bsize
bytes_used = blocks_used * st.f_bsize
ts = LTS(
{
"hostname": hostname,
"mountpoint": mountpoint,
"fstype": fstype,
"measure": "bytes_usable",
"unit": "bytes",
}
)
ts.add(now, bytes_usable)
ts.save()
ts = LTS(
{
"hostname": hostname,
"mountpoint": mountpoint,
"fstype": fstype,
"measure": "bytes_used",
"unit": "bytes",
}
)
ts.add(now, bytes_used)
ts.save()

35
record_dus Executable file
View File

@ -0,0 +1,35 @@
#!/usr/bin/python3
import argparse
import os
import socket
import subprocess
import time
from ltsdb_json import LTS
ap = argparse.ArgumentParser()
ap.add_argument("directory")
args = ap.parse_args()
# It's a bit weird that this works.
hostname = socket.gethostbyaddr(socket.gethostname())[0]
p = subprocess.run(["/usr/bin/dus", args.directory], capture_output=True, text=True)
t0 = time.time()
for ln in p.stdout.split("\n")[:-1]:
(size, directory) = ln.split("\t")
ts = LTS(
{
"hostname": hostname,
"directory": directory,
"measure": "diskusage",
"unit": "bytes",
}
)
ts.add(t0, int(size)*1024)
ts.save()
t1 = time.time()
print("recorded in", t1 - t0, "seconds")