From 01543d857bfe3a56a8d4fc49b80c8556a4228b29 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Sun, 21 Aug 2022 12:00:07 +0200 Subject: [PATCH] Record disk usage in LTsDb --- record_df | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ record_dus | 35 ++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100755 record_df create mode 100755 record_dus diff --git a/record_df b/record_df new file mode 100755 index 0000000..b5eb9ec --- /dev/null +++ b/record_df @@ -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() diff --git a/record_dus b/record_dus new file mode 100755 index 0000000..6e77810 --- /dev/null +++ b/record_dus @@ -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")