Files
ATLAS/deploy/scripts/health-check.sh
2026-01-21 08:26:01 +00:00

248 lines
6.3 KiB
Bash

#!/bin/bash
# ============================================
# Sistema de ADAN - Health Check
# ============================================
# Verifica el estado de todos los servicios
#
# Uso: ./health-check.sh [--verbose] [--json]
# ============================================
set -o pipefail
# ---------------------------------------------
# Colores
# ---------------------------------------------
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# Variables
INSTALL_DIR="${INSTALL_DIR:-/opt/adan}"
VERBOSE=false
JSON_OUTPUT=false
EXIT_CODE=0
# Cargar variables de entorno
if [[ -f "$INSTALL_DIR/.env" ]]; then
export $(grep -v '^#' "$INSTALL_DIR/.env" | xargs)
fi
# ---------------------------------------------
# Funciones
# ---------------------------------------------
parse_args() {
while [[ $# -gt 0 ]]; do
case $1 in
--verbose|-v) VERBOSE=true; shift ;;
--json|-j) JSON_OUTPUT=true; shift ;;
--help|-h)
echo "Uso: $0 [--verbose] [--json]"
exit 0
;;
*) shift ;;
esac
done
}
check_service() {
local service="$1"
local name="$2"
if systemctl is-active --quiet "$service" 2>/dev/null; then
echo "ok"
else
echo "fail"
fi
}
check_port() {
local port="$1"
if nc -z localhost "$port" 2>/dev/null; then
echo "ok"
else
echo "fail"
fi
}
check_url() {
local url="$1"
local timeout="${2:-5}"
if curl -sf --max-time "$timeout" "$url" > /dev/null 2>&1; then
echo "ok"
else
echo "fail"
fi
}
check_db() {
local host="${POSTGRES_HOST:-localhost}"
local port="${POSTGRES_PORT:-5432}"
local db="${POSTGRES_DB:-adan}"
local user="${POSTGRES_USER:-adan}"
if PGPASSWORD="${POSTGRES_PASSWORD}" psql -h "$host" -p "$port" -U "$user" -d "$db" -c "SELECT 1" > /dev/null 2>&1; then
echo "ok"
else
echo "fail"
fi
}
check_redis() {
local password="${REDIS_PASSWORD:-}"
if [[ -n "$password" ]]; then
if redis-cli -a "$password" ping 2>/dev/null | grep -q "PONG"; then
echo "ok"
else
echo "fail"
fi
else
if redis-cli ping 2>/dev/null | grep -q "PONG"; then
echo "ok"
else
echo "fail"
fi
fi
}
print_status() {
local name="$1"
local status="$2"
local details="$3"
if [[ "$JSON_OUTPUT" == "true" ]]; then
return
fi
if [[ "$status" == "ok" ]]; then
echo -e " ${GREEN}[OK]${NC} $name"
else
echo -e " ${RED}[FAIL]${NC} $name"
EXIT_CODE=1
fi
if [[ "$VERBOSE" == "true" ]] && [[ -n "$details" ]]; then
echo -e " $details"
fi
}
# ---------------------------------------------
# Main
# ---------------------------------------------
main() {
parse_args "$@"
# Resultados para JSON
declare -A results
if [[ "$JSON_OUTPUT" != "true" ]]; then
echo ""
echo -e "${BLUE}========================================${NC}"
echo -e "${BLUE} HEALTH CHECK - Sistema de ADAN${NC}"
echo -e "${BLUE}========================================${NC}"
echo ""
echo -e "${BLUE}Servicios Systemd:${NC}"
fi
# Servicios systemd
results[adan_api]=$(check_service "adan-api" "API Backend")
print_status "adan-api" "${results[adan_api]}"
results[adan_web]=$(check_service "adan-web" "Frontend Web")
print_status "adan-web" "${results[adan_web]}"
results[postgresql]=$(check_service "postgresql" "PostgreSQL")
print_status "postgresql" "${results[postgresql]}"
results[redis]=$(check_service "redis-server" "Redis")
print_status "redis" "${results[redis]}"
results[traccar]=$(check_service "traccar" "Traccar GPS")
print_status "traccar" "${results[traccar]}"
results[mediamtx]=$(check_service "mediamtx" "MediaMTX")
print_status "mediamtx" "${results[mediamtx]}"
results[mosquitto]=$(check_service "mosquitto" "Mosquitto MQTT")
print_status "mosquitto" "${results[mosquitto]}"
if [[ "$JSON_OUTPUT" != "true" ]]; then
echo ""
echo -e "${BLUE}Conectividad:${NC}"
fi
# Puertos
results[port_api]=$(check_port "${API_PORT:-8000}")
print_status "API (puerto ${API_PORT:-8000})" "${results[port_api]}"
results[port_frontend]=$(check_port "${FRONTEND_PORT:-3000}")
print_status "Frontend (puerto ${FRONTEND_PORT:-3000})" "${results[port_frontend]}"
results[port_traccar]=$(check_port "${TRACCAR_PORT:-5055}")
print_status "Traccar (puerto ${TRACCAR_PORT:-5055})" "${results[port_traccar]}"
results[port_rtsp]=$(check_port 8554)
print_status "MediaMTX RTSP (puerto 8554)" "${results[port_rtsp]}"
if [[ "$JSON_OUTPUT" != "true" ]]; then
echo ""
echo -e "${BLUE}Base de Datos:${NC}"
fi
# Base de datos
results[db_connection]=$(check_db)
print_status "PostgreSQL conexion" "${results[db_connection]}"
results[redis_connection]=$(check_redis)
print_status "Redis conexion" "${results[redis_connection]}"
if [[ "$JSON_OUTPUT" != "true" ]]; then
echo ""
echo -e "${BLUE}APIs:${NC}"
fi
# APIs
results[api_health]=$(check_url "http://localhost:${API_PORT:-8000}/health")
print_status "API /health" "${results[api_health]}"
results[mediamtx_api]=$(check_url "http://localhost:9997/v3/paths/list")
print_status "MediaMTX API" "${results[mediamtx_api]}"
# JSON output
if [[ "$JSON_OUTPUT" == "true" ]]; then
echo "{"
echo " \"timestamp\": \"$(date -Iseconds)\","
echo " \"status\": \"$([ $EXIT_CODE -eq 0 ] && echo 'healthy' || echo 'unhealthy')\","
echo " \"checks\": {"
first=true
for key in "${!results[@]}"; do
if [[ "$first" != "true" ]]; then
echo ","
fi
first=false
printf " \"%s\": \"%s\"" "$key" "${results[$key]}"
done
echo ""
echo " }"
echo "}"
else
echo ""
if [[ $EXIT_CODE -eq 0 ]]; then
echo -e "${GREEN}Estado general: SALUDABLE${NC}"
else
echo -e "${RED}Estado general: PROBLEMAS DETECTADOS${NC}"
fi
echo ""
fi
exit $EXIT_CODE
}
main "$@"