#!/usr/bin/env bash
#
# CheckMK local check: Nextcloud
#
#   ohne Parameter : erzeugt <<<local:sep(0)>>> Output
#   --detect       : exit 0 = Plugin wird auf dieser Maschine benoetigt
#   --roots        : Debug - zeigt gefundene Webroots und Instanzen
#
# Benoetigt: bash, awk, sed, find, stat, php (CLI)
#
# Achtung: kein "set -e" - viele Aufrufe duerfen fehlschlagen, ohne dass das
# Plugin abbricht. Fehler werden pro Service in einen Status uebersetzt.
set -uo pipefail
# Werkzeugmeldungen landen im Servicetext: englisch und ohne Umlaute, damit
# sie zeichensatzsicher transportiert werden und die Muster unten greifen.
export LC_ALL=C LANG=C

########################### Konfiguration ##################################
OCC_TIMEOUT="${OCC_TIMEOUT:-120}"          # Timeout je occ-Aufruf
# Timeout fuer Config-Dumps der Webserver (nginx -T, apache2ctl). Normal unter
# einer Sekunde; ein Include auf einem toten Netzlaufwerk darf den Lauf nicht
# blockieren.
CMD_TIMEOUT="${CMD_TIMEOUT:-30}"
SEARCH_DEPTH="${SEARCH_DEPTH:-3}"          # Suchtiefe unterhalb eines Webroots

# Gleichzeitig laufende Checks insgesamt (nicht pro Instanz). 1 = seriell.
MAX_PARALLEL="${MAX_PARALLEL:-$(nproc 2>/dev/null || echo 4)}"
# Nicht-numerisch waere in (( )) unter set -u fatal und braeche den Lauf ab.
[[ "$MAX_PARALLEL" =~ ^[0-9]+$ ]] || MAX_PARALLEL=4

# Integritaetspruefung der installierten Apps. Kostet EINEN occ-Aufruf JE APP
# (occ kennt kein check-app --all), bei 30 Apps also gut eine halbe Minute.
# Der Core-Check laeuft immer.
NC_CHECK_APPS="${NC_CHECK_APPS:-0}"

# Nextcloud kennt keine Ausschlussliste fuer die Integritaetspruefung - jede
# fremde Datei im Installationsverzeichnis wird als EXTRA_FILE gemeldet.
# Panels legen dort aber regelmaessig eigene Verzeichnisse an (ISPConfig:
# stats/ mit AWStats-Ausgaben, error/, webdav/), ebenso NAS-Systeme (@eaDir,
# #recycle) und Editoren (.swp).
#
# Der Filter gilt AUSSCHLIESSLICH fuer EXTRA_FILE. Ein geaenderter Hash
# (INVALID_HASH) oder eine fehlende Datei bleibt immer kritisch - genau das
# ist der Zweck der Pruefung, und dort darf nichts ausgeblendet werden.
NC_INTEGRITY_IGNORE="${NC_INTEGRITY_IGNORE:-^(stats|awstats|webalizer|error|webdav|cgi-bin)/|^@eaDir/|^#recycle/|\.swp$|^\.well-known/}"

# Setup-Checks aus /settings/admin/overview ("Sicherheits- & Einrichtungs-
# warnungen"). Gibt es ab Nextcloud 28 als "occ setupchecks".
NC_SETUPCHECKS="${NC_SETUPCHECKS:-1}"
SETUPCHECK_TIMEOUT="${SETUPCHECK_TIMEOUT:-180}"

# Checks, die nicht bewertet werden: erweiterte regulaere Ausdruecke, mit "|"
# getrennt, gegen den Checknamen. Der Log-Teil der Admin-Uebersicht ist
# ausdruecklich ausgenommen - dort stehen Anwendungsfehler, die nichts ueber
# die Serverkonfiguration aussagen und im Monitoring nur Rauschen erzeugen.
NC_SETUPCHECK_IGNORE="${NC_SETUPCHECK_IGNORE:-[Ll]og|LogLevel|Errors in}"

# Status fuer Befunde der Stufe "error". Default 1 (WARN): gemeldet werden soll,
# dass Einrichtungswarnungen offen sind - der Dienst selbst laeuft ja. Auf 2
# setzen, wenn "error" als CRIT gelten soll.
NC_SETUPCHECK_ERROR_STATE="${NC_SETUPCHECK_ERROR_STATE:-1}"

# Wartungsmodus und ausstehendes DB-Upgrade als WARN melden. Wer regelmaessig
# in Wartungsfenstern arbeitet, setzt das auf 0.
NC_WARN_MAINTENANCE="${NC_WARN_MAINTENANCE:-1}"

# Zusaetzliche Verzeichnisse, die immer mitgeprueft werden (Leerzeichen-getrennt)
EXTRA_ROOTS="${EXTRA_ROOTS:-}"

# Caddy Admin-API (Live-Config). Bei abweichendem Listen-Endpoint anpassen.
CADDY_ADMIN="${CADDY_ADMIN:-localhost:2019}"

########################### Helfer #########################################

have() { command -v "$1" >/dev/null 2>&1; }

# emit <state> <servicename> <perfdata|-> <text>
emit() {
  local state="$1" name="$2" perf="${3:--}" text="$4"
  name="${name//\"/}"; name="${name//$'\t'/ }"
  text="${text//$'\n'/ }"; text="${text//$'\t'/ }"
  printf '%s "%s" %s %s\n' "$state" "$name" "$perf" "$text"
}

# Installationshinweis passend zum Paketsystem dieses Hosts.
pkg_hint() {  # <debian-paket> [rpm-paket] [suse-paket]
  local deb="$1" rpm="${2:-$1}" suse="${3:-${2:-$1}}" h
  if   have apt-get; then h="apt install $deb"
  elif have dnf;     then h="dnf install $rpm"
  elif have yum;     then h="yum install $rpm"
  elif have zypper;  then h="zypper install $suse"
  elif have apk;     then h="apk add $deb"
  else h="Paket $deb installieren"
  fi
  # Im Container (container-apps) ist eine Installation beim naechsten
  # Neuaufbau weg - der Hinweis muss aufs Image zielen.
  [[ -n "${CMK_IN_CONTAINER:-}" ]] && h="im Container-Image ergaenzen ($h) bzw. ein Image mit diesem Werkzeug verwenden"
  printf '%s' "$h"
}

# Grund zu einem Exit-Code von timeout/Shell.
rc_reason() {  # <rc> [timeout-sekunden]
  case "$1" in
    124|137) printf 'Zeitlimit %ss ueberschritten' "${2:-$CMD_TIMEOUT}" ;;
    126)     printf 'nicht ausfuehrbar' ;;
    127)     printf 'Programm nicht gefunden' ;;
    *)       printf 'Exit %s' "$1" ;;
  esac
}

# Aussagekraeftigste Zeile einer Werkzeugausgabe (Fehlerzeile bevorzugt).
first_err() {  # <text>
  local t l
  t="$(printf '%s\n' "$1" | sed -E 's/\x1b\[[0-9;]*[A-Za-z]//g; s/^[[:space:]]+//; s/[[:space:]]+$//' | grep -v '^$')"
  l="$(grep -m1 -iE 'error|fatal|denied|refused|not found|no such|failed|unable|cannot|could not|timed? ?out' <<< "$t")"
  [[ -n "$l" ]] || l="$(head -n1 <<< "$t")"
  printf '%s' "${l:0:200}"
}

# Zugangsdaten aus Fehlermeldungen entfernen, bevor sie im Monitoring landen:
# DSN mit Passwort (pgsql://user:pw@host), password=..., 'dbpassword' => '...'.
# "using password: YES" bleibt stehen - das ist nur die Angabe, OB eins kam.
mask_secrets() {  # <text>
  sed -E "s#(://[^:/@[:space:]]+:)[^@[:space:]]+@#\1***@#g
          s/((pass(word|wd)?|pwd|secret|token)['\"]?[[:space:]]*(=>|=|,)[[:space:]]*['\"]?)[^'\"[:space:],;)]+/\1***/Ig" <<< "$1"
}

# PHP-Paketname fuer eine Extension (Debian versioniert: php8.3-zip).
php_ext_pkg() {  # <extension> <php-binary>
  local ext="${1,,}" v deb rpm suse
  v="$(grep -oE '[0-9]+\.[0-9]+$' <<< "$2")"
  case "$ext" in
    mysql|mysqli|mysqlnd|pdo_mysql) deb=mysql;  rpm=mysqlnd;     suse=mysql ;;
    pgsql|pdo_pgsql)                deb=pgsql;  rpm=pgsql;       suse=pgsql ;;
    posix|pcntl)                    deb=common; rpm=process;     suse="$ext" ;;
    apcu)                           deb=apcu;   rpm=pecl-apcu;   suse=APCu ;;
    redis)                          deb=redis;  rpm=pecl-redis6; suse=redis ;;
    imagick)                        deb=imagick; rpm=pecl-imagick-im7; suse=imagick ;;
    dom|simplexml|xmlreader|xmlwriter|xml) deb=xml; rpm=xml;     suse="$ext" ;;
    *)                              deb="$ext"; rpm="$ext";      suse="$ext" ;;
  esac
  pkg_hint "php${v:-}-$deb" "php-$rpm" "php8-$suse"
}

