multi-use-api / app /routers /testers /server_status.py
sameerbanchhor's picture
Upload folder using huggingface_hub
438ec1c verified
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)