Add disk usage snapshot reports

This commit is contained in:
2026-02-08 02:21:15 +03:00
parent 3df9db3bf7
commit c34a142698
5 changed files with 70 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ import time
import psutil
from system_checks import list_disks, smart_health, disk_temperature
from services.system import worst_disk_usage
from services.disk_report import build_disk_report
async def monitor_resources(cfg, notify, bot, chat_id):
@@ -12,10 +13,12 @@ async def monitor_resources(cfg, notify, bot, chat_id):
notify_recovery = bool(alerts_cfg.get("notify_recovery", True))
disk_warn = int(cfg.get("thresholds", {}).get("disk_warn", 80))
snapshot_warn = int(cfg.get("disk_report", {}).get("threshold", disk_warn))
snapshot_cooldown = int(cfg.get("disk_report", {}).get("cooldown_sec", 21600))
load_warn = float(cfg.get("thresholds", {}).get("load_warn", 2.0))
high_warn = float(cfg.get("thresholds", {}).get("high_load_warn", load_warn * 1.5))
last_sent = {"disk": 0.0, "load": 0.0, "disk_na": 0.0}
last_sent = {"disk": 0.0, "load": 0.0, "disk_na": 0.0, "disk_report": 0.0}
state = {"disk_high": False, "disk_na": False, "load_level": 0}
while True:
@@ -42,6 +45,11 @@ async def monitor_resources(cfg, notify, bot, chat_id):
await notify(bot, chat_id, f"🟢 Disk usage OK ({usage}% {mount})")
state["disk_high"] = False
if usage >= snapshot_warn and now - last_sent["disk_report"] >= snapshot_cooldown:
report = await build_disk_report(cfg, mount or "/", usage)
await notify(bot, chat_id, f"📦 Disk snapshot\n\n{report}")
last_sent["disk_report"] = now
load = psutil.getloadavg()[0]
if load >= high_warn:
level = 2

38
services/disk_report.py Normal file
View File

@@ -0,0 +1,38 @@
import os
from typing import Any
from services.runner import run_cmd
def _top_dirs_cmd(path: str, limit: int) -> list[str]:
return ["bash", "-lc", f"du -xhd1 {path} 2>/dev/null | sort -h | tail -n {limit}"]
async def build_disk_report(cfg: dict[str, Any], mount: str, usage: int) -> str:
limit = int(cfg.get("disk_report", {}).get("top_dirs", 8))
lines = ["🧱 Disk report", f"💽 {mount}: {usage}%"]
rc, out = await run_cmd(_top_dirs_cmd(mount, limit), timeout=30)
if rc == 0 and out.strip():
lines.append("")
lines.append("Top directories:")
lines.append(out.strip())
docker_dir = cfg.get("disk_report", {}).get("docker_dir", "/var/lib/docker")
if docker_dir and os.path.exists(docker_dir):
rc2, out2 = await run_cmd(_top_dirs_cmd(docker_dir, limit), timeout=30)
if rc2 == 0 and out2.strip():
lines.append("")
lines.append(f"Docker dir: {docker_dir}")
lines.append(out2.strip())
logs_dir = cfg.get("disk_report", {}).get("logs_dir", "/var/log")
if logs_dir and os.path.exists(logs_dir):
rc3, out3 = await run_cmd(_top_dirs_cmd(logs_dir, limit), timeout=30)
if rc3 == 0 and out3.strip():
lines.append("")
lines.append(f"Logs dir: {logs_dir}")
lines.append(out3.strip())
return "\n".join(lines)