File size: 1,472 Bytes
a2b82e9 1c7b1fc a2b82e9 45c349c 1c7b1fc 45c349c a2b82e9 45c349c 1c7b1fc 45c349c a2b82e9 45c349c a2b82e9 695d52f |
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 41 42 43 44 45 46 47 48 49 50 51 52 |
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
|