#!/bin/bash # /usr/local/bin/nut-notify.sh # Emergency power notification script for Network UPS Tools on Snarfnet set -euo pipefail EVENT_TYPE="${1:-$NOTIFYTYPE}" UPS_NAME="${UPSNAME:-snarf-ups}" TIMESTAMP=$(date '+%Y-%m-%d %H:%M:%S') # Internal Gotify VIP endpoint (bypasses Authentik Forward Auth and NPM) GOTIFY_INTERNAL_URL="http://140.44.4.70:8090/message" GOTIFY_APP_TOKEN="${GOTIFY_APP_TOKEN:-A4uN4fEmErGeNcYtOkEn}" # Direct Discord fallback webhook (used if Shipyard/Gotify is already down during Tier 4-6 shutdown) DISCORD_UPS_WEBHOOK="${DISCORD_UPS_WEBHOOK:-https://discord.com/api/webhooks/1566076118535114813/2e_fCJeHb-mF8VwerzOORGRa-QUDFuwN6R2Gfz-MGUiIOudA0PiXGZSyd19PNFG-vR7D}" send_gotify() { local title="$1" local message="$2" local priority="$3" local color="${4:-16711680}" # Primary: Send to Gotify (which pushes to mobile devices + forwards to Discord ups-power webhook) if ! curl -sf -m 5 -X POST "${GOTIFY_INTERNAL_URL}?token=${GOTIFY_APP_TOKEN}" \ -H "Content-Type: application/json" \ -d "{\"title\": \"${title}\", \"message\": \"${message}\", \"priority\": ${priority}}" >/dev/null 2>&1; then # Fallback: If Gotify/Shipyard is already powered down (Tier 4+), send directly to Discord via UDM Pro (Tier 6) curl -sf -m 5 -X POST "${DISCORD_UPS_WEBHOOK}" \ -H "Content-Type: application/json" \ -d "{\"username\": \"SnarfNet NUT Master (Direct Fallback)\", \"embeds\": [{\"title\": \"${title}\", \"description\": \"${message}\", \"color\": ${color}}]}" >/dev/null 2>&1 || true fi } case "$EVENT_TYPE" in ONBATT) send_gotify "⚡ Grid Power Lost" "[$TIMESTAMP] Homelab is running on UPS battery (${UPS_NAME}). Staged shutdown countdown started." 8 16753920 logger -t nut-notify "Grid power failure detected - running on UPS battery." ;; ONLINE) send_gotify "✅ Grid Power Restored" "[$TIMESTAMP] Utility AC power restored to ${UPS_NAME}. Battery recharging." 5 3066993 logger -t nut-notify "AC utility power restored." ;; LOWBATT) send_gotify "🚨 UPS Battery Critical" "[$TIMESTAMP] Battery level critical on ${UPS_NAME}! Final Tier 4-6 shutdown initiated." 10 15158332 logger -t nut-notify "CRITICAL: UPS battery low. Enforcing emergency shutdown." ;; FSD) send_gotify "🛑 Forced Shutdown Engaged" "[$TIMESTAMP] NUT Master has commanded immediate power cutoff across all clusters." 10 10038562 logger -t nut-notify "FSD signal received. Halting systems." ;; *) logger -t nut-notify "Unhandled UPS event: ${EVENT_TYPE}" ;; esac