Refactor bot and integrate services
This commit is contained in:
69
services/system.py
Normal file
69
services/system.py
Normal file
@@ -0,0 +1,69 @@
|
||||
import psutil
|
||||
|
||||
|
||||
def format_disks() -> str:
|
||||
parts = psutil.disk_partitions(all=False)
|
||||
lines = []
|
||||
|
||||
skip_prefixes = (
|
||||
"/snap",
|
||||
"/proc",
|
||||
"/sys",
|
||||
"/run",
|
||||
"/boot/efi",
|
||||
)
|
||||
|
||||
for p in parts:
|
||||
mp = p.mountpoint
|
||||
if mp.startswith(skip_prefixes):
|
||||
continue
|
||||
|
||||
try:
|
||||
usage = psutil.disk_usage(mp)
|
||||
except PermissionError:
|
||||
continue
|
||||
|
||||
icon = "🟢"
|
||||
if usage.percent > 90:
|
||||
icon = "🔴"
|
||||
elif usage.percent > 80:
|
||||
icon = "🟡"
|
||||
|
||||
lines.append(
|
||||
f"{icon} **{mp}**: "
|
||||
f"{usage.used // (1024**3)} / {usage.total // (1024**3)} GiB "
|
||||
f"({usage.percent}%)"
|
||||
)
|
||||
|
||||
if not lines:
|
||||
return "💽 Disks: n/a"
|
||||
|
||||
return "💽 **Disks**\n" + "\n".join(lines)
|
||||
|
||||
|
||||
def worst_disk_usage() -> tuple[int | None, str | None]:
|
||||
parts = psutil.disk_partitions(all=False)
|
||||
skip_prefixes = (
|
||||
"/snap",
|
||||
"/proc",
|
||||
"/sys",
|
||||
"/run",
|
||||
"/boot/efi",
|
||||
)
|
||||
|
||||
worst_percent = None
|
||||
worst_mount = None
|
||||
|
||||
for p in parts:
|
||||
mp = p.mountpoint
|
||||
if mp.startswith(skip_prefixes):
|
||||
continue
|
||||
try:
|
||||
usage = psutil.disk_usage(mp)
|
||||
except PermissionError:
|
||||
continue
|
||||
if worst_percent is None or usage.percent > worst_percent:
|
||||
worst_percent = int(usage.percent)
|
||||
worst_mount = mp
|
||||
|
||||
return worst_percent, worst_mount
|
||||
Reference in New Issue
Block a user