File size: 11,410 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
"""
Health Monitoring System
Continuous health monitoring for all API endpoints
"""

import schedule
import time
import requests
import json
import logging
from datetime import datetime
from typing import Dict, List, Optional
from pathlib import Path

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class HealthMonitor:
    """Continuous health monitoring for all endpoints"""
    
    def __init__(self, base_url: str = "http://localhost:7860"):
        self.base_url = base_url
        self.endpoints = self.load_endpoints()
        self.health_history = []
        self.alert_threshold = 3  # Number of consecutive failures before alert
        self.failure_counts = {}  # Track consecutive failures per endpoint
    
    def load_endpoints(self) -> List[Dict]:
        """Load endpoints from service registry"""
        registry_file = Path("config/service_registry.json")
        
        if not registry_file.exists():
            logger.warning("⚠ Service registry not found, using default endpoints")
            return self._get_default_endpoints()
        
        try:
            with open(registry_file, 'r') as f:
                registry = json.load(f)
            
            endpoints = []
            for service in registry.get("services", []):
                for endpoint in service.get("endpoints", []):
                    endpoints.append({
                        "path": endpoint.get("path", ""),
                        "method": endpoint.get("method", "GET"),
                        "category": service.get("category", "unknown"),
                        "service_id": service.get("id", "unknown"),
                        "base_url": self.base_url
                    })
            
            return endpoints
        
        except Exception as e:
            logger.error(f"❌ Failed to load endpoints: {e}")
            return self._get_default_endpoints()
    
    def _get_default_endpoints(self) -> List[Dict]:
        """Get default endpoints for monitoring"""
        return [
            {"path": "/api/health", "method": "GET", "category": "system", "base_url": self.base_url},
            {"path": "/api/ohlcv/BTC", "method": "GET", "category": "market_data", "base_url": self.base_url},
            {"path": "/api/v1/ohlcv/BTC", "method": "GET", "category": "market_data", "base_url": self.base_url},
            {"path": "/api/market/ohlcv", "method": "GET", "category": "market_data", "base_url": self.base_url, "params": {"symbol": "BTC", "interval": "1d", "limit": 30}},
        ]
    
    def check_endpoint_health(self, endpoint: Dict) -> Dict:
        """Check health of single endpoint"""
        path = endpoint["path"]
        method = endpoint.get("method", "GET").upper()
        params = endpoint.get("params", {})
        
        try:
            start_time = time.time()
            url = f"{endpoint['base_url']}{path}"
            
            if method == "GET":
                response = requests.get(url, params=params, timeout=10)
            elif method == "POST":
                response = requests.post(url, json=params, timeout=10)
            else:
                response = requests.request(method, url, json=params, timeout=10)
            
            response_time = (time.time() - start_time) * 1000
            
            is_healthy = response.status_code in [200, 201]
            
            result = {
                "endpoint": path,
                "status": "healthy" if is_healthy else "degraded",
                "status_code": response.status_code,
                "response_time_ms": round(response_time, 2),
                "timestamp": datetime.now().isoformat(),
                "method": method
            }
            
            # Update failure count
            if is_healthy:
                self.failure_counts[path] = 0
            else:
                self.failure_counts[path] = self.failure_counts.get(path, 0) + 1
                result["consecutive_failures"] = self.failure_counts[path]
            
            return result
        
        except requests.exceptions.Timeout:
            self.failure_counts[path] = self.failure_counts.get(path, 0) + 1
            return {
                "endpoint": path,
                "status": "down",
                "error": "timeout",
                "timestamp": datetime.now().isoformat(),
                "method": method,
                "consecutive_failures": self.failure_counts[path]
            }
        
        except Exception as e:
            self.failure_counts[path] = self.failure_counts.get(path, 0) + 1
            return {
                "endpoint": path,
                "status": "down",
                "error": str(e),
                "timestamp": datetime.now().isoformat(),
                "method": method,
                "consecutive_failures": self.failure_counts[path]
            }
    
    def check_all_endpoints(self):
        """Check health of all registered endpoints"""
        results = []
        
        logger.info(f"πŸ” Checking {len(self.endpoints)} endpoints...")
        
        for endpoint in self.endpoints:
            health = self.check_endpoint_health(endpoint)
            results.append(health)
            
            # Check if alert needed
            if health['status'] != "healthy":
                self.handle_unhealthy_endpoint(health)
        
        # Store in history
        self.health_history.append({
            "check_time": datetime.now().isoformat(),
            "results": results,
            "summary": {
                "total": len(results),
                "healthy": sum(1 for r in results if r['status'] == "healthy"),
                "degraded": sum(1 for r in results if r['status'] == "degraded"),
                "down": sum(1 for r in results if r['status'] == "down")
            }
        })
        
        # Keep only last 100 checks
        if len(self.health_history) > 100:
            self.health_history = self.health_history[-100:]
        
        # Save to file
        self.save_health_report(results)
        
        return results
    
    def handle_unhealthy_endpoint(self, health: Dict):
        """Handle unhealthy endpoint detection"""
        path = health["endpoint"]
        consecutive_failures = health.get("consecutive_failures", 0)
        
        if consecutive_failures >= self.alert_threshold:
            self.send_alert(health)
    
    def send_alert(self, health: Dict):
        """Send alert about failing endpoint"""
        alert_message = f"""
⚠️ ALERT: Endpoint Health Issue

Endpoint: {health['endpoint']}
Status: {health['status']}
Error: {health.get('error', 'N/A')}
Time: {health['timestamp']}
Consecutive Failures: {health.get('consecutive_failures', 0)}
"""
        
        logger.error(alert_message)
        
        # Save alert to file
        alerts_file = Path("monitoring/alerts.json")
        alerts_file.parent.mkdir(parents=True, exist_ok=True)
        
        try:
            if alerts_file.exists():
                with open(alerts_file, 'r') as f:
                    alerts = json.load(f)
            else:
                alerts = []
            
            alerts.append({
                "timestamp": datetime.now().isoformat(),
                "endpoint": health["endpoint"],
                "status": health["status"],
                "error": health.get("error"),
                "consecutive_failures": health.get("consecutive_failures", 0)
            })
            
            # Keep only last 50 alerts
            alerts = alerts[-50:]
            
            with open(alerts_file, 'w') as f:
                json.dump(alerts, f, indent=2)
        
        except Exception as e:
            logger.error(f"Failed to save alert: {e}")
    
    def save_health_report(self, results: List[Dict]):
        """Save health check results to file"""
        reports_dir = Path("monitoring/reports")
        reports_dir.mkdir(parents=True, exist_ok=True)
        
        report_file = reports_dir / f"health_report_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
        
        report = {
            "timestamp": datetime.now().isoformat(),
            "total_endpoints": len(results),
            "healthy": sum(1 for r in results if r['status'] == "healthy"),
            "degraded": sum(1 for r in results if r['status'] == "degraded"),
            "down": sum(1 for r in results if r['status'] == "down"),
            "results": results
        }
        
        try:
            with open(report_file, 'w') as f:
                json.dump(report, f, indent=2)
            
            # Also update latest report
            latest_file = reports_dir / "health_report_latest.json"
            with open(latest_file, 'w') as f:
                json.dump(report, f, indent=2)
        
        except Exception as e:
            logger.error(f"Failed to save health report: {e}")
    
    def get_health_summary(self) -> Dict:
        """Get summary of health status"""
        if not self.health_history:
            return {
                "status": "unknown",
                "message": "No health checks performed yet"
            }
        
        latest = self.health_history[-1]
        summary = latest["summary"]
        
        total = summary["total"]
        healthy = summary["healthy"]
        health_percentage = (healthy / total * 100) if total > 0 else 0
        
        return {
            "status": "healthy" if health_percentage >= 95 else "degraded" if health_percentage >= 80 else "unhealthy",
            "health_percentage": round(health_percentage, 2),
            "total_endpoints": total,
            "healthy": healthy,
            "degraded": summary["degraded"],
            "down": summary["down"],
            "last_check": latest["check_time"]
        }
    
    def start_monitoring(self, interval_minutes: int = 5):
        """Start continuous monitoring"""
        logger.info(f"πŸ” Health monitoring started (checking every {interval_minutes} minutes)")
        logger.info(f"πŸ“Š Monitoring {len(self.endpoints)} endpoints")
        
        # Run initial check
        self.check_all_endpoints()
        
        # Schedule periodic checks
        schedule.every(interval_minutes).minutes.do(self.check_all_endpoints)
        
        try:
            while True:
                schedule.run_pending()
                time.sleep(1)
        except KeyboardInterrupt:
            logger.info("πŸ›‘ Health monitoring stopped")


if __name__ == "__main__":
    import argparse
    
    parser = argparse.ArgumentParser(description="Health Monitoring System")
    parser.add_argument("--base-url", default="http://localhost:7860", help="Base URL for API")
    parser.add_argument("--interval", type=int, default=5, help="Check interval in minutes")
    parser.add_argument("--once", action="store_true", help="Run once and exit")
    
    args = parser.parse_args()
    
    monitor = HealthMonitor(base_url=args.base_url)
    
    if args.once:
        results = monitor.check_all_endpoints()
        summary = monitor.get_health_summary()
        print("\n" + "="*50)
        print("HEALTH SUMMARY")
        print("="*50)
        print(json.dumps(summary, indent=2))
        print("="*50)
    else:
        monitor.start_monitoring(interval_minutes=args.interval)