Add network snapshot to status
This commit is contained in:
@@ -34,6 +34,7 @@ async def cmd_status(msg: Message):
|
||||
cpu_percent = psutil.cpu_percent(interval=None)
|
||||
|
||||
disks = format_disks()
|
||||
net_lines = await _network_snapshot()
|
||||
|
||||
await msg.answer(
|
||||
"📊 **Server status**\n\n"
|
||||
@@ -42,7 +43,8 @@ async def cmd_status(msg: Message):
|
||||
f"{cpu_icon} **Load (1m):** {load1:.2f}\n"
|
||||
f"🧮 **CPU:** {cpu_percent:.0f}%\n"
|
||||
f"🧠 **RAM:** {mem.used // (1024**3)} / {mem.total // (1024**3)} GiB ({mem.percent}%)\n\n"
|
||||
f"{disks}",
|
||||
f"{disks}\n\n"
|
||||
f"{net_lines}",
|
||||
reply_markup=menu_kb,
|
||||
parse_mode="Markdown",
|
||||
)
|
||||
@@ -72,3 +74,41 @@ async def h(msg: Message):
|
||||
async def st(msg: Message):
|
||||
if is_admin_msg(msg):
|
||||
await cmd_status(msg)
|
||||
|
||||
|
||||
def _rate_str(value: float) -> str:
|
||||
if value >= 1024 * 1024:
|
||||
return f"{value / (1024 * 1024):.2f} MiB/s"
|
||||
if value >= 1024:
|
||||
return f"{value / 1024:.1f} KiB/s"
|
||||
return f"{value:.0f} B/s"
|
||||
|
||||
|
||||
async def _network_snapshot(interval: float = 1.0) -> str:
|
||||
start = psutil.net_io_counters(pernic=True)
|
||||
await asyncio.sleep(interval)
|
||||
end = psutil.net_io_counters(pernic=True)
|
||||
|
||||
rows = []
|
||||
for nic, s in end.items():
|
||||
if nic.startswith("lo"):
|
||||
continue
|
||||
e = start.get(nic)
|
||||
if not e:
|
||||
continue
|
||||
rx = max(0, s.bytes_recv - e.bytes_recv)
|
||||
tx = max(0, s.bytes_sent - e.bytes_sent)
|
||||
err = max(0, (s.errin - e.errin) + (s.errout - e.errout))
|
||||
score = rx + tx + (err * 1024)
|
||||
rows.append((score, nic, rx, tx, err))
|
||||
|
||||
rows.sort(reverse=True)
|
||||
top = rows[:3]
|
||||
if not top:
|
||||
return "📡 **Network (1s):** no data"
|
||||
|
||||
lines = ["📡 **Network (1s):**"]
|
||||
for _score, nic, rx, tx, err in top:
|
||||
err_part = f", err {err}" if err else ""
|
||||
lines.append(f"- {nic}: RX {_rate_str(rx / interval)}, TX {_rate_str(tx / interval)}{err_part}")
|
||||
return "\n".join(lines)
|
||||
|
||||
Reference in New Issue
Block a user