#!/bin/sh # MCPTesta Documentation Health Check Script # Comprehensive health validation for container monitoring set -e # Configuration HOST=${HOST:-localhost} PORT=${PORT:-4321} TIMEOUT=${HEALTH_TIMEOUT:-10} MAX_RESPONSE_TIME=5000 # milliseconds # Colors for output (simplified for sh compatibility) RED='\033[0;31m' GREEN='\033[0;32m' NC='\033[0m' # Logging functions log() { echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" } success() { echo "${GREEN}[HEALTHY]${NC} $1" } error() { echo "${RED}[UNHEALTHY]${NC} $1" >&2 exit 1 } # Health check functions check_port() { if ! nc -z "$HOST" "$PORT" 2>/dev/null; then error "Port $PORT is not accessible on $HOST" fi log "Port $PORT is accessible" } check_http_response() { local response_code local response_time # Check HTTP response with timeout if ! response_code=$(wget --spider --server-response --timeout="$TIMEOUT" --tries=1 \ "http://$HOST:$PORT/" 2>&1 | grep "HTTP/" | tail -1 | awk '{print $2}'); then error "HTTP request failed or timed out" fi # Validate response code if [ "$response_code" != "200" ]; then error "HTTP response code: $response_code (expected: 200)" fi log "HTTP response: $response_code OK" } check_response_time() { local start_time local end_time local response_time start_time=$(date +%s%3N) if ! wget --spider --quiet --timeout="$TIMEOUT" --tries=1 "http://$HOST:$PORT/" 2>/dev/null; then error "Response time check failed" fi end_time=$(date +%s%3N) response_time=$((end_time - start_time)) if [ "$response_time" -gt "$MAX_RESPONSE_TIME" ]; then error "Response time too slow: ${response_time}ms (max: ${MAX_RESPONSE_TIME}ms)" fi log "Response time: ${response_time}ms" } check_content() { local content # Check if the page contains expected content if ! content=$(wget --quiet --timeout="$TIMEOUT" --tries=1 -O- "http://$HOST:$PORT/" 2>/dev/null); then error "Failed to retrieve page content" fi # Basic content validation if ! echo "$content" | grep -q "MCPTesta"; then error "Page content validation failed - 'MCPTesta' not found" fi if ! echo "$content" | grep -q "/dev/null 2>&1 || error "wget command not found" command -v nc >/dev/null 2>&1 || error "nc (netcat) command not found" log "Required dependencies available" } check_memory_usage() { local memory_usage local memory_limit_mb=512 # Default limit # Get memory usage in MB (simplified check) if [ -f /proc/meminfo ]; then memory_usage=$(awk '/MemAvailable/ {printf "%.0f", $2/1024}' /proc/meminfo) if [ "$memory_usage" -lt 50 ]; then error "Low available memory: ${memory_usage}MB" fi log "Available memory: ${memory_usage}MB" else log "Memory check skipped (no /proc/meminfo)" fi } check_disk_space() { local disk_usage local disk_limit=90 # 90% threshold # Check disk usage of /app if disk_usage=$(df /app 2>/dev/null | tail -1 | awk '{print $5}' | sed 's/%//'); then if [ "$disk_usage" -gt "$disk_limit" ]; then error "High disk usage: ${disk_usage}% (limit: ${disk_limit}%)" fi log "Disk usage: ${disk_usage}%" else log "Disk check skipped" fi } # Main health check routine main() { log "Starting comprehensive health check..." log "Target: http://$HOST:$PORT/" log "Timeout: ${TIMEOUT}s" # Run all health checks check_dependencies check_memory_usage check_disk_space check_port check_http_response check_response_time check_content success "All health checks passed" log "Container is healthy and ready to serve requests" } # Run health check main