diff --git a/miyagi-backup.sh b/miyagi-backup.sh index e24f4ff..92a82ce 100644 --- a/miyagi-backup.sh +++ b/miyagi-backup.sh @@ -75,7 +75,21 @@ get_sourcehostname() { log "Detected SOURCEHOSTNAME: $SOURCEHOSTNAME" fi } +zfs_replace() { + local replace_detected=false + for pool in $(zpool list -H -o name); do + if zpool status "$pool" 2>/dev/null | grep -qiE "replacing|in replacing|scan: replacing"; then + log "Replace-Vorgang läuft im Pool: $pool" + replace_detected=true + fi + done + if $replace_detected; then + return 0 + else + return 1 + fi +} set_wol_g_enabled() { log "Checking if ethtool is installed..." @@ -304,12 +318,98 @@ run_scrub_stop() { ' } +write_pbs_status() { + local spool_file="/var/lib/check_mk_agent/spool/90000_checkpbs_local" + local repo="$BACKUPSTOREPBS" + local tmpfile + tmpfile=$(mktemp) + + if ! command -v proxmox-backup-client >/dev/null 2>&1; then + log "proxmox-backup-client nicht installiert – PBS-Status-Ausgabe übersprungen." + return + fi + + echo "<<>>" > "$tmpfile" + + local now + now=$(date +%s) + + # === Schwellenwert(e) parsen === + local warn_threshold=0 + local crit_threshold=86400 # Default kritisch ab 24h + + if [[ -n "${PBSBACKUP_STATUS:-}" ]]; then + IFS=',' read -r t1 t2 <<< "$PBSBACKUP_STATUS" + if [[ "$t1" =~ ^[0-9]+$ && -z "$t2" ]]; then + # Nur ein Wert vorhanden + crit_threshold=$t1 + log "Verwende einfachen Schwellenwert: CRIT=${crit_threshold}s" + elif [[ "$t1" =~ ^[0-9]+$ && "$t2" =~ ^[0-9]+$ ]]; then + warn_threshold=$t1 + crit_threshold=$t2 + log "Verwende gestaffelte Schwellenwerte: WARN=${warn_threshold}s, CRIT=${crit_threshold}s" + else + log "WARNUNG: Ungültiges PBSBACKUP_STATUS-Format – Fallback: CRIT=${crit_threshold}s" + fi + fi + + # Backup-Daten extrahieren + rssh root@"$SOURCEHOST" proxmox-backup-client list --repository "$repo" --output-format json 2>/dev/null | \ + grep -E '"backup-id"|"backup-type"|"backup-time"' | \ + sed -E 's/[",]//g; s/^ *//' > "$tmpfile.json" + + local backup_type="" backup_id="" backup_time="" + while read -r line; do + case "$line" in + backup-type:*) + backup_type="${line#*: }" + ;; + backup-id:*) + backup_id="${line#*: }" + ;; + backup-time:*) + backup_time="${line#*: }" + + if [[ -n "$backup_type" && -n "$backup_id" && -n "$backup_time" ]]; then + local age status timestamp msg + age=$((now - backup_time)) + timestamp=$(date -d "@$backup_time" "+%Y-%m-%d %H:%M:%S") + + if (( age < warn_threshold )); then + status=0 + elif (( age < crit_threshold )); then + status=1 + else + status=2 + fi + + msg="$backup_type/$backup_id last backup $timestamp (age: $((age / 3600))h)" + echo "$status PBS_${backup_type}_${backup_id} - $msg" >> "$tmpfile" + + # Reset + backup_type="" backup_id="" backup_time="" + fi + ;; + esac + done < "$tmpfile.json" + + rm -f "$tmpfile.json" + mv "$tmpfile" "$spool_file" + log "PBS Backup Status lokal geschrieben: $spool_file" +} + shutdown_now() { if [[ "${SHUTDOWN,,}" == "yes" ]]; then + if zfs_replace; then + log "ZFS Replace-Vorgang erkannt – Shutdown abgebrochen." + return + fi + send_piggyback send_piggyback_external send_checkzfs_external - log "Shutting down now..." + log "Shutting down now...in 60sec" + sleep 60 shutdown now else log "No shutdown requested." @@ -424,11 +524,10 @@ wait() { if [[ $? -eq 0 ]]; then log "Press ENTER to continue." else - log "No press ENTER, wait for 60 Sec." + log "No press ENTER, wait for 60 Sec.." sleep 60 fi } - # Main execution: if [[ $# -eq 0 ]]; then if [[ -n "$CONFIG_FILE" ]]; then