File size: 2,345 Bytes
438ec1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import APIRouter, status
from fastapi.responses import JSONResponse
import platform
import psutil

# Initialize the router for this feature
router = APIRouter(
    prefix="/server-status",
    tags=["Server Info"],
    responses={404: {"description": "Not found"}},
)

@router.get(
    "/full-info", 
    summary="Get complete server/PC status and information",
    status_code=status.HTTP_200_OK
)
def get_server_status():
    """
    Retrieves detailed hardware and software status of the server.
    """
    
    # --- System Information ---
    system_info = {
        "system": platform.system(),
        "node_name": platform.node(),
        "release": platform.release(),
        "version": platform.version(),
        "machine": platform.machine(),
        "processor": platform.processor(),
    }
    
    # --- CPU Information ---
    cpu_info = {
        "physical_cores": psutil.cpu_count(logical=False),
        "total_cores": psutil.cpu_count(logical=True),
        "cpu_usage_percent": psutil.cpu_percent(interval=1),
        "cpu_frequency_mhz": f"{psutil.cpu_freq().current:.2f}",
    }

    # --- Memory Information (RAM) ---
    virtual_memory = psutil.virtual_memory()
    memory_info = {
        "total_gb": f"{virtual_memory.total / (1024**3):.2f}",
        "available_gb": f"{virtual_memory.available / (1024**3):.2f}",
        "used_gb": f"{virtual_memory.used / (1024**3):.2f}",
        "usage_percent": virtual_memory.percent,
    }
    
    # --- Disk Usage Information (Root partition) ---
    disk_usage = psutil.disk_usage('/')
    disk_info = {
        "total_gb": f"{disk_usage.total / (1024**3):.2f}",
        "used_gb": f"{disk_usage.used / (1024**3):.2f}",
        "free_gb": f"{disk_usage.free / (1024**3):.2f}",
        "usage_percent": disk_usage.percent,
    }
    
    # --- Network Information (Simple) ---
    network_info = {
        "bytes_sent_mb": f"{psutil.net_io_counters().bytes_sent / (1024**2):.2f}",
        "bytes_recv_mb": f"{psutil.net_io_counters().bytes_recv / (1024**2):.2f}",
    }

    response_data = {
        "system_information": system_info,
        "cpu_status": cpu_info,
        "memory_status": memory_info,
        "disk_status": disk_info,
        "network_io": network_info,
    }

    return JSONResponse(content=response_data, status_code=status.HTTP_200_OK)