# Flaches JSON: json_val <key> < input
json_val() {
  grep -oE "\"$1\"[[:space:]]*:[[:space:]]*(\"[^\"]*\"|[^,}]*)" \
    | head -n1 | sed -E 's/^"[^"]*"[[:space:]]*:[[:space:]]*//; s/^"//; s/"$//' \
    | tr -d '[:space:]'
}

########################### Webroot-Discovery ##############################

# Identisch zum WordPress-Plugin: ausschliesslich die Config-Dump-Funktionen
# der Webserver, kein Grep ueber feste Verzeichnisse. Damit werden auch Panels
# wie CloudPanel/ISPConfig erfasst - und nur aktive Configs gelesen.

apache_bin() {
  local b
  for b in apache2ctl apachectl httpd; do
    have "$b" && { printf '%s\n' "$b"; return 0; }
  done
  return 1
}

apache_active_configs() {
  local bin="$1"
  timeout "$CMD_TIMEOUT" "$bin" -t -D DUMP_INCLUDES </dev/null 2>/dev/null \
    | sed -nE 's/^[[:space:]]*\([^)]*\)[[:space:]]+(\/.*)$/\1/p'
  timeout "$CMD_TIMEOUT" "$bin" -S </dev/null 2>/dev/null \
    | grep -oE '\(/[^:()]+:[0-9]+\)' \
    | sed -E 's/^\(//; s/:[0-9]+\)$//'
}

apache_roots() {
  local bin f
  bin="$(apache_bin)" || return 0
  apache_active_configs "$bin" | sort -u | while read -r f; do
    [[ -f "$f" ]] || continue
    grep -hiE '^[[:space:]]*DocumentRoot[[:space:]]+' "$f" 2>/dev/null
  done | sed -E 's/^[[:space:]]*[Dd]ocument[Rr]oot[[:space:]]+//; s/"//g; s/[[:space:]]*$//'
}

