refactor(mx): extract disk check services into disk-check.nix
Share the check script via a parameterized mkDiskCheck function over
{ name, mountPoint, label } and iterate an attrset to emit the boot
and root services plus their daily timers.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
6d0186eadb
commit
4045aa1859
2 changed files with 66 additions and 69 deletions
|
|
@ -1,10 +1,16 @@
|
||||||
{ pkgs, lib, config, ... }:
|
{
|
||||||
|
pkgs,
|
||||||
|
lib,
|
||||||
|
config,
|
||||||
|
...
|
||||||
|
}:
|
||||||
{
|
{
|
||||||
imports = [
|
imports = [
|
||||||
# ./goaccess.nix
|
# ./goaccess.nix
|
||||||
./acme.nix
|
./acme.nix
|
||||||
./backup.nix
|
./backup.nix
|
||||||
./coturn.nix
|
./coturn.nix
|
||||||
|
./disk-check.nix
|
||||||
./forgejo.nix
|
./forgejo.nix
|
||||||
./hardware-configuration.nix
|
./hardware-configuration.nix
|
||||||
./headscale.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";
|
system.stateVersion = "23.05";
|
||||||
}
|
}
|
||||||
|
|
|
||||||
59
systems/x86_64-linux/mx/disk-check.nix
Normal file
59
systems/x86_64-linux/mx/disk-check.nix
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue