AkurAI Build
Menu

Public repository / 10d19fe0941c186ec244d1f0e2af6b19200dcb0c

AkurAI-Mail / scripts/ops/disk-alert.sh

Latest change e0753e49bfea7ef672aabf8df9a3a27b8bdd9dc7 - Recover EC2-only ops scripts, checks and live divergent copies by Ólafur Búi Ólafsson

#!/usr/bin/env bash
# scripts/ops/disk-alert.sh — root-disk threshold alert for the mail VM.
# Run by disk-alert.timer (systemd). Sends one mail per threshold crossing per
# day (state file dedupes) via the local Postfix relay (already configured to
# Brevo — see `postconf -n | grep relayhost`).
set -euo pipefail

: "${DISK_ALERT_TO:=postmaster@olibuijr.com}"
: "${DISK_ALERT_STATE:=/var/lib/akurai-mail-ops/disk-alert.state}"
THRESHOLDS=(85 80 75)

mkdir -p "$(dirname "$DISK_ALERT_STATE")"
used=$(df -P / | awk 'NR==2 {gsub("%","",$5); print $5}')
today=$(date +%F)
last=""
[ -f "$DISK_ALERT_STATE" ] && last=$(cat "$DISK_ALERT_STATE")

for t in "${THRESHOLDS[@]}"; do
    if [ "$used" -ge "$t" ]; then
        key="${today}:${t}"
        if [ "$last" != "$key" ]; then
            {
                echo "Subject: [mail VM] root disk at ${used}% (>= ${t}% threshold)"
                echo "To: ${DISK_ALERT_TO}"
                echo
                echo "root filesystem usage: ${used}%"
                df -h /
                echo
                echo "top consumers:"
                du -h -d2 / 2>/dev/null | sort -rh | head -15 || true
            } | /usr/sbin/sendmail -t
            echo "$key" > "$DISK_ALERT_STATE"
        fi
        exit 0
    fi
done

# Below every threshold: nothing to do. Explicit success so `set -e` doesn't
# leave the failing `[ -ge ]` test as the script's exit status (systemd would
# report the normal no-alert run as a unit FAILURE).
exit 0