nginx_roots() {
  have nginx || return 0
  timeout "$CMD_TIMEOUT" nginx -T </dev/null 2>/dev/null | awk '
    { sub(/#.*/, "") }
    {
      for (i = 1; i < NF; i++) {
        if ($i == "root" || $i == "alias") {
          v = $(i+1); gsub(/[;"{}]/, "", v)
          if (v ~ /^\//) print v
        }
      }
    }
  '
}

caddy_roots() {
  have caddy || return 0
  local json="" cf
  if have curl; then
    json="$(curl -s --max-time 5 "http://${CADDY_ADMIN}/config/" 2>/dev/null)"
  fi
  if [[ -z "$json" || "$json" == "null" ]]; then
    for cf in /etc/caddy/Caddyfile /usr/local/etc/caddy/Caddyfile; do
      [[ -f "$cf" ]] || continue
      json="$(timeout "$CMD_TIMEOUT" caddy adapt --config "$cf" --adapter caddyfile </dev/null 2>/dev/null)"
      break
    done
  fi
  [[ -n "$json" ]] || return 0
  if have jq; then
    printf '%s' "$json" | jq -r '.. | objects | select(has("root")) | .root' 2>/dev/null
  else
    printf '%s' "$json" \
      | grep -oE '"root"[[:space:]]*:[[:space:]]*"[^"]+"' \
      | sed -E 's/.*"root"[[:space:]]*:[[:space:]]*"//; s/"$//'
  fi
}

prune_roots() {
  local -a in=() out=()
  local r p keep
  mapfile -t in
  for r in "${in[@]}"; do
    r="${r%/}"
    [[ -n "$r" && "$r" != "/" && -d "$r" ]] || continue
    keep=1
    for p in "${in[@]}"; do
      p="${p%/}"
      [[ -z "$p" || "$p" == "$r" ]] && continue
      if [[ "$r" == "$p"/* ]]; then keep=0; break; fi
    done
    (( keep )) && out+=("$r")
  done
  (( ${#out[@]} )) && printf '%s\n' "${out[@]}" | sort -u
  return 0
}

discover_webroots() {
  # tr -d '\r': CloudPanel & Co. schreiben Vhosts teilweise mit CRLF. awk/sed
  # zaehlen \r nicht als Whitespace, es haengt sonst am Pfadende.
  { apache_roots; nginx_roots; caddy_roots; printf '%s\n' $EXTRA_ROOTS; } \
    | tr -d '\r' \
    | sed '/^$/d' \
    | sort -u \
    | prune_roots
}

# Marker: eine Datei "occ" plus ein version.php mit $vendor = 'nextcloud'.
# Das grenzt sauber gegen ownCloud ab, das dieselbe Struktur nutzt, aber ein
# anderes occ mit abweichenden Subkommandos hat.
is_nextcloud() {
  local d="$1"
  [[ -f "$d/occ" && -f "$d/version.php" && -d "$d/lib" ]] || return 1
  grep -qi "'nextcloud'" "$d/version.php" 2>/dev/null
}

find_nc_instances() {
  local root f d
  discover_webroots | while read -r root; do
    find "$root" -maxdepth "$SEARCH_DEPTH" -type f -name occ -print 2>/dev/null
  done | while read -r f; do
    d="$(dirname "$f")"
    is_nextcloud "$d" && printf '%s\n' "$d"
  done | sort -u
}

########################### debug mode #####################################

if [[ "${1:-}" == "--roots" ]]; then
  echo "== Webroots =="
  discover_webroots | cat -A | sed 's/\$$//'
  echo
  echo "== Nextcloud-Instanzen =="
  find_nc_instances
  exit 0
fi

########################### detect mode ####################################

if [[ "${1:-}" == "--detect" ]]; then
  have apache2ctl || have apachectl || have httpd || have nginx || have caddy || exit 1
  have php || exit 1
  [[ -n "$(find_nc_instances | head -n1)" ]] && exit 0
  exit 1
fi

########################### occ ############################################

# Panels betreiben mehrere PHP-Versionen parallel. occ mit der falschen CLI-
# Version bricht mit "This version of Nextcloud requires PHP >= x" ab, deshalb
# wenn moeglich die Version nehmen, unter der die Instanz auch im Web laeuft.
nc_php_bin() {  # $1 = fpm-version ("8.3") oder leer
  local v="$1"
  [[ -n "$v" ]] && have "php$v" && { printf 'php%s\n' "$v"; return 0; }
  [[ -n "$v" && -x "/usr/bin/php$v" ]] && { printf '/usr/bin/php%s\n' "$v"; return 0; }
  printf 'php\n'
}

# occ verweigert den Dienst als root ("Console has to be executed with the user
# that owns the file config/config.php"), daher immer im Owner-Kontext.
# </dev/null: im Job-Pool erben die Kinder sonst den stdin des Agenten.
# -k 10: ignoriert php das TERM (haengender DB-Connect), folgt KILL.
# Exit 124/137 = Timeout.
# stderr geht an den Aufrufer (nc_exec faengt es fuer die Diagnose ein).
nc_run() {  # php owner path <args...>
  local php="$1" owner="$2" path="$3"; shift 3
  local home
  if [[ "$(id -u)" != "0" ]]; then
    timeout -k 10 "$OCC_TIMEOUT" "$php" "$path/occ" \
      --no-ansi --no-interaction --no-warnings "$@" </dev/null
  else
    home="$(getent passwd "$owner" | cut -d: -f6)"
    [[ -d "${home:-}" ]] || home="/tmp"
    # runuser als Rueckfall: minimale Container-Images haben oft kein sudo.
    if have sudo; then
      timeout -k 10 "$OCC_TIMEOUT" sudo -n -u "$owner" env HOME="$home" \
        "$php" "$path/occ" --no-ansi --no-interaction --no-warnings "$@" </dev/null
    elif have runuser; then
      timeout -k 10 "$OCC_TIMEOUT" runuser -u "$owner" -- env HOME="$home" \
        "$php" "$path/occ" --no-ansi --no-interaction --no-warnings "$@" </dev/null
    else
      echo "cmk: weder sudo noch runuser vorhanden" >&2
      return 127
    fi
  fi
}

# Fuehrt occ aus und haelt stdout, stderr und Exit-Code getrennt fest:
#   NC_OUT = stdout, NC_ERR = stderr, NC_RC = Exit-Code
# Direkt aufrufen, NICHT in $(...): die Ergebnisse stehen in globalen
# Variablen und waeren in einer Subshell verloren.
NC_OUT=""; NC_ERR=""; NC_RC=0
nc_exec() {  # php owner path <args...>
  local ef
  if ! ef="$(mktemp)"; then
    NC_OUT=""; NC_ERR="mktemp fehlgeschlagen (/tmp voll oder nicht schreibbar?)"; NC_RC=1
    return 1
  fi
  NC_OUT="$(nc_run "$@" 2>"$ef")"; NC_RC=$?
  NC_ERR="$(cat "$ef" 2>/dev/null)"; rm -f "$ef"
  return "$NC_RC"
}

# Bekannte occ-/PHP-Fehler -> "<Grund> - Abhilfe: <Schritt>" (eine Zeile).
# Ausgewertet wird stderr UND stdout: je nach Nextcloud-Version schreibt occ
# "maintenance mode", "require upgrade" & Co. mal hierhin, mal dorthin.
nc_diag() {  # <rc> <php> <owner> <path> <occ-kommando> [timeout]
  local rc="$1" php="$2" owner="$3" path="$4" cmd="$5" to="${6:-$OCC_TIMEOUT}"
  local t e r occ m
  # Rahmenzeilen der Exception-Ausgabe tragen keine Information
  t="$(printf '%s\n%s\n' "$NC_ERR" "$NC_OUT" \
       | grep -vE '^[[:space:]]*(An unhandled exception has been thrown:|Stack trace:|#[0-9]+ |In [A-Za-z0-9_.]+ line [0-9]+:)' \
       | sed '/^[[:space:]]*$/d')"
  e="$(mask_secrets "$(first_err "$t")")"
  if [[ "$(id -u)" == "0" && "$owner" != "root" ]]; then
    occ="sudo -u $owner $php $path/occ"
  else
    occ="$php $path/occ"
  fi

  if (( rc == 124 || rc == 137 )); then
    r="$(rc_reason "$rc" "$to")${e:+, zuletzt: $e} - Abhilfe: haengende DB-/Redis-/HTTP-Verbindung pruefen ($occ $cmd -vvv) oder OCC_TIMEOUT erhoehen"
  elif grep -q 'weder sudo noch runuser' <<< "$t"; then
    r="weder sudo noch runuser vorhanden, Wechsel zu $owner unmoeglich - Abhilfe: $(pkg_hint sudo)"
  elif grep -qiE 'a password is required|a terminal is required|not in the sudoers|is not allowed to (execute|run)' <<< "$t"; then
    r="sudo verweigert den Wechsel zu $owner ($e) - Abhilfe: Plugin als root ausfuehren (Push-Agent-Dienst), sudoers-Einschraenkungen pruefen"
  elif grep -qiE 'unknown user|user .* does not exist|no passwd entry' <<< "$t"; then
    e="$(grep -m1 -iE 'unknown user|user .* does not exist|no passwd entry' <<< "$t")"
    r="Owner $owner von config/config.php ist kein gueltiger Benutzer ($e) - Abhilfe: chown auf den Webserver-/Site-Benutzer"
  elif grep -qiE 'has to be executed with the user that owns the file config/config.php' <<< "$t"; then
    m="$(grep -m1 -oiE 'Current user( id)?: *[^ ]+' <<< "$t")"
    r="occ laeuft nicht als Owner von config/config.php (${m:-falscher Benutzer}, Owner: $owner) - Abhilfe: Plugin als root ausfuehren, damit es per sudo -u $owner wechselt; Owner pruefen: stat -c %U $path/config/config.php"
  elif grep -qiE 'maintenance mode' <<< "$t"; then
    r="Nextcloud ist im Wartungsmodus - Abhilfe: falls kein Update/Backup laeuft: $occ maintenance:mode --off"
  elif grep -qiE 'require upgrade|requires? an upgrade|occ upgrade command|needs? (a|an )?upgrade' <<< "$t"; then
    r="Nextcloud oder eine App wartet auf das Upgrade - Abhilfe: $occ upgrade (vorher Backup)"
  elif grep -qiE 'Nextcloud is not installed' <<< "$t"; then
    r="Nextcloud ist nicht installiert (config.php ohne installed => true) - Abhilfe: Installation abschliessen ($occ maintenance:install) oder Verzeichnis aus dem Webroot entfernen"
  elif grep -qiE 'APCu not available|Memcache .* not available' <<< "$t"; then
    e="$(grep -m1 -oiE 'Memcache [^ ]+ not available[^(]*' <<< "$t")"
    r="Memcache fuer die CLI nicht verfuegbar ($e) - Abhilfe: apc.enable_cli=1 in der CLI-php.ini ($php --ini) bzw. $(php_ext_pkg apcu "$php")"
  elif grep -qiE 'RedisException|Redis server went away|redis.*(refused|timed out|Permission denied|NOAUTH|WRONGPASS)' <<< "$t"; then
    e="$(mask_secrets "$(grep -m1 -iE 'redis' <<< "$t" | cut -c1-160)")"
    r="Redis nicht erreichbar ($e) - Abhilfe: Redis-Dienst pruefen (systemctl status redis-server bzw. redis), 'redis'-Block (host/port/password) in $path/config/config.php und Socket-Rechte fuer $owner pruefen"
  elif grep -qiE 'Failed to connect to the database|SQLSTATE|could not connect to server|Access denied for user|Connection refused.*(3306|5432)|Can.t connect to (local )?(MySQL|MariaDB)|database is locked' <<< "$t"; then
    e="$(grep -m1 -oiE "Access denied for user[^)]*\)|SQLSTATE\[[^]]*\].*|could not connect to server.*|Can.t connect to [^(]*|database is locked" <<< "$t" \
         | sed -E 's/ in \/[^ ]+\.php(:[0-9]+| on line [0-9]+)?.*$//')"
    r="Datenbank nicht erreichbar${e:+ ($(mask_secrets "${e:0:160}"))} - Abhilfe: DB-Dienst pruefen (systemctl status mariadb/postgresql), dbhost/dbuser/dbpassword in $path/config/config.php pruefen"
  elif grep -qiE 'Allowed memory size of [0-9]+ bytes exhausted' <<< "$t"; then
    r="PHP-Speicherlimit erschoepft ($(grep -m1 -oE 'Allowed memory size of [0-9]+ bytes' <<< "$t")) - Abhilfe: memory_limit in der CLI-php.ini auf mind. 512M erhoehen ($php --ini zeigt die Datei)"
  elif grep -qiE 'requires? (at least )?PHP|not compatible with (this version of )?PHP|PHP [0-9.]+ (is|are) (not supported|no longer supported)|This version of Nextcloud' <<< "$t"; then
    r="PHP-Version von $php passt nicht zu Nextcloud ($e) - Abhilfe: passende PHP-CLI installieren; das Plugin nutzt die PHP-Version des FPM-Pools der Instanz"
  elif m="$(grep -m1 -oiE "PHP module [A-Za-z0-9_]+ not installed|The (PHP )?[A-Za-z0-9_]+ extension(s)? (is|are) required|PHP extension [A-Za-z0-9_]+|Call to undefined function (posix|pcntl|mb|gz|imagecreate|curl|zip|json|simplexml|xml|intl|ctype|iconv|openssl)[a-z_]*" <<< "$t")"; [[ -n "$m" ]]; then
    case "$m" in
      *"PHP module "*)      m="${m#PHP module }"; m="${m%% *}" ;;
      *"PHP extension "*)   m="${m##* }" ;;
      *"undefined function "*)
        m="${m##*function }"
        case "$m" in mb*) m=mbstring ;; gz*) m=zlib ;; imagecreate*) m=gd ;; posix*) m=posix ;; pcntl*) m=pcntl ;; *) m="${m%%_*}" ;; esac ;;
      *) m="$(sed -E 's/^The (PHP )?//; s/ extension.*$//' <<< "$m")" ;;
    esac
    r="PHP-Modul ${m,,} fehlt fuer $php - Abhilfe: $(php_ext_pkg "$m" "$php")"
  elif grep -qiE 'apps\.nextcloud\.com|updates\.nextcloud\.com|appstore|cURL error|Could not resolve host|Connection timed out|Operation timed out|Failed to connect|SSL certificate problem' <<< "$t"; then
    r="Nextcloud-Server nicht erreichbar ($e) - Abhilfe: DNS und ausgehend 443/tcp zu updates.nextcloud.com/apps.nextcloud.com pruefen, Proxy ueber 'proxy' => 'host:port' in $path/config/config.php; offline gewollt: 'appstoreenabled' => false"
  elif m="$(grep -m1 -oiE 'Command "[^"]+" is not defined|There are no commands defined in the "[^"]+" namespace' <<< "$t")"; [[ -n "$m" ]]; then
    case "$cmd" in
      setupchecks*) r="$m (Nextcloud aelter als 28) - Abhilfe: Nextcloud aktualisieren oder NC_SETUPCHECKS=0 setzen" ;;
      update:check*) r="$m - Abhilfe: App updatenotification aktivieren: $occ app:enable updatenotification" ;;
      *) r="$m - Abhilfe: Nextcloud-Version pruefen ($occ list)" ;;
    esac
  elif grep -qiE 'Could not open input file|Permission denied|failed to open stream|Failed opening required' <<< "$t"; then
    r="Rechteproblem fuer $owner ($e) - Abhilfe: $path (occ, config/, data/) muss fuer $owner les-/schreibbar sein"
  elif (( rc == 127 )); then
    r="$php nicht gefunden - Abhilfe: $(pkg_hint php-cli php-cli php8)"
  elif (( rc == 126 )); then
    r="$php nicht ausfuehrbar - Abhilfe: Rechte pruefen (ls -l $(command -v "$php" 2>/dev/null || echo "$php"))"
  elif (( rc == 255 )) && [[ -z "$t" ]]; then
    r="PHP-Abbruch ohne Meldung (Exit 255, display_errors aus) - Abhilfe: $occ $cmd -vvv bzw. $path/data/nextcloud.log pruefen"
  elif [[ -n "$e" ]]; then
    r="$(rc_reason "$rc"): $e - Abhilfe: $occ $cmd -vvv ausfuehren, Details in $path/data/nextcloud.log"
  else
    r="$(rc_reason "$rc") ohne Meldung - Abhilfe: $occ $cmd -vvv ausfuehren"
  fi
  r="${r//$'\n'/ }"; r="${r//$'\037'/ }"
  printf '%s' "$r"
}

# occ meldet das, solange ein Upgrade aussteht oder Wartungsmodus aktiv ist -
# dann liefern update:check und integrity:check-core keine belastbaren Daten.
nc_blocked() {
  grep -qiE 'require upgrade|maintenance mode|not installed' <<< "$1"
}

########################### PHP EOL ########################################

# PHP-Version, unter der DIESE Site laeuft - FPM-Pool oder mod_php.
# Rueckgabe: "version|quelle"
#
# Fuenf Stufen, weil die Zuordnung je nach Setup woanders steht:
#   1. Pool-Datei, die den Webroot-Pfad enthaelt        (ISPConfig)
#   2. Pool-Datei, deren Pool-Name/user der Owner ist   (CloudPanel)
#   3. Socket aus dem Vhost -> Pool ueber "listen" bzw. Symlinkziel
#   4. Vhost mit eindeutiger FPM-Version im Text
#   5. Apache-Vhost-Block mit FPM-Handler               (mehrere Vhosts je Datei)
#   6. mod_php: kein FPM im Spiel, PHP laeuft im Apache-Prozess
#
# Stufe 5 zuletzt: mod_php gilt fuer den gesamten Apache. Sobald ein Vhost
# einen eigenen FPM-Handler hat, gewinnt der - deshalb erst pruefen, ob
# irgendwo eine FPM-Zuordnung existiert.
apache_modphp_version() {
  local f v
  # Debian: /etc/apache2/mods-enabled/php8.3.load mit
  #   LoadModule php_module /usr/lib/apache2/modules/libphp8.3.so
  for f in /etc/apache2/mods-enabled/php*.load /etc/httpd/conf.modules.d/*php*.conf; do
    [[ -f "$f" ]] || continue
    v="$(grep -oE 'libphp[0-9]+\.[0-9]+\.so' "$f" 2>/dev/null | grep -oE '[0-9]+\.[0-9]+' | head -n1)"
    [[ -n "$v" ]] || v="$(basename "$f" | grep -oE '[0-9]+\.[0-9]+' | head -n1)"
    [[ -n "$v" ]] && { printf '%s\n' "$v"; return 0; }
  done
  # Modul geladen, aber ohne Version im Namen (libphp7.so, libphp.so):
  # dann die Version des Moduls ueber die geladene Bibliothek bestimmen.
  for f in /usr/lib/apache2/modules/libphp*.so; do
    [[ -f "$f" ]] || continue
    v="$(basename "$f" | grep -oE '[0-9]+\.[0-9]+' | head -n1)"
    [[ -n "$v" ]] && { printf '%s\n' "$v"; return 0; }
  done
  return 1
}

# Laeuft mod_php im Apache ueberhaupt?
apache_has_modphp() {
  local b
  for b in apache2ctl apachectl httpd; do
    have "$b" || continue
    timeout "$CMD_TIMEOUT" "$b" -M </dev/null 2>/dev/null | grep -qiE '^[[:space:]]*php[0-9_]*_module' && return 0
  done
  compgen -G "/etc/apache2/mods-enabled/php*.load" >/dev/null && return 0
  return 1
}

# Socket-Pfad, den der Vhost dieser Site benutzt.
# nginx:  fastcgi_pass unix:/run/php/xyz.sock;
# apache: SetHandler "proxy:unix:/run/php/xyz.sock|fcgi://localhost"
vhost_socket_for_root() {  # $1 = pfad -> socketpfad
  local path="$1" d f sock
  for d in /etc/nginx /etc/apache2 /etc/httpd; do
    [[ -d "$d" ]] || continue
    while IFS= read -r f; do
      [[ -f "$f" ]] || continue
      sock="$(sed -E 's/^[[:space:]]*#.*//' "$f" \
              | grep -oE '(unix:)?/[^[:space:]"'"'"';|]*\.sock' \
              | sed 's/^unix://' | head -n1)"
      [[ -n "$sock" ]] && { printf '%s\n' "$sock"; return 0; }
    done < <(grep -rlF -- "$path" "$d" 2>/dev/null | head -n10)
  done
  return 1
}

# PHP-Version zu einem Socket. Zwei Wege, weil der Socket im Vhost heute oft
# generisch benannt und nur ein Symlink auf die versionierte Datei ist:
#   a) den Pool finden, dessen "listen" auf genau diesen Socket zeigt -
#      unabhaengig von jeder Namenskonvention
#   b) den Symlink aufloesen und die Version aus dem Zielpfad lesen
php_version_from_socket() {  # $1 = socketpfad -> "version|quelle"
  local sock="$1" real d f v
  real="$(readlink -f -- "$sock" 2>/dev/null)"; real="${real:-$sock}"

  for d in /etc/php/*/fpm/pool.d /etc/php*/fpm/pool.d; do
    [[ -d "$d" ]] || continue
    for f in "$d"/*.conf; do
      [[ -f "$f" ]] || continue
      awk -v s1="$sock" -v s2="$real" '
        /^[[:space:]]*listen[[:space:]]*=/ {
          v = $0
          sub(/^[[:space:]]*listen[[:space:]]*=[[:space:]]*/, "", v)
          sub(/[[:space:]]+$/, "", v)
          if (v == s1 || v == s2) found = 1
        }
        END { exit !found }' "$f" || continue
      v="$(sed -nE 's|.*/php/?([0-9]+\.[0-9]+)/fpm/.*|\1|p' <<< "$f")"
      [[ -n "$v" ]] && { printf '%s|FPM-Pool %s (via Socket)\n' "$v" "${f##*/}"; return 0; }
    done
  done

  # Symlink aufgeloest: /run/php/php8.3-fpm-web1.sock
  v="$(grep -oE 'php[0-9]+\.[0-9]+' <<< "$real" | grep -oE '[0-9]+\.[0-9]+' | head -n1)"
  if [[ -n "$v" ]]; then
    if [[ "$real" != "$sock" ]]; then
      printf '%s|Socket %s -> %s\n' "$v" "${sock##*/}" "${real##*/}"
    else
      printf '%s|Socket %s\n' "$v" "${sock##*/}"
    fi
    return 0
  fi
  return 1
}

site_php_version() {  # $1 = pfad, $2 = owner (optional)
  local path="$1" owner="${2:-}" f v d vers n

  # --- 1. Pool referenziert den Webroot direkt ---
  for d in /etc/php/*/fpm/pool.d /etc/php*/fpm/pool.d; do
    [[ -d "$d" ]] || continue
    f="$(grep -rlF -- "$path" "$d" 2>/dev/null | head -n1)"
    [[ -n "$f" ]] || continue
    v="$(sed -nE 's|.*/php/?([0-9]+\.[0-9]+)/fpm/.*|\1|p' <<< "$f")"
    [[ -n "$v" ]] && { printf '%s|FPM-Pool %s\n' "$v" "${f##*/}"; return 0; }
  done

  # --- 2. Pool gehoert dem Owner (CloudPanel: ein Pool je Site-Benutzer) ---
  if [[ -n "$owner" ]]; then
    for d in /etc/php/*/fpm/pool.d /etc/php*/fpm/pool.d; do
      [[ -d "$d" ]] || continue
      f="$(grep -rlE "^[[:space:]]*(\[${owner}\]|user[[:space:]]*=[[:space:]]*${owner})[[:space:]]*$" \
           "$d" 2>/dev/null | head -n1)"
      [[ -n "$f" ]] || continue
      v="$(sed -nE 's|.*/php/?([0-9]+\.[0-9]+)/fpm/.*|\1|p' <<< "$f")"
      [[ -n "$v" ]] && { printf '%s|FPM-Pool %s (Owner)\n' "$v" "${f##*/}"; return 0; }
    done
  fi

  # --- 3. Socket aus dem Vhost: praeziser als jede Namenssuche, weil er
  #        ueber "listen" direkt auf den Pool zeigt bzw. per Symlink auf die
  #        versionierte Datei ---
  local sock
  if sock="$(vhost_socket_for_root "$path")"; then
    v="$(php_version_from_socket "$sock")" && [[ -n "$v" ]] && { printf '%s\n' "$v"; return 0; }
  fi

  # --- 4. Vhost-Datei mit dem Webroot, wenn eindeutig ---
  for d in /etc/nginx /etc/apache2 /etc/httpd /etc/caddy; do
    [[ -d "$d" ]] || continue
    while IFS= read -r f; do
      [[ -f "$f" ]] || continue
      vers="$(sed -E 's/^[[:space:]]*#.*//' "$f" \
              | grep -oE 'php[0-9]+\.[0-9]+|/php/[0-9]+\.[0-9]+/' \
              | grep -oE '[0-9]+\.[0-9]+' | sort -u)"
      n="$(grep -c . <<< "$vers")"
      if [[ -n "$vers" ]] && (( n == 1 )); then
        printf '%s|Vhost %s\n' "$vers" "${f##*/}"; return 0
      fi
    done < <(grep -rlF -- "$path" "$d" 2>/dev/null | head -n10)
  done

  # --- 5. Apache: Block um den DocumentRoot ---
  for d in /etc/apache2 /etc/httpd; do
    [[ -d "$d" ]] || continue
    while IFS= read -r f; do
      [[ -f "$f" ]] || continue
      v="$(awk -v p="$path" '
        { sub(/^[[:space:]]*#.*/, "") }
        /<VirtualHost/ { blk=""; hit=0 }
        { blk = blk "\n" $0 }
        index($0, p) > 0 { hit=1 }
        /<\/VirtualHost>/ { if (hit) { print blk; hit=0 } blk="" }' "$f" \
        | grep -oE 'php[0-9]+\.[0-9]+|/php/[0-9]+\.[0-9]+/' \
        | grep -oE '[0-9]+\.[0-9]+' | head -n1)"
      [[ -n "$v" ]] && { printf '%s|Vhost-Block %s\n' "$v" "${f##*/}"; return 0; }
    done < <(grep -rlF -- "$path" "$d" 2>/dev/null | head -n10)
  done

  # --- 6. mod_php: PHP laeuft im Apache-Prozess, kein FPM ---
  if apache_has_modphp; then
    v="$(apache_modphp_version)" && [[ -n "$v" ]] \
      && { printf '%s|mod_php im Apache\n' "$v"; return 0; }
  fi

  return 0
}

# Alter Name, damit bestehende Aufrufe weiter funktionieren.
fpm_version_for_root() { site_php_version "$@"; }

########################### Job-Pool #######################################

# Globaler Semaphor ueber eine FIFO: jedes Token ist ein Byte. Kinder erben den
# Filedeskriptor und geben ihr Token selbst zurueck, dadurch gilt MAX_PARALLEL
# fuer den gesamten Lauf und nicht pro Instanz.
SEM_FD=""

pool_init() {
  local fifo i
  (( MAX_PARALLEL > 1 )) || return 1
  fifo="$(mktemp -u)" || return 1
  mkfifo "$fifo" 2>/dev/null || return 1
  exec {SEM_FD}<>"$fifo" || { rm -f "$fifo"; SEM_FD=""; return 1; }
  rm -f "$fifo"
  for (( i = 0; i < MAX_PARALLEL; i++ )); do printf '.' >&"$SEM_FD"; done
  return 0
}

# Das Kommando laeuft in einer eigenen Subshell: bricht es ab (z.B. "unbound
# variable" unter set -u), wird das Token trotzdem zurueckgegeben - sonst
# verklemmt der Pool - und seriell reisst es nicht das ganze Plugin mit.
pool_run() {  # <ausgabedatei> <kommando...>
  local out="$1"; shift
  if [[ -z "$SEM_FD" ]]; then
    ( "$@" ) </dev/null > "$out" 2>/dev/null
    return 0
  fi
  local tok
  IFS= read -r -n1 -u "$SEM_FD" tok
  { ( "$@" ) </dev/null > "$out" 2>/dev/null; printf '.' >&"$SEM_FD"; } &
}

# Erwartete Services je Ausgabedatei. Bleibt eine Datei leer (Kind
# abgestuerzt oder gekillt), meldet pool_fill_missing fuer jeden erwarteten
# Service UNKNOWN, statt ihn still verschwinden zu lassen.
declare -A POOL_EXPECT=()
pool_expect() {  # <ausgabedatei> <servicename...>
  local out="$1"; shift
  POOL_EXPECT["$out"]="$(printf '%s\037' "$@")"
}
pool_fill_missing() {  # nach "wait" aufrufen
  local out name
  local -a names
  for out in ${POOL_EXPECT[@]+"${!POOL_EXPECT[@]}"}; do
    [[ -s "$out" ]] && continue
    IFS=$'\037' read -r -a names <<< "${POOL_EXPECT[$out]}"
    for name in ${names[@]+"${names[@]}"}; do
      emit 3 "$name" "-" "UNKNOWN - Check lieferte keine Ausgabe (abgebrochen) - Diagnose: bash -x $0"
    done > "$out"
  done
  return 0
}

########################### Einzelchecks ###################################

# Ein occ-Aufruf liefert Version, Wartungsmodus und ausstehendes DB-Upgrade.
# Feldtrenner ist \037 (Unit Separator), NICHT Tab: Tab ist ein
# IFS-Whitespace-Zeichen, dadurch kollabieren aufeinanderfolgende Trenner und
# ein leeres Feld (z.B. nicht ermittelbare FPM-Version) verschiebt alle
# folgenden Werte nach links.
nc_bootstrap() {  # $1 = path -> "owner|php|fpm|version|maint|dbupg|id|fehler" (\037-getrennt)
  local path="$1" owner php_bin fpm_ver php_ver status ver maint dbupg id url diag=""
  owner="$(stat -c '%U' "$path/config/config.php" 2>/dev/null)"
  [[ -n "$owner" ]] || owner="$(stat -c '%U' "$path/occ" 2>/dev/null)"
  [[ -n "$owner" ]] || owner="root"

  # FPM-Version der Instanz zuerst - occ muss mit derselben Version laufen,
  # sonst bricht es bei abweichender Anforderung ab.
  fpm_ver="$(site_php_version "$path" "$owner")"   # "version|quelle"
  php_bin="$(nc_php_bin "${fpm_ver%%|*}")"
  php_ver="$(timeout 30 "$php_bin" -r 'echo PHP_VERSION;' </dev/null 2>/dev/null | tr -d '[:space:]')"

  nc_exec "$php_bin" "$owner" "$path" status --output=json
  status="$NC_OUT"
  ver="$(printf   '%s' "$status" | json_val versionstring)"
  [[ -n "$ver" ]] || diag="$(nc_diag "$NC_RC" "$php_bin" "$owner" "$path" "status")"
  maint="$(printf '%s' "$status" | json_val maintenance)"
  dbupg="$(printf '%s' "$status" | json_val needsDbUpgrade)"

  # Kein occ-Aufruf noetig: die URL steht in der config.php.
  url="$(sed -nE "s/.*'overwrite\.cli\.url'[^']*'(https?:\/\/[^']*)'.*/\\1/p" \
           "$path/config/config.php" 2>/dev/null | head -n1)"
  id="${url#*://}"; id="${id%%/*}"
  [[ -n "$id" ]] || id="$(basename "$path")"

  printf '%s\037%s\037%s\037%s\037%s\037%s\037%s\037%s\n' \
    "$owner" "$php_bin" "$fpm_ver" "$ver" "$maint" "$dbupg" "$id" "$diag"
}

# update:check liefert Server- UND App-Updates in einem Aufruf, deshalb
# erzeugt dieser eine Job beide Services.
nc_check_updates() {  # svc php owner path installed_ver
  local svc="$1" php="$2" owner="$3" path="$4" cur="$5"
  local out core_new apps acount rc

  local diag
  nc_exec "$php" "$owner" "$path" update:check; rc=$NC_RC
  out="$NC_OUT"

  if [[ -z "$out" ]]; then
    diag="$(nc_diag "$rc" "$php" "$owner" "$path" "update:check")"
    # Exit 0 ohne jede Meldung: meist ist die App updatenotification aus
    (( rc == 0 )) && [[ -z "${NC_ERR//[[:space:]]/}" ]] \
      && diag="occ update:check ohne Ausgabe - Abhilfe: App updatenotification aktivieren: occ app:enable updatenotification"
    emit 3 "$svc Core Update" "-" "UNKNOWN - occ update:check fehlgeschlagen: $diag"
    emit 3 "$svc App Updates" "-" "UNKNOWN - occ update:check fehlgeschlagen: $diag"
    return 0
  fi
  if nc_blocked "$out"$'\n'"$NC_ERR"; then
    diag="$(nc_diag "$rc" "$php" "$owner" "$path" "update:check")"
    emit 3 "$svc Core Update" "-" "UNKNOWN - occ update:check blockiert: $diag"
    emit 3 "$svc App Updates" "-" "UNKNOWN - occ update:check blockiert: $diag"
    return 0
  fi

  core_new="$(printf '%s\n' "$out" | sed -nE 's/^Nextcloud ([0-9][^ ]*) is available.*/\1/p' | head -n1)"
  # Fehlerausgabe mit Exit != 0 und ohne einen einzigen Update-Treffer ist
  # kein "aktuell", sondern nicht geprueft (Timeout, Appstore/DB-Fehler).
  if (( rc != 0 )) && [[ -z "$core_new" ]] && ! grep -q '^Update for ' <<< "$out"; then
    diag="$(nc_diag "$rc" "$php" "$owner" "$path" "update:check")"
    emit 3 "$svc Core Update" "-" "UNKNOWN - occ update:check fehlgeschlagen: $diag"
    emit 3 "$svc App Updates" "-" "UNKNOWN - occ update:check fehlgeschlagen: $diag"
    return 0
  fi
  if [[ -n "$core_new" ]]; then
    emit 1 "$svc Core Update" "core_updates=1;1;;0" \
      "WARN - Nextcloud $core_new verfuegbar (installiert: $cur)"
  else
    emit 0 "$svc Core Update" "core_updates=0;1;;0" "OK - Nextcloud $cur ist aktuell"
  fi

  apps="$(printf '%s\n' "$out" \
          | sed -nE 's/^Update for (.+) to version (.+) is available\.?$/\1 (\2)/p' \
          | paste -sd, -)"
  acount=$(printf '%s' "$apps" | awk -F, 'NF{print NF}'); acount="${acount:-0}"
  if (( acount > 0 )); then
    emit 1 "$svc App Updates" "app_updates=$acount;1;;0" \
      "WARN - $acount App-Update(s): $apps"
  else
    emit 0 "$svc App Updates" "app_updates=0;1;;0" "OK - alle Apps aktuell"
  fi
}

# Core- und (optional) App-Integritaet. Getrennte Funktionen, weil der Core-
# Teil an mehreren Stellen frueh zurueckkehrt - frueher fiel dadurch gerade
# im Gutfall (Core intakt) der App-Check samt Service weg.
nc_check_integrity() {  # svc php owner path
  local svc="$1" crc
  nc_check_core_integrity "$@"; crc=$?
  [[ "$NC_CHECK_APPS" == "1" ]] || return 0
  if (( crc != 0 )); then
    emit 3 "$svc App Integrity" "-" \
      "UNKNOWN - App-Signaturen nicht geprueft, Integritaetspruefung nicht moeglich (siehe $svc Integrity)"
    return 0
  fi
  nc_check_app_integrity "$@"
  return 0
}

# integrity:check-core gibt bei Erfolg NICHTS aus und liefert auch im
# Fehlerfall Exit-Code 0 - der Status haengt allein an der Ausgabe.
# Rueckgabe 1 = occ lief nicht verwertbar bzw. Pruefung deaktiviert.
nc_check_core_integrity() {  # svc php owner path
  local svc="$1" php="$2" owner="$3" path="$4"
  local raw parsed prc line cat file rc
  local -a hard=() extra=() ignored=()

  # --output=json wird nicht von jeder Nextcloud-Version fuer dieses Kommando
  # unterstuetzt. Erst JSON versuchen, sonst die Textausgabe parsen.
  nc_exec "$php" "$owner" "$path" integrity:check-core --output=json; rc=$NC_RC
  raw="$NC_OUT"
  # "The --output option does not exist" steht auf stderr
  if [[ "$raw$NC_ERR" == *"does not exist"* || "$raw$NC_ERR" == *"not defined"* ]] \
     || ! grep -q '{' <<< "$raw"; then
    nc_exec "$php" "$owner" "$path" integrity:check-core; rc=$NC_RC
    raw="$NC_OUT"
  fi

  # Leere Ausgabe heisst nur dann "intakt", wenn occ auch sauber gelaufen ist.
  # Timeout, fehlendes sudo oder falsches PHP liefern ebenfalls nichts.
  if (( rc == 124 || rc == 137 )); then
    emit 3 "$svc Integrity" "-" "UNKNOWN - occ integrity:check-core fehlgeschlagen: $(nc_diag "$rc" "$php" "$owner" "$path" "integrity:check-core")"
    return 1
  fi
  if (( rc != 0 )) && [[ -z "${raw//[[:space:]]/}" ]]; then
    emit 3 "$svc Integrity" "-" "UNKNOWN - occ integrity:check-core fehlgeschlagen: $(nc_diag "$rc" "$php" "$owner" "$path" "integrity:check-core")"
    return 1
  fi
  # Exception statt Pruefergebnis (DB weg, Redis weg, Modul fehlt ...): das
  # ist "nicht geprueft", nicht "Integritaet verletzt". Erkennbar daran, dass
  # keine einzige Befund-Kategorie in der Ausgabe steht.
  if (( rc != 0 )) && ! grep -qE 'INVALID_HASH|EXTRA_FILE|MISSING_FILE|EXCEPTION|"core"' <<< "$raw"; then
    emit 3 "$svc Integrity" "-" "UNKNOWN - occ integrity:check-core fehlgeschlagen: $(nc_diag "$rc" "$php" "$owner" "$path" "integrity:check-core")"
    return 1
  fi

  if grep -qi 'disabled' <<< "$raw"; then
    emit 1 "$svc Integrity" "-" \
      "WARN - Integritaetspruefung ist deaktiviert (integrity.check.disabled), Code wird nicht verifiziert"
    return 1
  fi
  if nc_blocked "$raw"$'\n'"$NC_ERR"; then
    emit 3 "$svc Integrity" "-" "UNKNOWN - occ integrity:check-core blockiert: $(nc_diag "$rc" "$php" "$owner" "$path" "integrity:check-core")"
    return 1
  fi
  # Bei intakter Installation gibt das Kommando gar nichts aus.
  if [[ -z "${raw//[[:space:]]/}" ]]; then
    emit 0 "$svc Integrity" "integrity_issues=0;;1;0 integrity_ignored=0" \
      "OK - Core-Integritaet in Ordnung"
    return 0
  fi

  # JSON: {"core":{"EXTRA_FILE":{"stats/index.html":{...}},"INVALID_HASH":{...}}}
  # Geparst mit PHP - das Binary ist ohnehin da, und nur so lassen sich
  # Kategorie und Dateiname sauber zuordnen.
  parsed="$(printf '%s' "$raw" | timeout 30 "$php" -r '
    $in = stream_get_contents(STDIN);
    // Beginn des JSON suchen: occ stellt gelegentlich Textwarnungen voran.
    // Es kann ein Objekt {...} ODER ein Array [] sein - "keine Befunde"
    // liefert genau [], und nur nach "{" zu suchen macht daraus faelschlich
    // einen Parse-Fehler.
    $p = strcspn($in, "{[");
    if ($p >= strlen($in)) { exit(1); }
    $j = json_decode(substr($in, $p), true);
    if (!is_array($j)) { exit(1); }
    foreach ($j as $scope => $cats) {
      if (!is_array($cats)) { continue; }
      foreach ($cats as $cat => $files) {
        if (!is_array($files)) { continue; }
        foreach ($files as $file => $_) {
          echo $cat, "\t", $file, "\n";
        }
      }
    }
  ' 2>/dev/null)"; prc=$?

  # Kein JSON verwertbar - Textausgabe parsen. Sie ist hierarchisch:
  #   - core                  <- Scope
  #     - EXTRA_FILE          <- Kategorie
  #       - stats/index.html  <- Datei
  #         - expected:       <- Detail
  # Die Zuordnung laeuft ueber die Einrichtungstiefe: alles, was tiefer
  # eingerueckt ist als die Kategoriezeile, ist eine Datei dieser Kategorie.
  if (( prc != 0 )); then
    parsed="$(awk '
      function indent(l,   i) { match(l, /[^ \t]/); return RSTART - 1 }
      {
        line = $0
        sub(/\r$/, "", line)
        if (line ~ /^[[:space:]]*$/) next
        ind = indent(line)
        # Detailzeilen ZUERST aussortieren - nach dem Entfernen des
        # Doppelpunkts unten waeren sie nicht mehr als solche erkennbar.
        if (line ~ /-[[:space:]]*(expected|current)[[:space:]]*:/) next
        txt = line
        sub(/^[[:space:]]*-[[:space:]]*/, "", txt)
        sub(/[[:space:]]*:[[:space:]]*$/, "", txt)

        if (txt ~ /^(INVALID_HASH|EXTRA_FILE|MISSING_FILE|EXCEPTION)$/) {
          cat = txt; catind = ind; next
        }
        if (cat != "" && ind <= catind) { cat = ""; next }
        if (cat != "" && txt != "") print cat "\t" txt
      }' <<< "$raw")"
    prc=0
    if [[ -z "${parsed//[[:space:]]/}" ]]; then
      emit 2 "$svc Integrity" "integrity_issues=1;;1;0" \
        "CRIT - Integritaetspruefung meldet Abweichungen, Ausgabe nicht auswertbar: $(tr '\n' ' ' <<< "$raw" | cut -c1-250)"
      return 0
    fi
  fi

  while IFS=$'\t' read -r cat file; do
    [[ -n "$cat" ]] || continue
    if [[ "$cat" == "EXTRA_FILE" ]] && [[ -n "$NC_INTEGRITY_IGNORE" ]] \
       && grep -qE "$NC_INTEGRITY_IGNORE" <<< "$file"; then
      ignored+=("$file"); continue
    fi
    case "$cat" in
      EXTRA_FILE) extra+=("$file") ;;
      *)          hard+=("$cat:$file") ;;
    esac
  done <<< "$parsed"

  local total=$(( ${#hard[@]} + ${#extra[@]} ))
  local perf="integrity_issues=$total;;1;0 integrity_ignored=${#ignored[@]}"
  local note=""
  (( ${#ignored[@]} > 0 )) && note=" (${#ignored[@]} EXTRA_FILE ignoriert: $(printf '%s ' "${ignored[@]:0:5}" | cut -c1-120)…)"

  if (( ${#hard[@]} > 0 )); then
    emit 2 "$svc Integrity" "$perf" \
      "CRIT - Core-Integritaet verletzt: ${#hard[@]} geaenderte/fehlende Datei(en): $(printf '%s | ' "${hard[@]:0:6}" | cut -c1-350)$note"
  elif (( ${#extra[@]} > 0 )); then
    # Zusaetzliche Dateien sind kein Codeeingriff - der Kern ist unveraendert.
    emit 1 "$svc Integrity" "$perf" \
      "WARN - ${#extra[@]} zusaetzliche Datei(en) im Installationsverzeichnis, Core selbst unveraendert: $(printf '%s ' "${extra[@]:0:8}" | cut -c1-300)$note"
  else
    emit 0 "$svc Integrity" "$perf" "OK - Core-Integritaet in Ordnung$note"
  fi
  return 0
}

# Optional: jede App einzeln. occ hat kein check-app --all.
nc_check_app_integrity() {  # svc php owner path
  local svc="$1" php="$2" owner="$3" path="$4"
  local apps app aout arc why=""
  local -a bad=() unchecked=()
  nc_exec "$php" "$owner" "$path" app:list --output=json
  apps="$(printf '%s' "$NC_OUT" | grep -oE '"[a-z0-9_]+":"[0-9]' | cut -d'"' -f2 | sort -u)"
  if [[ -z "${apps//[[:space:]]/}" ]]; then
    emit 3 "$svc App Integrity" "-" "UNKNOWN - occ app:list fehlgeschlagen, App-Signaturen nicht geprueft: $(nc_diag "$NC_RC" "$php" "$owner" "$path" "app:list")"
    return 0
  fi
  while read -r app; do
    [[ -n "$app" ]] || continue
    nc_exec "$php" "$owner" "$path" integrity:check-app "$app"; arc=$NC_RC
    aout="$NC_OUT"
    # Timeout bzw. Abbruch ohne Befund = nicht geprueft, nicht "gueltig".
    # Der erste Grund reicht - meist scheitern dann alle Apps gleich.
    # Befunde tragen eine der Kategorien; eine Exception (DB/Redis weg) nicht.
    if (( arc == 124 || arc == 137 )) || { (( arc != 0 )) && ! grep -qE 'INVALID_HASH|EXTRA_FILE|MISSING_FILE|EXCEPTION' <<< "$aout"; }; then
      unchecked+=("$app")
      [[ -n "$why" ]] || why="$(nc_diag "$arc" "$php" "$owner" "$path" "integrity:check-app $app")"
      continue
    fi
    [[ -n "${aout//[[:space:]]/}" ]] && bad+=("$app")
  done <<< "$apps"

  if (( ${#bad[@]} > 0 )); then
    emit 2 "$svc App Integrity" "app_integrity_issues=${#bad[@]};;1;0" \
      "CRIT - Signaturfehler in: ${bad[*]}"
  elif (( ${#unchecked[@]} > 0 )); then
    emit 3 "$svc App Integrity" "app_integrity_issues=0;;1;0" \
      "UNKNOWN - occ integrity:check-app fehlgeschlagen fuer ${unchecked[*]}: $why"
  else
    emit 0 "$svc App Integrity" "app_integrity_issues=0;;1;0" "OK - alle App-Signaturen gueltig"
  fi
}

# Wertet "occ setupchecks --output=json" aus. Geparst wird mit PHP statt jq:
# das Binary ist ohnehin da, und die Struktur ist verschachtelt
# (Kategorie -> Checkname -> {severity, description}).
nc_check_setup() {  # svc php owner path
  local svc="$1" php="$2" owner="$3" path="$4"
  local raw parsed prc sev name desc
  local -a errors=() warnings=() ignored=()

  if [[ "$NC_SETUPCHECKS" != "1" ]]; then
    emit 0 "$svc Setup Checks" "-" "OK - Setup-Checks deaktiviert (NC_SETUPCHECKS=0)"
    return 0
  fi

  OCC_TIMEOUT="$SETUPCHECK_TIMEOUT" nc_exec "$php" "$owner" "$path" setupchecks --output=json
  raw="$NC_OUT"

  if [[ -z "${raw//[[:space:]]/}" ]]; then
    emit 3 "$svc Setup Checks" "-" \
      "UNKNOWN - occ setupchecks fehlgeschlagen: $(nc_diag "$NC_RC" "$php" "$owner" "$path" "setupchecks" "$SETUPCHECK_TIMEOUT")"
    return 0
  fi
  if nc_blocked "$raw"$'\n'"$NC_ERR"; then
    emit 3 "$svc Setup Checks" "-" "UNKNOWN - occ setupchecks blockiert: $(nc_diag "$NC_RC" "$php" "$owner" "$path" "setupchecks")"
    return 0
  fi

  # occ stellt dem JSON gelegentlich Textwarnungen voran ("Nextcloud or one of
  # the apps require upgrade ..."). Das Abschneiden ab der ersten geschweiften
  # Klammer erledigt das PHP-Skript selbst.
  local perr; perr="$(mktemp)" || perr=/dev/null
  parsed="$(printf '%s' "$raw" | timeout 30 "$php" -r '
    $in = stream_get_contents(STDIN);
    // Beginn des JSON suchen: occ stellt gelegentlich Textwarnungen voran.
    // Es kann ein Objekt {...} ODER ein Array [] sein - "keine Befunde"
    // liefert genau [], und nur nach "{" zu suchen macht daraus faelschlich
    // einen Parse-Fehler.
    $p = strcspn($in, "{[");
    if ($p >= strlen($in)) { fwrite(STDERR, "kein JSON in der Ausgabe\n"); exit(1); }
    $j = json_decode(substr($in, $p), true);
    if (!is_array($j)) { fwrite(STDERR, "JSON-Fehler: " . json_last_error_msg() . "\n"); exit(1); }
    foreach ($j as $cat => $checks) {
      if (!is_array($checks)) { continue; }
      foreach ($checks as $name => $r) {
        $sev  = is_array($r) ? ($r["severity"] ?? "") : "";
        $desc = is_array($r) ? ($r["description"] ?? "") : "";
        $desc = trim(preg_replace("/\s+/", " ", strip_tags((string)$desc)));
        if ($sev === "" || $sev === "success" || $sev === "info") { continue; }
        echo $sev, "\t", $name, "\t", mb_substr($desc, 0, 160), "\n";
      }
    }
  ' 2>"$perr")"; prc=$?
  local pmsg; pmsg="$(first_err "$(cat "$perr" 2>/dev/null)")"
  [[ "$perr" != /dev/null ]] && rm -f "$perr"

  # Leere Ausgabe ist zweideutig: entweder keine offenen Punkte oder das
  # Parsen ist gescheitert. Ohne diese Unterscheidung meldet der Check bei
  # kaputtem PHP oder geaendertem JSON-Format stillschweigend OK.
  if (( prc != 0 )); then
    if grep -qiE 'undefined function mb_' <<< "$pmsg"; then
      pmsg="PHP-Modul mbstring fehlt fuer $php - Abhilfe: $(php_ext_pkg mbstring "$php")"
    elif grep -qiE 'undefined function json_' <<< "$pmsg"; then
      pmsg="PHP-Modul json fehlt fuer $php - Abhilfe: $(php_ext_pkg json "$php")"
    else
      pmsg="$(rc_reason "$prc" 30)${pmsg:+: $pmsg} (unerwartetes JSON-Format?) - Abhilfe: Ausgabe von occ setupchecks --output=json pruefen"
    fi
    emit 3 "$svc Setup Checks" "-" \
      "UNKNOWN - Auswertung von occ setupchecks fehlgeschlagen: $pmsg"
    return 0
  fi
  if [[ -z "${parsed//[[:space:]]/}" ]]; then
    emit 0 "$svc Setup Checks" "setup_errors=0;;1;0 setup_warnings=0;1;;0" \
      "OK - keine offenen Einrichtungs- oder Sicherheitswarnungen"
    return 0
  fi

  while IFS=$'\t' read -r sev name desc; do
    [[ -n "$sev" ]] || continue
    if [[ -n "$NC_SETUPCHECK_IGNORE" ]] && grep -qE "$NC_SETUPCHECK_IGNORE" <<< "$name"; then
      ignored+=("$name"); continue
    fi
    case "$sev" in
      error)   errors+=("$name: $desc") ;;
      warning) warnings+=("$name: $desc") ;;
    esac
  done <<< "$parsed"

  local perf="setup_errors=${#errors[@]};;1;0 setup_warnings=${#warnings[@]};1;;0"
  local note=""
  (( ${#ignored[@]} > 0 )) && note=" (${#ignored[@]} ignoriert: ${ignored[*]})"

  if (( ${#errors[@]} > 0 )); then
    local est="${NC_SETUPCHECK_ERROR_STATE:-1}" etxt=(OK WARN CRIT UNKNOWN)
    # Ungueltiger Wert waere als Array-Index unter set -u fatal.
    [[ "$est" =~ ^[0-3]$ ]] || est=1
    emit "$est" "$svc Setup Checks" "$perf" \
      "${etxt[$est]} - ${#errors[@]} Fehler, ${#warnings[@]} Warnung(en) unter /settings/admin/overview: $(printf '%s | ' "${errors[@]}" | cut -c1-500)$note"
  elif (( ${#warnings[@]} > 0 )); then
    emit 1 "$svc Setup Checks" "$perf" \
      "WARN - ${#warnings[@]} Einrichtungswarnung(en) unter /settings/admin/overview: $(printf '%s | ' "${warnings[@]}" | cut -c1-500)$note"
  else
    emit 0 "$svc Setup Checks" "$perf" \
      "OK - keine offenen Einrichtungs- oder Sicherheitswarnungen$note"
  fi
}

########################### Ausgabe sammeln ################################

collect() {
  echo "<<<local:sep(0)>>>"

  if ! have php; then
    emit 2 "Nextcloud" "-" "CRIT - occ nicht lauffaehig: kein PHP-CLI im PATH - Abhilfe: $(pkg_hint php-cli php-cli php8)"
    return 0
  fi

  local -a instances
  mapfile -t instances < <(find_nc_instances)

  emit 0 "Nextcloud Instances" "instances=${#instances[@]}" \
       "OK - ${#instances[@]} Nextcloud-Instanz(en) gefunden"
  (( ${#instances[@]} )) || return 0

  local tmpdir
  tmpdir="$(mktemp -d)" || return 0
  trap 'rm -rf "$tmpdir"' RETURN

  pool_init || SEM_FD=""

  # Phase 1: ein occ-Aufruf je Instanz, parallel.
  local i n path
  for i in "${!instances[@]}"; do
    pool_run "$(printf '%s/%03d.boot' "$tmpdir" "$i")" nc_bootstrap "${instances[$i]}"
  done
  wait

  # Zwischenschritt seriell: EOL-Datum je PHP-Version aufloesen. Muss vor dem
  # Forken passieren, sonst waere das Memo in jedem Kind leer und jede Instanz
  # wuerde endoflife.date einzeln befragen.
  local -a owner=() phpbin=() fpm=() ver=() maint=() dbupg=() id=() bootdiag=()
  local line
  for i in "${!instances[@]}"; do
    line="$(cat "$(printf '%s/%03d.boot' "$tmpdir" "$i")" 2>/dev/null)"
    IFS=$'\037' read -r owner[i] phpbin[i] fpm[i] ver[i] maint[i] dbupg[i] id[i] bootdiag[i] <<< "$line"
  done

  # Phase 2: alle uebrigen Checks aller Instanzen in einem gemeinsamen Pool.
  # Bewusst flach - ein Instanz-Pool plus innerer Check-Pool wuerde bei kleinem
  # MAX_PARALLEL blockieren, weil aeussere Jobs Tokens halten.
  local svc out st txt fver fsrc
  for i in "${!instances[@]}"; do
    path="${instances[$i]}"
    svc="Nextcloud ${id[i]:-$(basename "$path")}"

    out="$(printf '%s/%03d-00.out' "$tmpdir" "$i")"
    if [[ -z "${ver[i]:-}" ]]; then
      # Der Servicename haengt nicht an occ (id kommt aus der config.php),
      # deshalb auch die Folgeservices als UNKNOWN melden statt sie
      # verschwinden zu lassen.
      {
        emit 3 "$svc Status" "-" \
          "UNKNOWN - occ status fuer $path (Owner: ${owner[i]:-?}, PHP: ${phpbin[i]:-?}) fehlgeschlagen: ${bootdiag[i]:-keine Ausgabe (Bootstrap abgebrochen)}"
        local why="nicht geprueft, occ status fehlgeschlagen (Grund und Abhilfe: siehe $svc Status)"
        emit 3 "$svc Core Update" "-" "UNKNOWN - $why"
        emit 3 "$svc App Updates" "-" "UNKNOWN - $why"
        emit 3 "$svc Integrity" "-" "UNKNOWN - $why"
        [[ "$NC_CHECK_APPS" == "1" ]] \
          && emit 3 "$svc App Integrity" "-" "UNKNOWN - $why"
        if [[ "$NC_SETUPCHECKS" == "1" ]]; then
          emit 3 "$svc Setup Checks" "-" "UNKNOWN - $why"
        else
          emit 0 "$svc Setup Checks" "-" "OK - Setup-Checks deaktiviert (NC_SETUPCHECKS=0)"
        fi
      } > "$out"
      continue
    fi

    local occcmd="${phpbin[i]:-php} $path/occ" hint=""
    [[ "$(id -u)" == "0" && "${owner[i]:-root}" != "root" ]] && occcmd="sudo -u ${owner[i]} $occcmd"
    st=0; txt="OK - Nextcloud ${ver[i]} in $path (Owner: ${owner[i]:-?})"
    if [[ "${maint[i]:-false}" == "true" ]]; then
      txt="Wartungsmodus AKTIV - Nextcloud ${ver[i]} in $path"
      hint=" - Abhilfe: falls kein Update/Backup laeuft: $occcmd maintenance:mode --off"
      [[ "$NC_WARN_MAINTENANCE" == "1" ]] && st=1
    elif [[ "${dbupg[i]:-false}" == "true" ]]; then
      txt="DB-Upgrade steht aus - Nextcloud ${ver[i]} in $path"
      hint=" - Abhilfe: $occcmd upgrade (vorher Backup)"
      [[ "$NC_WARN_MAINTENANCE" == "1" ]] && st=1
    fi
    fver="${fpm[i]:-}"; fsrc="${fver#*|}"; fver="${fver%%|*}"
    [[ "$fsrc" == "$fver" ]] && fsrc=""
    txt="$txt (PHP ${fver:-?} via ${phpbin[i]:-php}${fsrc:+, $fsrc})$hint"
    emit "$st" "$svc Status" "-" "$txt" > "$out"

    local f
    f="$(printf '%s/%03d-01.out' "$tmpdir" "$i")"
    pool_expect "$f" "$svc Core Update" "$svc App Updates"
    pool_run "$f" nc_check_updates   "$svc" "${phpbin[i]:-php}" "${owner[i]:-root}" "$path" "${ver[i]}"
    f="$(printf '%s/%03d-03.out' "$tmpdir" "$i")"
    if [[ "$NC_CHECK_APPS" == "1" ]]; then
      pool_expect "$f" "$svc Integrity" "$svc App Integrity"
    else
      pool_expect "$f" "$svc Integrity"
    fi
    pool_run "$f" nc_check_integrity "$svc" "${phpbin[i]:-php}" "${owner[i]:-root}" "$path"
    f="$(printf '%s/%03d-04.out' "$tmpdir" "$i")"; pool_expect "$f" "$svc Setup Checks"
    pool_run "$f" nc_check_setup     "$svc" "${phpbin[i]:-php}" "${owner[i]:-root}" "$path"

    # Die PHP-Version wird nicht mehr hier bewertet - dafuer gibt es ein
    # eigenes Plugin (php-versions).
  done
  wait
  pool_fill_missing

  for n in "$tmpdir"/*.out; do
    [[ -s "$n" ]] && cat "$n"
  done
  return 0
}

########################### Main ###########################################

collect

exit 0