homelab-codex-ws/scripts/lib/diagnostics.sh

58 lines
1.7 KiB
Bash

#!/usr/bin/env bash
# diagnostics.sh - Deployment failure diagnostics
collect_diagnostics() {
local host=$1
local service=$2
log "INFO" "Stage: DIAGNOSE ($host - ${service:-all})"
if [[ -n "$service" ]]; then
emit_event "remediation_started" "warning" "diagnostics.sh" "$service" "${TIMESTAMP}" "{\"reason\": \"failure_detected\"}"
fi
local diag_file="${LOG_DIR}/diagnostics_${TIMESTAMP}.txt"
{
echo "--- DIAGNOSTICS FOR ${service:-all} (Host: $host, Time: $(date)) ---"
echo "Uptime: $(uptime)"
echo "Memory: $(free -h)"
echo "Disk: $(df -h /)"
echo "--- Docker Status ---"
docker ps --filter "name=${service:-}"
if [[ -n "$service" ]]; then
local svc_dir="${REPO_PATH}/services/$service"
if [[ -d "$svc_dir" ]]; then
echo "--- $service Logs ---"
cd "$svc_dir" && docker compose logs --tail=50
fi
fi
echo "--- END DIAGNOSTICS ---"
} > "$diag_file" 2>&1
# Also output to console for immediate visibility
cat "$diag_file"
log "INFO" "Diagnostics stored in $diag_file"
}
print_summary() {
local host=$1
local status=$2
local last_stage=$(get_stage)
local last_service=$(get_last_service)
echo ""
echo "=========================================="
echo " DEPLOYMENT SUMMARY"
echo "=========================================="
echo "Host: $host"
echo "Status: $status"
echo "Last Stage: $last_stage"
[[ -n "$last_service" ]] && echo "Last Service: $last_service"
echo "Log File: $LOG_FILE"
echo "=========================================="
echo ""
}
export -f collect_diagnostics
export -f print_summary