#!/usr/bin/env python3 """ Trading System Monitor Real-time monitoring for Docker deployment. """ import time import os from datetime import datetime, timedelta def monitor_system(): """Monitor the trading system performance""" print("šŸ” Trading System Monitor - Starting...") print("=" * 60) while True: try: current_time = datetime.now() print(f"\nšŸ“Š System Status - {current_time.strftime('%Y-%m-%d %H:%M:%S')}") print("-" * 40) # Check health file health_status = "āŒ UNKNOWN" if os.path.exists('/tmp/health'): try: with open('/tmp/health', 'r') as f: health_data = f.read().strip() if health_data.startswith('OK:'): parts = health_data.split(':') health_time = datetime.fromisoformat(parts[1]) price = parts[2] if len(parts) > 2 else "N/A" # Check if health is recent (within 10 minutes) if current_time - health_time < timedelta(minutes=10): health_status = f"āœ… HEALTHY (Price: ${price})" else: health_status = f"āš ļø STALE ({(current_time - health_time).seconds//60}m ago)" else: health_status = "āŒ ERROR" except Exception as e: health_status = f"āŒ ERROR: {e}" print(f"Health Status: {health_status}") # Check log files log_files = { 'trading.log': 'General', 'trades.log': 'Trades', 'risk.log': 'Risk Events', 'performance.log': 'Performance' } for log_file, description in log_files.items(): log_path = f'/app/logs/{log_file}' if os.path.exists(log_path): try: stat = os.stat(log_path) size_mb = stat.st_size / (1024 * 1024) mod_time = datetime.fromtimestamp(stat.st_mtime) age_minutes = (current_time - mod_time).seconds // 60 print(f"{description:12}: {size_mb:.1f}MB, {age_minutes}m ago") except Exception as e: print(f"{description:12}: Error - {e}") else: print(f"{description:12}: Not found") # Show recent performance if available perf_file = '/app/logs/performance.log' if os.path.exists(perf_file): try: with open(perf_file, 'r') as f: lines = f.readlines() if lines: recent_lines = lines[-3:] # Last 3 entries print("\nšŸ“ˆ Recent Performance:") for line in recent_lines: if line.strip(): print(f" {line.strip()}") except Exception as e: print(f"Performance read error: {e}") # Show recent trades if available trades_file = '/app/logs/trades.log' if os.path.exists(trades_file): try: with open(trades_file, 'r') as f: lines = f.readlines() if lines: recent_trades = [line for line in lines[-5:] if 'BUY' in line or 'SELL' in line] if recent_trades: print("\nšŸ’° Recent Trades:") for trade in recent_trades[-2:]: # Last 2 trades print(f" {trade.strip()}") except Exception as e: print(f"Trades read error: {e}") print("\n" + "=" * 60) # Wait 5 minutes before next check time.sleep(300) except KeyboardInterrupt: print("\nšŸ‘‹ Monitor stopped by user") break except Exception as e: print(f"āŒ Monitor error: {e}") time.sleep(60) # Wait 1 minute on error if __name__ == "__main__": monitor_system()