Files
proxmox/scripts/unifi/monitor-health.sh
defiQUG b3a8fe4496
Some checks failed
Deploy to Phoenix / deploy (push) Has been cancelled
chore: sync all changes to Gitea
- Config, docs, scripts, and backup manifests
- Submodule refs unchanged (m = modified content in submodules)

Made-with: Cursor
2026-03-02 11:37:34 -08:00

67 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Monitor UniFi controller health and send alerts
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
LOG_FILE="${LOG_FILE:-$PROJECT_ROOT/logs/unifi-health.log}"
cd "$PROJECT_ROOT"
# Create logs directory if it doesn't exist
mkdir -p "$(dirname "$LOG_FILE")"
# Load environment variables
if [ -f ~/.env ]; then
source <(grep "^UNIFI_" ~/.env | sed 's/^/export /')
fi
TIMESTAMP=$(date -Iseconds)
STATUS="unknown"
# Function to log message
log() {
echo "[$TIMESTAMP] $1" | tee -a "$LOG_FILE"
}
# Function to check API health
check_api_health() {
if NODE_TLS_REJECT_UNAUTHORIZED=0 pnpm unifi:cli sites 2>&1 | grep -q "internalReference"; then
return 0
else
return 1
fi
}
# Function to send alert (placeholder - implement your alerting mechanism)
send_alert() {
local message="$1"
log "ALERT: $message"
# Add your alerting mechanism here (email, webhook, etc.)
# Example: curl -X POST https://your-webhook-url -d "{\"text\":\"$message\"}"
}
# Main health check
log "Starting health check..."
if check_api_health; then
STATUS="healthy"
log "✅ Controller is healthy"
else
STATUS="unhealthy"
log "❌ Controller health check failed"
send_alert "UniFi Controller API health check failed at $TIMESTAMP"
fi
log "Health check complete: $STATUS"
# Exit with appropriate code
if [ "$STATUS" = "healthy" ]; then
exit 0
else
exit 1
fi