Monitor media directory sizes with node exporter

This commit is contained in:
Pim Kunis 2025-03-23 13:43:22 +01:00
parent 27995cbc04
commit 10d7e92c0d

View file

@ -2,6 +2,7 @@
self, self,
config, config,
pkgs, pkgs,
lib,
... ...
}: { }: {
config = { config = {
@ -22,5 +23,48 @@
data-sharing.enable = true; data-sharing.enable = true;
backups.enable = true; backups.enable = true;
}; };
systemd.timers.read-dir-sizes = {
wantedBy = ["timers.target"];
timerConfig = {
OnBootSec = "5m";
OnUnitActiveSec = "5m";
Unit = "read-dir-sizes.service";
};
};
systemd.services."read-dir-sizes" = {
script = let
script = pkgs.writeShellScriptBin "read-dir-sizes.sh" ''
DIRS=(
"/mnt/longhorn/persistent/media/movies"
"/mnt/longhorn/persistent/media/shows"
)
temp_file=$(mktemp)
trap 'rm -f "$temp_file"' EXIT
for DIR_PATH in "''${DIRS[@]}"; do
# Find all top-level subdirectories and calculate their size
find "$DIR_PATH" -mindepth 1 -maxdepth 1 -type d | while read -r subdir; do
# Calculate the size of the top-level subdirectory
du --block-size=1 -s "$subdir" | while read -r size path; do
# Print size in Prometheus format
echo "directory_size_bytes{dir=\"$path\"} $size" >> $temp_file
done
done
done
mkdir -p /var/lib/node_exporter/textfile_collector
cp $temp_file /var/lib/node_exporter/textfile_collector/dir_sizes.prom
chmod o=r /var/lib/node_exporter/textfile_collector/dir_sizes.prom
'';
in "${lib.getExe script}";
serviceConfig = {
Type = "oneshot";
User = "root";
};
};
services.prometheus.exporters.node.extraFlags = ["--collector.textfile.directory=/var/lib/node_exporter/textfile_collector"];
}; };
} }