tranny / App /Monitor /monitorRoutes.py
Mbonea's picture
metrics
a2b82e9
raw
history blame
1.03 kB
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