#!/bin/bash
# Fails (exit 1) when any ZFS pool's capacity is at or above WARN_PCT, so that
# OnFailure=unit-failure-mail@%n.service mails an alert. Written after the
# 2026-08-11 incident where rpool hit genuine ENOSPC mid-backup and broke
# pmxcfs (the cluster config filesystem) pool-wide for ~7.5 hours -- no OOM,
# no crash, just every /etc/pve write failing until the box was rebooted.
set -uo pipefail

WARN_PCT="${WARN_PCT:-90}"

status=0
while read -r name cap; do
    cap="${cap%\%}"
    if [ "$cap" -ge "$WARN_PCT" ]; then
        echo "POOL '$name' AT ${cap}% CAPACITY (warn threshold ${WARN_PCT}%)"
        status=1
    fi
done < <(zpool list -H -o name,capacity)

zpool list
exit "$status"
