Automatically clean up old torrent files

This commit is contained in:
Pim Kunis 2025-03-23 13:54:29 +01:00
parent 10d7e92c0d
commit 274ff4b98c

View file

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