from fastapi import APIRouter, Request import psutil import json from App import bot monitor_router = APIRouter(tags=["monitor"]) def convert_to_gb(bytes_value): gb_value = bytes_value / (1024 * 1024 * 1024) return round(gb_value, 2) @monitor_router.get("/metrics") def get_metrics(): # Get CPU usage cpu_usage = psutil.cpu_percent() # Get memory usage memory_usage = psutil.virtual_memory() memory_total = convert_to_gb(memory_usage.total) memory_available = convert_to_gb(memory_usage.available) memory_percent = memory_usage.percent # Get disk usage disk_usage = psutil.disk_usage("/") disk_total = convert_to_gb(disk_usage.total) disk_used = convert_to_gb(disk_usage.used) disk_percent = disk_usage.percent # Get network statistics network_stats = psutil.net_io_counters() # Create a dictionary with the metrics metrics = { "cpu_usage": cpu_usage, "memory_usage": { "total": memory_total, "available": memory_available, "percent": memory_percent, }, "disk_usage": {"total": disk_total, "used": disk_used, "percent": disk_percent}, "network_stats": { "bytes_sent": network_stats.bytes_sent, "bytes_received": network_stats.bytes_recv, "packets_sent": network_stats.packets_sent, "packets_received": network_stats.packets_recv, }, } return metrics