#!/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())