Files
moyosapp_beta.0.0.3/scripts/health-check.sh
2026-01-15 16:19:14 +02:00

194 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# Health check script for Moyos Wedding App
# Usage: ./health-check.sh [--verbose]
set -e
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
VERBOSE=false
if [ "$1" = "--verbose" ]; then
VERBOSE=true
fi
log() {
echo -e "${GREEN}[OK]${NC} $1"
}
error() {
echo -e "${RED}[FAIL]${NC} $1" >&2
}
warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
info() {
if [ "$VERBOSE" = true ]; then
echo -e "${BLUE}[INFO]${NC} $1"
fi
}
# Configuration
HEALTH_URL="${HEALTH_URL:-http://localhost:3000/api/health}"
METRICS_URL="${METRICS_URL:-http://localhost:3000/api/metrics}"
MAX_RESPONSE_TIME=5000 # 5 seconds in milliseconds
# Track failures
FAILURES=0
CHECKS=0
check() {
CHECKS=$((CHECKS + 1))
if [ "$1" -eq 0 ]; then
log "$2"
return 0
else
error "$2"
FAILURES=$((FAILURES + 1))
return 1
fi
}
echo "Running health checks for Moyos Wedding App..."
echo ""
# Check 1: Application is running
info "Checking if application is running..."
if command -v pm2 &> /dev/null; then
if pm2 list | grep -q "moyos-wedding-app.*online"; then
check 0 "Application is running (PM2)"
else
check 1 "Application is not running (PM2)"
fi
else
warn "PM2 not found, skipping PM2 check"
fi
# Check 2: Health endpoint
info "Checking health endpoint..."
HEALTH_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$HEALTH_URL" 2>/dev/null || echo "000")
if [ "$HEALTH_RESPONSE" = "200" ]; then
check 0 "Health endpoint returns 200 OK"
# Get response time
RESPONSE_TIME=$(curl -s -o /dev/null -w "%{time_total}" --max-time 5 "$HEALTH_URL" 2>/dev/null || echo "999")
RESPONSE_TIME_MS=$(echo "$RESPONSE_TIME * 1000" | bc | cut -d. -f1)
if [ "$RESPONSE_TIME_MS" -lt "$MAX_RESPONSE_TIME" ]; then
log "Health endpoint response time: ${RESPONSE_TIME_MS}ms"
else
warn "Health endpoint response time: ${RESPONSE_TIME_MS}ms (slow)"
fi
else
check 1 "Health endpoint returns $HEALTH_RESPONSE (expected 200)"
fi
# Check 3: Metrics endpoint
info "Checking metrics endpoint..."
METRICS_RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" --max-time 5 "$METRICS_URL" 2>/dev/null || echo "000")
if [ "$METRICS_RESPONSE" = "200" ]; then
check 0 "Metrics endpoint accessible"
else
warn "Metrics endpoint returns $METRICS_RESPONSE (may be restricted)"
fi
# Check 4: Database connection
info "Checking database connection..."
if [ -n "$DATABASE_URL" ]; then
if command -v psql &> /dev/null; then
if psql "$DATABASE_URL" -c "SELECT 1;" > /dev/null 2>&1; then
check 0 "Database connection successful"
else
check 1 "Database connection failed"
fi
else
warn "psql not found, skipping database check"
fi
else
warn "DATABASE_URL not set, skipping database check"
fi
# Check 5: Redis connection (if used)
info "Checking Redis connection..."
if [ -n "$REDIS_URL" ]; then
if command -v redis-cli &> /dev/null; then
REDIS_HOST=$(echo "$REDIS_URL" | sed -n 's/redis:\/\/\([^:]*\):.*/\1/p')
REDIS_PORT=$(echo "$REDIS_URL" | sed -n 's/redis:\/\/[^:]*:\([0-9]*\).*/\1/p')
if [ -z "$REDIS_PORT" ]; then
REDIS_PORT=6379
fi
if redis-cli -h "$REDIS_HOST" -p "$REDIS_PORT" ping > /dev/null 2>&1; then
check 0 "Redis connection successful"
else
check 1 "Redis connection failed"
fi
else
warn "redis-cli not found, skipping Redis check"
fi
else
info "REDIS_URL not set, skipping Redis check"
fi
# Check 6: Disk space
info "Checking disk space..."
DISK_USAGE=$(df -h / | awk 'NR==2 {print $5}' | sed 's/%//')
if [ "$DISK_USAGE" -lt 80 ]; then
check 0 "Disk usage: ${DISK_USAGE}%"
elif [ "$DISK_USAGE" -lt 90 ]; then
warn "Disk usage: ${DISK_USAGE}% (warning threshold)"
else
check 1 "Disk usage: ${DISK_USAGE}% (critical)"
fi
# Check 7: Memory usage
info "Checking memory usage..."
if command -v free &> /dev/null; then
MEM_TOTAL=$(free -m | awk 'NR==2{print $2}')
MEM_USED=$(free -m | awk 'NR==2{print $3}')
MEM_PERCENT=$((MEM_USED * 100 / MEM_TOTAL))
if [ "$MEM_PERCENT" -lt 85 ]; then
check 0 "Memory usage: ${MEM_PERCENT}%"
elif [ "$MEM_PERCENT" -lt 95 ]; then
warn "Memory usage: ${MEM_PERCENT}% (warning threshold)"
else
check 1 "Memory usage: ${MEM_PERCENT}% (critical)"
fi
else
warn "free command not found, skipping memory check"
fi
# Check 8: Nginx status (if running)
info "Checking Nginx status..."
if systemctl is-active --quiet nginx 2>/dev/null; then
check 0 "Nginx is running"
elif command -v nginx &> /dev/null; then
warn "Nginx is installed but not running (may be in container)"
else
info "Nginx not found, skipping check"
fi
# Summary
echo ""
echo "Health Check Summary:"
echo " Total checks: $CHECKS"
echo " Passed: $((CHECKS - FAILURES))"
echo " Failed: $FAILURES"
if [ $FAILURES -eq 0 ]; then
echo ""
log "All health checks passed! ✅"
exit 0
else
echo ""
error "Some health checks failed. Please review the errors above."
exit 1
fi