diff options
author | tdback <tyler@tdback.net> | 2025-01-05 14:03:49 -0500 |
---|---|---|
committer | tdback <tyler@tdback.net> | 2025-01-05 14:03:49 -0500 |
commit | 3cfffca88a65f56949126023909d61c234b79142 (patch) | |
tree | f3ec6076f8dff8e666c6edcc0b47642538b1f91e /modules/scripts/zquota | |
parent | 21a405e977754d31215244cbd4be5eb928732e69 (diff) |
fix: cleanup validation logic
Diffstat (limited to 'modules/scripts/zquota')
-rw-r--r-- | modules/scripts/zquota/default.nix | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/modules/scripts/zquota/default.nix b/modules/scripts/zquota/default.nix index 7bbe1b1..5741e0e 100644 --- a/modules/scripts/zquota/default.nix +++ b/modules/scripts/zquota/default.nix @@ -3,7 +3,11 @@ with lib; let cfg = config.services.zquota; - zquota = let hostname = config.networking.hostName; in + zquota = let + bc = getExe pkgs.bc; + zfs = getExe pkgs.zfs; + hostname = config.networking.hostName; + in pkgs.writeShellScriptBin "zquota" '' set -e @@ -15,24 +19,25 @@ let DATASET="$1" QUOTA="$2" - if [ -n "$(echo "$QUOTA" | tr -d 0-9.)" ]; then + if echo "$QUOTA" | grep -q '[^0-9.]'; then echo "failed to provide a valid quota" >&2 - exit 1 + exit 1 fi - USED=$(${getExe pkgs.zfs} list -Hpo used "$DATASET" 2>/dev/null) || { - echo "failed to provide a valid dataset" >&2 - exit 1 + USED=$(${zfs} list -Hpo used "$DATASET" 2>/dev/null) || { + echo "failed to provide a valid dataset" >&2 + exit 1 } - USAGE=$(${getExe pkgs.bc} <<< "scale=2; $USED / 1024^3") + USAGE=$(${bc} <<< "scale=2; $USED / 1024^3") - DIFF=$(${getExe pkgs.bc} <<< "scale=2; $USAGE - $QUOTA") + DIFF=$(${bc} <<< "scale=2; $USAGE - $QUOTA") - (( $(awk '{ print ($1 > $2) }' <<< "$USAGE $QUOTA") )) && - /run/current-system/sw/bin/pushover -t "${hostname} quota exceeded" \ - "dataset $DATASET on ${hostname} has exceeded quota by ''${DIFF}GB" - ''; + if [ 1 -eq $(${bc} <<< "$USAGE > $QUOTA") ]; then + /run/current-system/sw/bin/pushover -t "${hostname} quota exceeded" \ + "dataset $DATASET on ${hostname} has exceeded quota by ''${DIFF}GB" + fi + ''; in { options = { services.zquota = { @@ -63,8 +68,8 @@ in { serviceConfig.Type = "oneshot"; script = strings.concatStringsSep "\n" ( - mapAttrsToList ( - dataset: quota: "/run/current-system/sw/bin/zquota ${dataset} ${builtins.toString quota}" + mapAttrsToList (dataset: quota: + "/run/current-system/sw/bin/zquota ${dataset} ${builtins.toString quota}" ) cfg.quotas ); }; |