diff --git a/systems/x86_64-linux/mx/default.nix b/systems/x86_64-linux/mx/default.nix index f0043c3..ec2fd89 100644 --- a/systems/x86_64-linux/mx/default.nix +++ b/systems/x86_64-linux/mx/default.nix @@ -1,10 +1,16 @@ -{ pkgs, lib, config, ... }: +{ + pkgs, + lib, + config, + ... +}: { imports = [ # ./goaccess.nix ./acme.nix ./backup.nix ./coturn.nix + ./disk-check.nix ./forgejo.nix ./hardware-configuration.nix ./headscale.nix @@ -103,73 +109,5 @@ ]; }; - systemd.services = { - check_boot = { - serviceConfig = { - Type = "oneshot"; - Environment = "PATH=/run/current-system/sw/bin"; - ExecStart = toString ( - pkgs.writeShellScript "check_boot.sh" '' - CURRENT=$(df /boot | grep /boot | awk '{ print $5}' | sed 's/%//g') - THRESHOLD=85 - - if [ "$CURRENT" -gt "$THRESHOLD" ] ; then - ${pkgs.mailutils}/bin/mail -s '/boot Disk Space Alert' harald << EOF - Your /boot partition remaining free space is critically low. Used: $CURRENT% - EOF - TOKEN=$(cat ${config.sops.secrets.ntfy.path}) - ${pkgs.curl}/bin/curl -s -H "Authorization: Bearer $TOKEN" \ - -H "Title: /boot Disk Space Alert" \ - -H "Priority: high" \ - -d "Boot partition at $CURRENT%" \ - http://127.0.0.1:2586/alerts - fi - '' - ); - }; - wantedBy = [ "default.target" ]; - }; - check_root = { - serviceConfig = { - Type = "oneshot"; - Environment = "PATH=/run/current-system/sw/bin"; - ExecStart = toString ( - pkgs.writeShellScript "check_root.sh" '' - CURRENT=$(df / | grep / | awk '{ print $5}' | sed 's/%//g') - THRESHOLD=85 - - if [ "$CURRENT" -gt "$THRESHOLD" ] ; then - ${pkgs.mailutils}/bin/mail -s '/ Disk Space Alert' harald << EOF - Your root partition remaining free space is critically low. Used: $CURRENT% - EOF - TOKEN=$(cat ${config.sops.secrets.ntfy.path}) - ${pkgs.curl}/bin/curl -s -H "Authorization: Bearer $TOKEN" \ - -H "Title: / Disk Space Alert" \ - -H "Priority: high" \ - -d "Root partition at $CURRENT%" \ - http://127.0.0.1:2586/alerts - fi - '' - ); - }; - wantedBy = [ "default.target" ]; - }; - }; - - systemd.timers = { - check_boot = { - timerConfig = { - OnCalendar = "daily"; - }; - wantedBy = [ "timers.target" ]; - }; - check_root = { - timerConfig = { - OnCalendar = "daily"; - }; - wantedBy = [ "timers.target" ]; - }; - }; - system.stateVersion = "23.05"; } diff --git a/systems/x86_64-linux/mx/disk-check.nix b/systems/x86_64-linux/mx/disk-check.nix new file mode 100644 index 0000000..8dc7857 --- /dev/null +++ b/systems/x86_64-linux/mx/disk-check.nix @@ -0,0 +1,59 @@ +{ pkgs, config, ... }: +let + mkDiskCheck = + { + name, + mountPoint, + label, + }: + { + serviceConfig = { + Type = "oneshot"; + Environment = "PATH=/run/current-system/sw/bin"; + ExecStart = toString ( + pkgs.writeShellScript "${name}.sh" '' + CURRENT=$(df ${mountPoint} | awk 'NR==2 { print $5 }' | sed 's/%//g') + THRESHOLD=85 + + if [ "$CURRENT" -gt "$THRESHOLD" ] ; then + ${pkgs.mailutils}/bin/mail -s '${mountPoint} Disk Space Alert' harald << EOF + Your ${label} partition remaining free space is critically low. Used: $CURRENT% + EOF + TOKEN=$(cat ${config.sops.secrets.ntfy.path}) + ${pkgs.curl}/bin/curl -s -H "Authorization: Bearer $TOKEN" \ + -H "Title: ${mountPoint} Disk Space Alert" \ + -H "Priority: high" \ + -d "${label} partition at $CURRENT%" \ + http://127.0.0.1:2586/alerts + fi + '' + ); + }; + wantedBy = [ "default.target" ]; + }; + + checks = { + check_boot = { + mountPoint = "/boot"; + label = "Boot"; + }; + check_root = { + mountPoint = "/"; + label = "Root"; + }; + }; +in +{ + systemd.services = builtins.mapAttrs ( + name: cfg: + mkDiskCheck { + inherit name; + inherit (cfg) mountPoint label; + } + ) checks; + + systemd.timers = builtins.mapAttrs (_: _: { + timerConfig.OnCalendar = "daily"; + wantedBy = [ "timers.target" ]; + }) checks; +}