my-homelab-configs/scripts/status-focus

214 lines
12 KiB
Bash

#!/usr/bin/env bash
# Fast inventory-scoped status checks. Sourced by Jeannie after report-ui.
status_focus_ssh() {
local host="$1" user="$2" key="$3"
if [[ -z "${host}" || -z "${user}" ]]; then
echo "Host or user is missing from the inventory." >&2
return 1
fi
ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes \
"${user}@${host}" true
}
status_focus_edge_peer() {
local edge_ip="${LAB_EDGE_TAILSCALE_IP:-}"
if [[ -z "${edge_ip}" ]]; then
echo "OCI edge Tailscale IP is not configured." >&2
return 1
fi
if ! command -v tailscale >/dev/null 2>&1; then
echo "Tailscale client is unavailable." >&2
return 1
fi
timeout 4 tailscale status --json | python3 -c '
import json, sys
try:
status = json.load(sys.stdin)
if status.get("BackendState") != "Running":
raise ValueError("Debian Tailscale is not running")
peer = next((p for p in (status.get("Peer") or {}).values() if sys.argv[1] in (p.get("TailscaleIPs") or [])), None)
if peer is None:
raise ValueError("OCI edge peer is not visible in the tailnet")
if not peer.get("Online"):
raise ValueError("OCI edge peer is reported offline")
print("OCI edge peer is online")
except (ValueError, TypeError, json.JSONDecodeError) as exc:
print(str(exc), file=sys.stderr)
sys.exit(1)
' "${edge_ip}"
}
status_focus_http_ok() {
local code
code="$(curl -k -sS -o /dev/null -w '%{http_code}' --connect-timeout 2 --max-time 4 "$1")" || return 1
[[ "${code}" =~ ^[23][0-9][0-9]$ ]] || { echo "HTTP ${code}" >&2; return 1; }
echo "HTTP ${code}"
}
status_focus_dns() {
local host="${LAB_RPI_HOST:-}" port="${LAB_PIHOLE_DNS_PORT:-53}"
[[ -n "${host}" && "${port}" =~ ^[0-9]+$ ]] && (( 10#${port} >= 1 && 10#${port} <= 65535 )) || {
echo "Pi-hole host or DNS port is invalid in the inventory." >&2; return 1;
}
if command -v dig >/dev/null 2>&1; then
local answer
answer="$(dig +time=2 +tries=1 +short -p "${port}" @"${host}" example.com A)" || return 1
[[ -n "${answer}" ]] || { echo "DNS returned no answer." >&2; return 1; }
elif command -v nslookup >/dev/null 2>&1; then
nslookup -timeout=2 -retry=1 -port="${port}" example.com "${host}" >/dev/null || return 1
else
echo "Neither dig nor nslookup is installed." >&2; return 1
fi
echo "DNS query answered"
}
# One snapshot per host, with one report row for each Compose service instance.
status_focus_compose_report() {
local area="$1" host="$2" user="$3" key="$4" snapshot rows severity name detail err_file
local marker='__JEANNIE_COMPOSE_SNAPSHOT_V1__'
local -a docker_cmd=(sudo -n docker ps -a --filter label=com.docker.compose.project --format '{{json .}}')
err_file="$(mktemp "${REPORT_UI_WORK_DIR:-${TMPDIR:-/tmp}}/compose-stderr.XXXXXX")" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot create diagnostic file" ""; return 0;
}
if [[ "${host}" == local ]]; then
snapshot="$(timeout 8 "${docker_cmd[@]}" 2>"${err_file}")" || {
detail="$(<"${err_file}")"; rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Cannot read Docker containers" "${detail}"; return 0;
}
else
if [[ -z "${host}" || -z "${user}" ]]; then
rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Host or user missing from inventory" ""; return 0
fi
snapshot="$(timeout 8 ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes "${user}@${host}" \
"printf '%s\\n' '${marker}'; sudo -n docker ps -a --filter label=com.docker.compose.project --format '{{json .}}'" 2>"${err_file}")" || {
detail="$(<"${err_file}")"; rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Cannot read remote Docker containers" "${detail}"; return 0;
}
if [[ "${snapshot}" != "${marker}" && "${snapshot}" != *$'\n'"${marker}" && "${snapshot}" != "${marker}"$'\n'* && "${snapshot}" != *$'\n'"${marker}"$'\n'* ]]; then
rm -f "${err_file}"
report_ui_emit warn "${area}" "Compose inventory" "Remote Docker snapshot marker missing" "Remote command returned unexpected output"; return 0
fi
snapshot="${snapshot#*"${marker}"}"
snapshot="${snapshot#$'\n'}"
fi
rm -f "${err_file}"
local -a intent_args=()
if [[ "${host}" == local ]]; then intent_args+=("${HOMELAB_STATE_DIR:-${HOME}/.local/share/homelab}/compose-intent.json"); fi
rows="$(printf '%s\n' "${snapshot}" | python3 "${REPO_ROOT}/scripts/status-app-snapshot.py" compose "${intent_args[@]}" 2>&1)" || {
report_ui_emit warn "${area}" "Compose inventory" "Cannot interpret Docker snapshot" "${rows}"; return 0;
}
while IFS=$'\t' read -r severity name detail; do
[[ -n "${severity}" ]] || continue
report_ui_emit "${severity}" "${area}" "${name}" "${detail}" "${detail}"
done <<<"${rows}"
}
status_focus_runner_expected() {
local snapshot="$1" revision head configured deployment
revision="$(printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/status-app-snapshot.py" runner-candidate)" || return 1
[[ -n "${revision}" ]] || return 1
head="$(git -C "${REPO_ROOT}" rev-parse HEAD 2>/dev/null)" || return 1
[[ "${head}" == "${revision}" ]] || return 1
configured="$(git -C "${REPO_ROOT}" show HEAD:apps/gitea-runner/deployment.yaml 2>/dev/null | awk '/^ replicas:/ && !seen {print $2; seen=1}')" || return 1
[[ "${configured}" == 0 ]] || return 1
systemctl is-active --quiet homelab-gitea-runner.service || return 1
deployment="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=8s -n gitea-runner get deployment gitea-runner -o json 2>/dev/null)" || return 1
printf '%s' "${deployment}" | python3 -c 'import json,sys; d=json.load(sys.stdin); v=d.get("spec",{}).get("replicas"); sys.exit(0 if type(v) is int and v == 0 else 1)' 2>/dev/null
}
status_focus_applications_report() {
local snapshot rows severity name detail failed=0
local -a options=()
snapshot="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=8s -n argocd get applications -o json 2>&1)" || {
report_ui_emit warn "Kubernetes apps" "Argo CD applications" "Cannot read application status" "${snapshot}"; return 0;
}
if status_focus_runner_expected "${snapshot}"; then options+=(--expected-runner); fi
rows="$(printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/status-app-snapshot.py" applications "${options[@]}" 2>&1)" || {
report_ui_emit warn "Kubernetes apps" "Argo CD applications" "Cannot interpret application status" "${rows}"; return 0;
}
while IFS=$'\t' read -r severity name detail; do
[[ -n "${severity}" ]] || continue
report_ui_emit "${severity}" "Kubernetes apps" "${name}" "${detail}" "${detail}"
[[ "${severity}" != fail ]] || failed=1
done <<<"${rows}"
return "${failed}"
}
status_focus_collect() {
local scope="$1"
local key="${HOME}/.ssh/id_ed25519"
local rpi="${LAB_RPI_HOST:-}" rpi_user="${LAB_RPI_USER:-}"
local opi="${LAB_PIMOX_HOST:-}" opi_user="${LAB_PIMOX_USER:-}"
local public_url="${LAB_PUBLIC_URL:-}"
local ollama_url="${LAB_OLLAMA_URL:-http://${LAB_DEBIAN_LAN_IP:-192.168.100.73}:11434}"
if [[ "${scope}" == all || "${scope}" == debian ]]; then
report_ui_enqueue status_focus_compose_report "Debian Compose" local "" ""
report_ui_check fail "Debian" "Docker service" "./jeannie status debian --details" "" "" check_systemd_active docker
report_ui_check warn "Debian" "Gitea runner service" "./jeannie doctor gitea-runner" "" "" check_systemd_active homelab-gitea-runner.service
report_ui_check fail "Debian" "Gitea HTTP" "./jeannie status debian --details" "" "" status_focus_http_ok "http://127.0.0.1:${LAB_GITEA_HTTP_PORT:-3000}/"
report_ui_check warn "Debian" "Dex HTTP" "./jeannie status debian --details" "" "" status_focus_http_ok "http://127.0.0.1:${LAB_DEX_HTTP_PORT:-5556}/healthz"
report_ui_check warn "Debian" "Matrix API" "./jeannie status debian --details" "" "" status_focus_http_ok "http://127.0.0.1:${LAB_MATRIX_SYNAPSE_PORT:-8008}/_matrix/client/versions"
report_ui_check warn "Debian" "Ollama API" "./jeannie status debian --details" "" "" status_focus_http_ok "${ollama_url%/}/api/tags"
report_ui_check warn "Debian" "Disk usage" "./jeannie status debian --details" "" "" status_disk_pressure
fi
if [[ "${scope}" == all || "${scope}" == rpi ]]; then
report_ui_enqueue status_focus_compose_report "RPi4 Compose" "${rpi}" "${rpi_user}" "${LAB_RPI_SSH_KEY_PATH:-${key}}"
report_ui_check warn "RPi4" "SSH access" "./jeannie status rpi --details" "" "" status_focus_ssh "${rpi}" "${rpi_user}" "${LAB_RPI_SSH_KEY_PATH:-${key}}"
report_ui_check fail "RPi4" "Pi-hole DNS resolution" "./jeannie status rpi --details" "" "" status_focus_dns
report_ui_check warn "RPi4" "Uptime Kuma HTTP" "./jeannie status rpi --details" "" "" status_focus_http_ok "http://${rpi}:${UPTIME_KUMA_PORT:-3001}/"
fi
if [[ "${scope}" == all || "${scope}" == opi5 ]]; then
report_ui_enqueue status_focus_compose_report "Orange Pi Compose" "${opi}" "${opi_user}" "${LAB_PIMOX_SSH_KEY_PATH:-${key}}"
report_ui_check warn "Orange Pi" "SSH access" "./jeannie status opi5 --details" "" "" status_focus_ssh "${opi}" "${opi_user}" "${LAB_PIMOX_SSH_KEY_PATH:-${key}}"
if [[ "${LAB_CLUSTER_RUNTIME:-}" == k3s && "${LAB_CLUSTER_CONTROL_PLANE_LAN_IP:-}" == "${opi}" && -n "${opi}" ]]; then
report_ui_check fail "Orange Pi" "K3s runtime" "./jeannie status opi5 --details" "" "" status_cluster_control_runtime
report_ui_check warn "Orange Pi" "K3s storage mount" "./jeannie status opi5 --details" "" "" status_cluster_storage
else
report_ui_emit skip "Orange Pi" "Native K3s checks" "Not selected on this host" "profile=${LAB_CLUSTER_PROFILE:-unknown} runtime=${LAB_CLUSTER_RUNTIME:-unknown}"
fi
fi
if [[ "${scope}" == all || "${scope}" == cluster ]]; then
report_ui_enqueue status_focus_applications_report
report_ui_check fail "Kubernetes" "API readiness" "./jeannie status cluster --details" "" "" status_kubernetes_api_ok
report_ui_check fail "Kubernetes" "Nodes ready" "./jeannie status cluster --details" "" "" status_kubernetes_nodes_ready
report_ui_check warn "Kubernetes" "Node pressure" "./jeannie status cluster --details" "" "" status_node_pressure
fi
if [[ "${scope}" == all || "${scope}" == oci ]]; then
report_ui_enqueue status_focus_compose_report "OCI edge Compose" "${LAB_EDGE_TAILSCALE_IP:-}" "${LAB_EDGE_USER:-}" "${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-${key}}}"
report_ui_check warn "OCI edge" "Tailscale peer visibility" "./jeannie status oci --details" "" "" status_focus_edge_peer
if [[ -n "${public_url}" ]]; then
report_ui_check fail "OCI edge" "Public edge HTTP health" "./jeannie status oci --details" "" "" status_focus_http_ok "${public_url%/}/edge-health"
else
report_ui_emit warn "OCI edge" "Public edge HTTP health" "Public URL missing from inventory" ""
fi
fi
}
status_focused_report() {
local scope="$1" rc=0 arg
shift
# These dynamically scoped variables are consumed by the report helpers.
# shellcheck disable=SC2034
local REPORT_UI_ONLY=all REPORT_UI_LOCAL_RCA=false REPORT_UI_PARALLEL=true REPORT_UI_STATUS_SUMMARY=true
# shellcheck disable=SC2034
local LAB_REPORT_PARALLELISM="${LAB_REPORT_PARALLELISM:-8}"
for arg in "$@"; do
case "${arg}" in
--json|--all|--details|--no-local-rca) ;;
*) echo "Usage: ./jeannie status ${scope} [--json|--all|--details|--no-local-rca]" >&2; return 2 ;;
esac
done
report_ui_parse_args "$@" || return 2
report_ui_begin
status_focus_collect "${scope}"
report_ui_finish
((REPORT_UI_FAILURES == 0)) || rc=1
report_ui_render "Jeannie Status: ${scope}"
report_ui_cleanup
return "${rc}"
}