diff --git a/machines/lewis/configuration.nix b/machines/lewis/configuration.nix index fc069bf..d6c2017 100644 --- a/machines/lewis/configuration.nix +++ b/machines/lewis/configuration.nix @@ -2,6 +2,7 @@ self, config, pkgs, + lib, ... }: { config = { @@ -22,5 +23,48 @@ data-sharing.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"]; }; }