|
from fastapi import APIRouter, Request |
|
import psutil |
|
import json |
|
from App import bot |
|
|
|
monitor_router = APIRouter(tags=["monitor"]) |
|
|
|
|
|
@monitor_router.get("/metrics") |
|
def get_metrics(): |
|
|
|
cpu_usage = psutil.cpu_percent() |
|
|
|
|
|
memory_usage = psutil.virtual_memory().percent |
|
|
|
|
|
disk_usage = psutil.disk_usage("/").percent |
|
|
|
|
|
network_stats = psutil.net_io_counters() |
|
|
|
|
|
metrics = { |
|
"cpu_usage": cpu_usage, |
|
"memory_usage": memory_usage, |
|
"disk_usage": disk_usage, |
|
"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, |
|
}, |
|
} |
|
|
|
|
|
json_string = json.dumps(metrics, indent=4) |
|
|
|
return json_string |
|
|