Utdate notification service

This commit is contained in:
MacRimi
2026-03-25 19:47:47 +01:00
parent 6c2b03ae76
commit d53c1dc402
4 changed files with 44 additions and 8 deletions

View File

@@ -1325,7 +1325,7 @@ class HealthPersistence:
print(f"[HealthPersistence] Error recording UNKNOWN persistent: {e}")
# ───────────────────────────────────────────────────────────────
# ───────────────────────────────────────────────────────────────<EFBFBD><EFBFBD>
# Disk Observations API
# ────────────────────────────────────────────────────────────────

View File

@@ -1043,8 +1043,16 @@ class NotificationManager:
entity_id=entity_id,
)
# Process the event through the normal pipeline
return self._process_single_event(event)
# For urgent events (shutdown/reboot), dispatch directly to ensure
# immediate delivery before the system goes down.
# For other events, use the normal pipeline with aggregation.
_URGENT_EVENTS = {'system_shutdown', 'system_reboot'}
if event_type in _URGENT_EVENTS:
self._dispatch_event(event)
return {'success': True, 'event_type': event_type, 'dispatched': 'direct'}
else:
self._process_event(event)
return {'success': True, 'event_type': event_type, 'dispatched': 'queued'}
def send_notification(self, event_type: str, severity: str,
title: str, message: str,

View File

@@ -6,16 +6,26 @@
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_DIR="/var/lib/proxmenux"
CONFIG_FILE="$CONFIG_DIR/config.json"
LOG_FILE="/var/log/proxmenux-shutdown.log"
PORT="${PORT:-5000}"
# Logging function
log() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> "$LOG_FILE" 2>/dev/null || true
}
log "=== Shutdown notification script started ==="
# Determine if this is a reboot or shutdown
# Check for systemd targets or runlevel
is_reboot=false
if systemctl is-active --quiet reboot.target 2>/dev/null; then
is_reboot=true
log "Detected: reboot.target is active"
elif [ -f /run/systemd/shutdown/scheduled ]; then
if grep -q "reboot" /run/systemd/shutdown/scheduled 2>/dev/null; then
is_reboot=true
log "Detected: /run/systemd/shutdown/scheduled contains 'reboot'"
fi
fi
@@ -29,19 +39,37 @@ else
fi
hostname=$(hostname)
log "Event type: $event_type, Hostname: $hostname"
# Try to send notification via internal API endpoint
# The Flask server may still be running at this point
curl -s -X POST "http://127.0.0.1:$PORT/api/internal/shutdown-event" \
log "Sending notification to http://127.0.0.1:$PORT/api/internal/shutdown-event"
response=$(curl -s -w "\n%{http_code}" -X POST "http://127.0.0.1:$PORT/api/internal/shutdown-event" \
-H "Content-Type: application/json" \
-d "{\"event_type\": \"$event_type\", \"hostname\": \"$hostname\", \"reason\": \"$reason\"}" \
--max-time 5 2>/dev/null || true
--max-time 10 2>&1)
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | head -n -1)
log "Response HTTP code: $http_code"
log "Response body: $body"
if [ "$http_code" = "200" ]; then
log "Notification sent successfully"
else
log "WARNING: Notification may have failed (HTTP $http_code)"
fi
# Give the notification a moment to be sent
sleep 2
log "Waiting 3 seconds for notification delivery..."
sleep 3
# Now terminate the Flask process
# Find the main process and send SIGTERM
pkill -TERM -f "proxmenux-monitor" 2>/dev/null || true
log "Terminating Flask process..."
pkill -TERM -f "flask_server" 2>/dev/null || true
log "=== Shutdown notification script completed ==="
exit 0

View File

@@ -30,7 +30,7 @@ NC='\033[0m'
API="http://127.0.0.1:8008"
LOG_FILE="/tmp/proxmenux_real_test_$(date +%Y%m%d_%H%M%S).log"
# ── Helpers ────────────────────────────────────────────────────
# ── Helpers ─────────────────────<EFBFBD><EFBFBD><EFBFBD>───────────────────────────────
log() { echo -e "$1" | tee -a "$LOG_FILE"; }
header() {
echo "" | tee -a "$LOG_FILE"