|
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(): |
|
|
|
cpu_usage = psutil.cpu_percent() |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
network_stats = psutil.net_io_counters() |
|
|
|
|
|
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 |
|
|