Files
gogo2/COBY/health_check.py
2025-08-05 01:22:27 +03:00

104 lines
3.1 KiB
Python

#!/usr/bin/env python3
"""
Health check script for COBY application
Used by Docker health checks and monitoring systems
"""
import sys
import os
import requests
import json
from datetime import datetime
def check_health():
"""Perform health check on COBY application"""
try:
# Check main API endpoint
response = requests.get('http://localhost:8080/health', timeout=5)
if response.status_code == 200:
health_data = response.json()
# Basic health check passed
print(f"✅ API Health Check: PASSED")
print(f" Status: {health_data.get('status', 'unknown')}")
print(f" Timestamp: {health_data.get('timestamp', 'unknown')}")
# Check individual components
components = health_data.get('components', {})
all_healthy = True
for component, status in components.items():
if status.get('healthy', False):
print(f"{component}: HEALTHY")
else:
print(f"{component}: UNHEALTHY - {status.get('error', 'unknown error')}")
all_healthy = False
if all_healthy:
print("\n🎉 Overall Health: HEALTHY")
return 0
else:
print("\n⚠️ Overall Health: DEGRADED")
return 1
else:
print(f"❌ API Health Check: FAILED (HTTP {response.status_code})")
return 1
except requests.exceptions.ConnectionError:
print("❌ API Health Check: FAILED (Connection refused)")
return 1
except requests.exceptions.Timeout:
print("❌ API Health Check: FAILED (Timeout)")
return 1
except Exception as e:
print(f"❌ API Health Check: FAILED ({str(e)})")
return 1
def check_websocket():
"""Check WebSocket server health"""
try:
# Simple TCP connection check to WebSocket port
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5)
result = sock.connect_ex(('localhost', 8081))
sock.close()
if result == 0:
print("✅ WebSocket Server: ACCESSIBLE")
return True
else:
print("❌ WebSocket Server: NOT ACCESSIBLE")
return False
except Exception as e:
print(f"❌ WebSocket Server: ERROR ({str(e)})")
return False
def main():
"""Main health check function"""
print(f"COBY Health Check - {datetime.now().isoformat()}")
print("=" * 50)
# Check API health
api_healthy = check_health() == 0
# Check WebSocket
ws_healthy = check_websocket()
print("=" * 50)
if api_healthy and ws_healthy:
print("🎉 COBY System: FULLY HEALTHY")
return 0
elif api_healthy:
print("⚠️ COBY System: PARTIALLY HEALTHY (API only)")
return 1
else:
print("❌ COBY System: UNHEALTHY")
return 1
if __name__ == "__main__":
sys.exit(main())