File size: 3,533 Bytes
61a4c8d
20fae3a
 
83254b4
20fae3a
 
 
 
27f63e9
20fae3a
 
 
 
83254b4
 
 
 
 
 
 
 
 
 
 
27f63e9
83254b4
 
 
 
 
 
 
 
20fae3a
 
 
 
 
 
83254b4
 
 
 
 
20fae3a
83254b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20fae3a
 
83254b4
 
 
 
 
 
20fae3a
 
 
27f63e9
20fae3a
 
 
83254b4
 
 
 
 
 
 
 
 
 
 
 
 
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
# modules/system_diagnostics.py - System Diagnostics Module
from fastapi import APIRouter
import psutil
import os
from datetime import datetime

def register_module(app, client, username):
    """Register diagnostics module with FastAPI app"""
    router = APIRouter(prefix="/system")
    
    @router.get("/diagnostics/health")
    async def diagnostics_health():
        """Basic system health check"""
        try:
            cpu = psutil.cpu_percent()
            memory = psutil.virtual_memory()
            
            return {
                "success": True,
                "module": "diagnostics",
                "status": "active",
                "system": {
                    "cpu_usage": cpu,
                    "memory_used": memory.percent,
                    "memory_available": round(memory.available / (1024**3), 2),
                    "timestamp": datetime.now().isoformat()
                }
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "module": "diagnostics"
            }
    
    @router.get("/diagnostics/full")
    async def full_diagnostics():
        """Complete system diagnostics"""
        try:
            cpu = psutil.cpu_percent()
            memory = psutil.virtual_memory()
            disk = psutil.disk_usage('/')
            app_running = any('app.py' in p.info().get('cmdline', []) for p in psutil.process_iter(['cmdline']))
            
            return {
                "success": True,
                "diagnostics": {
                    "timestamp": datetime.now().isoformat(),
                    "system_id": f"DIAG-{os.getpid()}",
                    "status": "HEALTHY",
                    "health_score": 85,
                    "sections": {
                        "system_resources": {
                            "cpu": {"usage_percent": cpu, "cores": psutil.cpu_count()},
                            "memory": {
                                "used_percent": memory.percent,
                                "available_gb": round(memory.available / (1024**3), 2)
                            },
                            "disk": {
                                "used_percent": disk.percent,
                                "free_gb": round(disk.free / (1024**3), 2)
                            }
                        },
                        "application": {
                            "app_running": app_running,
                            "directories": {
                                "logs": os.path.exists('logs'),
                                "data": os.path.exists('data')
                            }
                        }
                    }
                }
            }
        except Exception as e:
            return {
                "success": False,
                "error": str(e),
                "message": "Diagnostics failed"
            }
    
    app.include_router(router)
    print("✅ Diagnostics module registered with FastAPI")
    return {"status": "registered"}

async def get_basic_metrics():
    """Get basic system metrics"""
    try:
        return {
            "success": True,
            "cpu": psutil.cpu_percent(),
            "memory": psutil.virtual_memory().percent,
            "disk": psutil.disk_usage('/').percent,
            "timestamp": datetime.now().isoformat()
        }
    except Exception as e:
        return {
            "success": False,
            "error": str(e)
        }