196 lines
10 KiB
Bash
196 lines
10 KiB
Bash
#!/usr/bin/env bash
|
|
# Sourced by Jeannie; all probes are read-only.
|
|
|
|
doctor_focus_usage() {
|
|
cat <<'EOF'
|
|
Usage: ./jeannie doctor {oci|rpi|opi5|debian|cluster} [ITEM] [options]
|
|
./jeannie doctor ARGOCD_APPLICATION [options]
|
|
|
|
Diagnose one inventory area. Optionally select an exact check, Argo CD
|
|
application, Compose project/service, or unambiguous Compose service name
|
|
shown by status. There is no all-area doctor.
|
|
|
|
--ai Request an evidence-validated AI explanation (text only;
|
|
enabled by default when diagnosing an item).
|
|
--no-local-rca Disable the AI explanation.
|
|
--json Return the structured diagnostic report.
|
|
--details Show supporting evidence.
|
|
--all Include healthy checks (default).
|
|
--item NAME Alternative to positional ITEM.
|
|
|
|
Examples:
|
|
./jeannie doctor rpi
|
|
./jeannie doctor gitea-runner
|
|
./jeannie doctor cluster gitea-runner --ai
|
|
./jeannie remote doctor debian --item homelab-gitea/gitea
|
|
EOF
|
|
}
|
|
|
|
doctor_focus_https() {
|
|
curl -fsS -o /dev/null --connect-timeout 3 --max-time 8 "${LAB_PUBLIC_URL%/}/edge-health"
|
|
}
|
|
|
|
doctor_focus_application_fetch() {
|
|
local name="$1"
|
|
[[ "${name}" =~ ^[a-z0-9]([-a-z0-9.]*[a-z0-9])?$ ]] || { echo "Invalid application name" >&2; return 1; }
|
|
kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s -n argocd get application "${name}" -o json 2>/dev/null
|
|
}
|
|
|
|
doctor_focus_application_evidence() {
|
|
local name="$1" snapshot deployments namespace deployment state count=0
|
|
snapshot="$(report_ui_cached_probe doctor_focus_application_fetch "${name}")" || return 1
|
|
printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/doctor-application.py" || return 1
|
|
deployments="$(printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/doctor-application.py" --deployments)" || return 1
|
|
while read -r namespace deployment; do
|
|
[[ -n "${namespace}" && -n "${deployment}" ]] || continue
|
|
((count < 8)) || { echo "Additional deployments omitted"; break; }
|
|
count=$((count + 1))
|
|
printf 'deployment %s/%s: ' "${namespace}" "${deployment}"
|
|
if state="$(kubectl --kubeconfig "${KUBECONFIG_PATH}" --request-timeout=10s -n "${namespace}" get deployment "${deployment}" -o json 2>/dev/null)"; then
|
|
printf '%s' "${state}" | python3 "${REPO_ROOT}/scripts/doctor-application.py" --deployment || return 1
|
|
else
|
|
echo "unavailable"
|
|
fi
|
|
done <<<"${deployments}"
|
|
}
|
|
|
|
doctor_focus_rpi_os_report() {
|
|
local host="${LAB_RPI_HOST:-}" user="${LAB_RPI_USER:-}" key="${LAB_RPI_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}"
|
|
local snapshot rows severity name detail
|
|
[[ -n "${host}" && -n "${user}" ]] || { report_ui_emit warn "RPi4 OS" "OS snapshot" "Host or user missing from inventory" ""; return 0; }
|
|
snapshot="$(timeout 12 ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
|
|
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes "${user}@${host}" '
|
|
export LC_ALL=C
|
|
printf "%s\n" __JEANNIE_RPI_OS_V1__
|
|
printf "kernel="; uname -sr
|
|
printf "uptime_load="; uptime
|
|
printf "memory_kib="; awk '\''/MemTotal:/ {t=$2} /MemAvailable:/ {a=$2} END {print t, a}'\'' /proc/meminfo
|
|
printf "root_percent="; df -P / | awk '\''NR==2 {gsub(/%/, "", $5); print $5}'\''
|
|
printf "systemd="; systemctl is-system-running || true
|
|
' 2>&1)" || { report_ui_emit warn "RPi4 OS" "OS snapshot" "Cannot read RPi OS state" "${snapshot}" "./jeannie doctor-rpi"; return 0; }
|
|
rows="$(printf '%s\n' "${snapshot}" | python3 "${REPO_ROOT}/scripts/doctor-rpi-os.py" "${LAB_STATUS_DISK_USE_THRESHOLD:-85}" 2>&1)" || {
|
|
report_ui_emit warn "RPi4 OS" "OS snapshot" "Cannot interpret RPi OS state" "${rows}" "./jeannie doctor-rpi"; return 0;
|
|
}
|
|
while IFS=$'\t' read -r severity name detail; do
|
|
[[ -n "${severity}" ]] || continue
|
|
report_ui_emit "${severity}" "RPi4 OS" "${name}" "${detail}" "${detail}" "./jeannie doctor-rpi"
|
|
done <<<"${rows}"
|
|
}
|
|
|
|
doctor_focus_compose_evidence() {
|
|
local scope="$1" check="$2" name host user key
|
|
name="${check##* (}"
|
|
name="${name%)}"
|
|
[[ "${name}" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.-]*$ ]] || { echo "Invalid container name" >&2; return 1; }
|
|
local format='{{.State.Status}} restarts={{.RestartCount}} exit={{.State.ExitCode}} health={{if .State.Health}}{{.State.Health.Status}}{{else}}not-configured{{end}}'
|
|
if [[ "${scope}" == debian ]]; then
|
|
timeout 10 sudo -n docker inspect --format "${format}" "${name}"
|
|
return
|
|
fi
|
|
case "${scope}" in
|
|
rpi) host="${LAB_RPI_HOST:-}"; user="${LAB_RPI_USER:-}"; key="${LAB_RPI_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}" ;;
|
|
opi5) host="${LAB_PIMOX_HOST:-}"; user="${LAB_PIMOX_USER:-}"; key="${LAB_PIMOX_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}" ;;
|
|
oci) host="${LAB_EDGE_TAILSCALE_IP:-}"; user="${LAB_EDGE_USER:-}"; key="${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}}" ;;
|
|
*) return 2 ;;
|
|
esac
|
|
[[ -n "${host}" && -n "${user}" ]] || { echo "Host or user is missing from inventory" >&2; return 1; }
|
|
timeout 10 ssh -n -i "${key}" -o BatchMode=yes -o ConnectTimeout=3 \
|
|
-o ConnectionAttempts=1 -o StrictHostKeyChecking=yes "${user}@${host}" \
|
|
"sudo -n docker inspect --format '${format}' '${name}'"
|
|
}
|
|
|
|
doctor_focus_extra() {
|
|
case "$1" in
|
|
oci)
|
|
if [[ -n "${LAB_PUBLIC_URL:-}" ]]; then
|
|
report_ui_check warn "OCI edge diagnostics" "Public HTTPS certificate and health" "curl -v --max-time 8 '${LAB_PUBLIC_URL%/}/edge-health'" "" "" doctor_focus_https
|
|
fi
|
|
;;
|
|
rpi)
|
|
report_ui_enqueue doctor_focus_rpi_os_report
|
|
report_ui_check warn "RPi4 diagnostics" "Docker root state" "./jeannie doctor-rpi --details" "" "" check_rpi_docker_root_state
|
|
report_ui_check warn "RPi4 diagnostics" "Tailscale IP" "./jeannie doctor-rpi --details" "" "" check_rpi_tailscale_ip
|
|
;;
|
|
debian)
|
|
report_ui_check warn "Debian diagnostics" "Gitea runner service" "systemctl status homelab-gitea-runner.service --no-pager" "" "" check_systemd_active homelab-gitea-runner
|
|
report_ui_check warn "Debian diagnostics" "Gitea SSH" "./jeannie doctor-gitea --details" "" "" check_gitea_ssh
|
|
;;
|
|
cluster)
|
|
report_ui_check warn "Kubernetes diagnostics" "Problem pods" "kubectl get pods -A -o wide" "" "" status_no_problem_pods
|
|
report_ui_check warn "Kubernetes diagnostics" "Recent deployments" "kubectl get deployments -A" "" "" status_recent_deployments
|
|
;;
|
|
opi5) ;;
|
|
esac
|
|
}
|
|
|
|
doctor_focus() {
|
|
local scope="${1:-}" item="" rc=0 selected area check severity evidence snapshot direct=false
|
|
local -a flags=()
|
|
case "${scope}" in
|
|
-h|--help) doctor_focus_usage; return 0 ;;
|
|
oci|rpi|opi5|debian|cluster) shift ;;
|
|
all|"") doctor_focus_usage >&2; return 2 ;;
|
|
-*) doctor_focus_usage >&2; return 2 ;;
|
|
*) item="${scope}"; scope=cluster; direct=true; shift ;;
|
|
esac
|
|
while (($#)); do
|
|
case "$1" in
|
|
-h|--help) doctor_focus_usage; return 0 ;;
|
|
--item)
|
|
if (($# < 2)) || [[ -z "$2" || "$2" == --* || -n "${item}" ]]; then echo "Provide exactly one item name." >&2; return 2; fi
|
|
item="$2"; shift 2 ;;
|
|
--ai|--no-local-rca|--json|--details|--all) flags+=("$1"); shift ;;
|
|
-*) echo "Unknown doctor option: $1" >&2; return 2 ;;
|
|
*) if [[ -n "${item}" ]]; then echo "Provide exactly one item name." >&2; return 2; fi; item="$1"; shift ;;
|
|
esac
|
|
done
|
|
require_debian_server "doctor"
|
|
# These variables are consumed by the dynamically scoped report helpers.
|
|
# shellcheck disable=SC2034
|
|
local REPORT_UI_ONLY=all REPORT_UI_LOCAL_RCA=false REPORT_UI_PARALLEL=true REPORT_UI_STATUS_SUMMARY=false
|
|
[[ -z "${item}" ]] || REPORT_UI_LOCAL_RCA=true
|
|
report_ui_parse_args "${flags[@]}" || return 2
|
|
report_ui_begin
|
|
if [[ "${direct}" == true ]]; then
|
|
if ! snapshot="$(report_ui_cached_probe doctor_focus_application_fetch "${item}")" ||
|
|
! selected="$(printf '%s' "${snapshot}" | python3 "${REPO_ROOT}/scripts/doctor-application.py" --report)"; then
|
|
report_ui_cleanup
|
|
echo "Cannot read Argo CD application '${item}' in the selected cluster." >&2
|
|
return 1
|
|
fi
|
|
printf '%s\n' "${selected}" >"${REPORT_UI_ROWS_FILE}"
|
|
else
|
|
status_focus_collect "${scope}"
|
|
if [[ -z "${item}" ]]; then doctor_focus_extra "${scope}"; fi
|
|
fi
|
|
report_ui_finish
|
|
if [[ -n "${item}" ]]; then
|
|
if ! selected="$(python3 "${REPO_ROOT}/scripts/doctor-filter.py" "${item}" <"${REPORT_UI_ROWS_FILE}")"; then
|
|
report_ui_cleanup
|
|
return 2
|
|
fi
|
|
printf '%s\n' "${selected}" >"${REPORT_UI_ROWS_FILE}"
|
|
REPORT_UI_FAILURES=0
|
|
IFS=$'\t' read -r severity area check _ <<<"${selected}"
|
|
[[ "${selected}" != fail$'\t'* ]] || REPORT_UI_FAILURES=1
|
|
if [[ "${area}" == "Kubernetes apps" ]]; then
|
|
# Application evidence is a separate read-only request for the selected item.
|
|
# shellcheck disable=SC2034
|
|
REPORT_UI_PARALLEL=false
|
|
if evidence="$(doctor_focus_application_evidence "${check}" 2>&1)"; then
|
|
report_ui_emit "${severity}" "Kubernetes evidence" "${check} application details" "Application state and resources" "${evidence}" "kubectl get pods -A -o wide"
|
|
else
|
|
report_ui_emit warn "Kubernetes evidence" "${check} application details" "Cannot read application evidence" "${evidence}" "kubectl get pods -A -o wide"
|
|
fi
|
|
elif [[ "${area}" == *" Compose" ]]; then
|
|
# shellcheck disable=SC2034
|
|
REPORT_UI_PARALLEL=false
|
|
report_ui_check warn "Compose evidence" "${check} container state retrieved" "docker ps -a --filter label=com.docker.compose.project" "" "" doctor_focus_compose_evidence "${scope}" "${check}"
|
|
fi
|
|
fi
|
|
((REPORT_UI_FAILURES == 0)) || rc=1
|
|
report_ui_render "Jeannie Doctor: ${scope}${item:+ / ${item}}"
|
|
report_ui_cleanup
|
|
return "${rc}"
|
|
}
|