diff --git a/updater.sh b/updater.sh index f9a6b84..5d9ad38 100644 --- a/updater.sh +++ b/updater.sh @@ -19,6 +19,12 @@ EXCLUDE_GUESTS="${EXCLUDE_GUESTS:-}" APT_LOCK_TIMEOUT="${APT_LOCK_TIMEOUT:-600}" LOG_FILE="${LOG_FILE:-/var/log/pve-guest-update.log}" +# Proxmox Notification System +NOTIFY_TARGET="${NOTIFY_TARGET:-iCloud-SMTP}" +NOTIFY_MATCHER="${NOTIFY_MATCHER:-proxmox-updater}" +NOTIFY_TEMPLATE="${NOTIFY_TEMPLATE:-proxmox-updater}" +NOTIFY_ENABLED="${NOTIFY_ENABLED:-yes}" + MODE="update" ONLY_CT="" ONLY_VM="" @@ -27,6 +33,8 @@ NO_REBOOT="no" ERRORS=0 HOST_OK="not-run" HOST_REBOOT_REQUIRED="no" +HOST_REBOOT_SCHEDULED="no" +MAIL_STATUS_DETAIL="Keine automatischen Neustarts erforderlich." # Laufende Gäste, die nach erfolgreichem Update einen Neustart benötigen. # Die Neustarts werden erst am Ende des gesamten Laufs entschieden. @@ -61,6 +69,133 @@ log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" } +ensure_notification_setup() { + [[ "$NOTIFY_ENABLED" == "yes" ]] || return 0 + + # Das konfigurierte Proxmox-Target muss existieren. + if ! pvesh get /cluster/notifications/targets --output-format json 2>/dev/null | + grep -Fq "\"name\":\"$NOTIFY_TARGET\"" + then + log "WARNUNG: Notification-Target '$NOTIFY_TARGET' nicht gefunden; keine Mail." + return 1 + fi + + # Eigene Templates für Betreff und Nachricht. + local template_dir="/etc/pve/notification-templates/default" + mkdir -p "$template_dir" + + cat > "$template_dir/${NOTIFY_TEMPLATE}-subject.txt.hbs" <<'EOF' +{{ title }} +EOF + + cat > "$template_dir/${NOTIFY_TEMPLATE}-body.txt.hbs" <<'EOF' +{{ message }} +EOF + + cat > "$template_dir/${NOTIFY_TEMPLATE}-body.html.hbs" <<'EOF' +
{{ message }}
+EOF + + # Eigener Matcher: nur Meldungen dieses Updaters an das vorhandene + # Proxmox-Notification-Target senden. + if ! pvesh get "/cluster/notifications/matchers/$NOTIFY_MATCHER" \ + >/dev/null 2>&1 + then + if ! pvesh create /cluster/notifications/matchers \ + --name "$NOTIFY_MATCHER" \ + --mode all \ + --match-field 'exact:type=proxmox-updater' \ + --target "$NOTIFY_TARGET" \ + --comment 'Proxmox Guest Updater' \ + >/dev/null + then + log "WARNUNG: Notification-Matcher '$NOTIFY_MATCHER' konnte nicht angelegt werden." + return 1 + fi + fi + + return 0 +} + +send_update_notification() { + [[ "$MODE" == "update" ]] || return 0 + [[ "$NOTIFY_ENABLED" == "yes" ]] || return 0 + + if ! ensure_notification_setup; then + return 0 + fi + + local severity="info" + local status="Erfolgreich" + local title + local message + + if (( ERRORS > 0 )); then + severity="error" + status="FEHLER" + fi + + title="Proxmox Updater - $status" + + message="Proxmox Guest Updater +Host: $(hostname) +Zeit: $(date '+%d.%m.%Y %H:%M:%S') + +Host: + Update: $HOST_OK + Reboot erforderlich: $HOST_REBOOT_REQUIRED + Reboot geplant: $HOST_REBOOT_SCHEDULED + +LXC: + Erfolgreich: $LXC_OK + Fehlgeschlagen: $LXC_FAILED + Übersprungen: $LXC_SKIPPED + +VM: + Erfolgreich: $VM_OK + Fehlgeschlagen: $VM_FAILED + Übersprungen: $VM_SKIPPED + +Fehler gesamt: $ERRORS + +Neustarts: + $MAIL_STATUS_DETAIL + +Log: + $LOG_FILE" + + export PVE_UPDATER_SEVERITY="$severity" + export PVE_UPDATER_TITLE="$title" + export PVE_UPDATER_MESSAGE="$message" + export PVE_UPDATER_TEMPLATE="$NOTIFY_TEMPLATE" + + if perl -MPVE::Notify -e ' + my $severity = $ENV{PVE_UPDATER_SEVERITY}; + my $template = $ENV{PVE_UPDATER_TEMPLATE}; + + my $data = { + %{PVE::Notify::common_template_data()}, + title => ($ENV{PVE_UPDATER_TITLE} // ""), + message => ($ENV{PVE_UPDATER_MESSAGE} // ""), + }; + + my $fields = { + type => "proxmox-updater", + }; + + PVE::Notify::notify($severity, $template, $data, $fields); + ' + then + log "E-Mail-Benachrichtigung über '$NOTIFY_TARGET' versendet: $title" + else + # Ein Mailproblem macht einen ansonsten erfolgreichen Update-Lauf + # nicht nachträglich zu einem Updatefehler. + log "WARNUNG: E-Mail-Benachrichtigung konnte nicht versendet werden." + fi + + unset PVE_UPDATER_SEVERITY PVE_UPDATER_TITLE PVE_UPDATER_MESSAGE PVE_UPDATER_TEMPLATE +} + usage() { cat <<'EOF' Proxmox Guest Updater @@ -861,6 +996,8 @@ log "============================================================" if [[ "$MODE" == "update" ]]; then if (( ERRORS > 0 )); then + MAIL_STATUS_DETAIL="Keine automatischen Neustarts ausgeführt, weil $ERRORS Fehler im Update-Lauf aufgetreten sind." + if [[ -z "$ONLY_CT" && -z "$ONLY_VM" && "$HOST_REBOOT_REQUIRED" == "yes" ]]; then log "" log "Host-Neustart erforderlich, aber automatischer Neustart wird NICHT ausgeführt." @@ -876,7 +1013,13 @@ if [[ "$MODE" == "update" ]]; then elif [[ -n "$ONLY_CT" || -n "$ONLY_VM" ]]; then # Gezielter Gast-Lauf: Der Proxmox-Host wurde nicht aktualisiert. # Deshalb notwendige Gast-Neustarts direkt am Ende ausführen. + local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} )) reboot_pending_guests + if (( local_pending > 0 )); then + MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet." + else + MAIL_STATUS_DETAIL="Kein Neustart erforderlich." + fi elif [[ "$HOST_OK" == "yes" \ && "$HOST_REBOOT_REQUIRED" == "yes" \ @@ -890,6 +1033,9 @@ if [[ "$MODE" == "update" ]]; then log "Automatischer Neustart in 1 Minute." log "Proxmox fährt die laufenden Gäste geordnet herunter." + HOST_REBOOT_SCHEDULED="yes" + MAIL_STATUS_DETAIL="Host-Neustart in 1 Minute geplant; notwendige Gast-Neustarts werden dadurch mit erledigt." + sync shutdown -r +1 \ @@ -899,8 +1045,21 @@ if [[ "$MODE" == "update" ]]; then # Kein automatischer Host-Neustart: notwendige Gast-Neustarts jetzt # einzeln ausführen. Das gilt auch bei --no-reboot oder wenn der # automatische Host-Neustart in der Konfiguration deaktiviert ist. + local_pending=$(( ${#LXC_REBOOT_PENDING[@]} + ${#VM_REBOOT_PENDING[@]} )) reboot_pending_guests + + if (( local_pending > 0 )); then + MAIL_STATUS_DETAIL="$local_pending notwendige(r) Gast-Neustart(s) wurde(n) verarbeitet." + elif [[ "$HOST_REBOOT_REQUIRED" == "yes" ]]; then + MAIL_STATUS_DETAIL="Host-Neustart erforderlich, aber automatischer Host-Reboot ist deaktiviert/unterdrückt." + else + MAIL_STATUS_DETAIL="Kein Neustart erforderlich." + fi fi + + # Erst nach allen Update- und Reboot-Entscheidungen versenden, damit + # der Bericht den tatsächlichen Endzustand des Laufs enthält. + send_update_notification fi if (( ERRORS > 0 )); then