File size: 1,029 Bytes
a2b82e9 |
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 |
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():
# Get CPU usage
cpu_usage = psutil.cpu_percent()
# Get memory usage
memory_usage = psutil.virtual_memory().percent
# Get disk usage
disk_usage = psutil.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": 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,
},
}
# Convert the dictionary to a nicely formatted JSON string
json_string = json.dumps(metrics, indent=4)
return json_string
|