#!/usr/bin/env sh
# generate-status-page.sh
# Fragt VictoriaMetrics ab und erzeugt eine statische HTML-Statusseite.
# Läuft in einem Alpine-Container im Docker-Netz (http://victoriametrics:9090)
# oder auf dem Host (http://localhost:9090).
#
# Ausgabe: /output/index.html (gemountet als /Volumes/ManaData/landings/status/)
set -eu
VM_URL="${VICTORIAMETRICS_URL:-http://victoriametrics:9090}"
OUTPUT="${OUTPUT_FILE:-/output/index.html}"
TMPDIR_LOCAL="$(mktemp -d)"
trap 'rm -rf "$TMPDIR_LOCAL"' EXIT
# ── Daten aus VictoriaMetrics holen ────────────────────────────────────────
fetch_metric() {
curl -sf --max-time 10 \
"${VM_URL}/api/v1/query?query=$(printf '%s' "$1" | sed 's/ /%20/g;s/{/%7B/g;s/}/%7D/g;s/=~/%3D~/g;s/|/%7C/g;s/"/%22/g')" \
2>/dev/null || echo '{"status":"error","data":{"result":[]}}'
}
SUCCESS_JSON="$(fetch_metric 'probe_success{job=~"blackbox-web|blackbox-api|blackbox-infra|blackbox-gpu"}')"
DURATION_JSON="$(fetch_metric 'probe_duration_seconds{job=~"blackbox-web|blackbox-api|blackbox-infra|blackbox-gpu"}')"
# ── Hilfsfunktionen ─────────────────────────────────────────────────────────
# Gibt den probe_success-Wert für eine Instanz zurück (0 oder 1)
get_success() {
instance="$1"
echo "$SUCCESS_JSON" | jq -r --arg inst "$instance" \
'.data.result[] | select(.metric.instance == $inst) | .value[1]' 2>/dev/null || echo "0"
}
# Gibt die Antwortzeit in ms zurück
get_duration_ms() {
instance="$1"
val=$(echo "$DURATION_JSON" | jq -r --arg inst "$instance" \
'.data.result[] | select(.metric.instance == $inst) | .value[1]' 2>/dev/null || echo "")
if [ -n "$val" ] && [ "$val" != "null" ]; then
printf "%.0f" "$(echo "$val * 1000" | awk '{printf "%.1f", $1}')"
else
echo ""
fi
}
# Alle Instanzen einer Job-Gruppe, sortiert
get_instances() {
job="$1"
echo "$SUCCESS_JSON" | jq -r --arg job "$job" \
'.data.result[] | select(.metric.job == $job) | .metric.instance' 2>/dev/null | sort
}
# Freundlicher Name aus URL
friendly_name() {
url="$1"
# Entferne https:// und .mana.how
name="${url#https://}"
name="${name%.mana.how}"
# Entferne /health suffix
name="${name%/health}"
# Erster Buchstabe groß (POSIX-kompatibel)
printf '%s' "$name" | awk '{print toupper(substr($0,1,1)) substr($0,2)}'
}
# Zählt UP-Dienste einer Job-Gruppe
count_up() {
job="$1"
echo "$SUCCESS_JSON" | jq -r --arg job "$job" \
'[.data.result[] | select(.metric.job == $job) | .value[1]] | map(tonumber) | add // 0' \
2>/dev/null || echo "0"
}
count_total() {
job="$1"
echo "$SUCCESS_JSON" | jq -r --arg job "$job" \
'[.data.result[] | select(.metric.job == $job)] | length' \
2>/dev/null || echo "0"
}
# ── Service-Rows HTML ────────────────────────────────────────────────────────
render_rows() {
job="$1"
instances="$(get_instances "$job")"
if [ -z "$instances" ]; then
printf '
| Noch keine Daten — Blackbox Exporter lädt… |
\n'
return
fi
echo "$instances" | while IFS= read -r inst; do
[ -z "$inst" ] && continue
success="$(get_success "$inst")"
ms="$(get_duration_ms "$inst")"
name="$(friendly_name "$inst")"
if [ "$success" = "1" ]; then
status_class="up"
status_text="UP"
ms_html="${ms:+${ms}ms}"
else
status_class="down"
status_text="DOWN"
ms_html=""
fi
printf ' | %s%s | %s %s |
\n' \
"$status_class" "$status_class" "$name" "$inst" \
"$status_text" \
"${ms_html:-}"
done
}
# ── Gesamtstatus ─────────────────────────────────────────────────────────────
web_up="$(count_up blackbox-web)"; web_total="$(count_total blackbox-web)"
api_up="$(count_up blackbox-api)"; api_total="$(count_total blackbox-api)"
infra_up="$(count_up blackbox-infra)"; infra_total="$(count_total blackbox-infra)"
gpu_up="$(count_up blackbox-gpu)"; gpu_total="$(count_total blackbox-gpu)"
total_up=$(( web_up + api_up + infra_up + gpu_up ))
total_all=$(( web_total + api_total + infra_total + gpu_total ))
total_down=$(( total_all - total_up ))
if [ "$total_down" -eq 0 ] && [ "$total_all" -gt 0 ]; then
overall_class="all-good"
overall_icon="✓"
overall_text="Alle Systeme operational"
elif [ "$total_up" -gt $(( total_all / 2 )) ]; then
overall_class="partial"
overall_icon="⚠"
overall_text="Teilweise Beeinträchtigungen (${total_down} Dienste down)"
else
overall_class="outage"
overall_icon="✕"
overall_text="Größerer Ausfall (${total_down} von ${total_all} Diensten down)"
fi
TIMESTAMP="$(date -u '+%d. %B %Y, %H:%M Uhr UTC')"
# ── HTML generieren ──────────────────────────────────────────────────────────
cat > "${OUTPUT}.tmp" << HTMLEOF
ManaCore Status
${overall_icon}
${overall_text}
Zuletzt aktualisiert: ${TIMESTAMP} · Auto-Refresh alle 60 s
${web_up}/${web_total}
Web Apps
${api_up}/${api_total}
API Backends
${infra_up}/${infra_total}
Infrastruktur
${gpu_up}/${gpu_total}
GPU Dienste
$(render_rows blackbox-web)
$(render_rows blackbox-api)
$(render_rows blackbox-infra)
$(render_rows blackbox-gpu)
HTMLEOF
mv "${OUTPUT}.tmp" "$OUTPUT"
echo "$(date '+%H:%M:%S') Status-Seite generiert → $OUTPUT (${total_up}/${total_all} online)"
# ── status.json für ManaScore Live-Badge ─────────────────────────────────────
JSON_OUTPUT="$(dirname "$OUTPUT")/status.json"
TIMESTAMP_ISO="$(date -u '+%Y-%m-%dT%H:%M:%SZ')"
echo "$SUCCESS_JSON" | jq \
--arg ts "$TIMESTAMP_ISO" \
--argjson total_up "$total_up" \
--argjson total_all "$total_all" \
'{
updated: $ts,
summary: { up: $total_up, total: $total_all },
services: (
.data.result | map({
key: (
.metric.instance
| ltrimstr("https://")
| rtrimstr("/health")
| rtrimstr("/")
| if . == "mana.how" then "manacore"
else rtrimstr(".mana.how")
end
),
value: (.value[1] == "1")
}) | from_entries
)
}' > "${JSON_OUTPUT}.tmp" && mv "${JSON_OUTPUT}.tmp" "$JSON_OUTPUT"
echo "$(date '+%H:%M:%S') status.json generiert → $JSON_OUTPUT"