From 4989314e2bdc617b48954c0fbe302947f64ae1a3 Mon Sep 17 00:00:00 2001 From: benya Date: Sun, 8 Feb 2026 01:21:25 +0300 Subject: [PATCH] Tune load thresholds for status and alerts --- config.example.yaml | 1 + handlers/status.py | 7 +++++-- services/alerts.py | 25 +++++++++++++++++-------- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index d258298..b812728 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -11,6 +11,7 @@ paths: thresholds: disk_warn: 80 load_warn: 2.0 + high_load_warn: 3.0 alerts: enabled: true diff --git a/handlers/status.py b/handlers/status.py index 1f00646..f6a6b95 100644 --- a/handlers/status.py +++ b/handlers/status.py @@ -21,10 +21,13 @@ async def cmd_status(msg: Message): minutes, _ = divmod(rem, 60) load1 = psutil.getloadavg()[0] + 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)) + cpu_icon = "🟢" - if load1 > 2.0: + if load1 > high_warn: cpu_icon = "🔴" - elif load1 > 1.0: + elif load1 > load_warn: cpu_icon = "🟡" mem = psutil.virtual_memory() diff --git a/services/alerts.py b/services/alerts.py index 69ce2d6..5150396 100644 --- a/services/alerts.py +++ b/services/alerts.py @@ -13,9 +13,10 @@ async def monitor_resources(cfg, notify, bot, chat_id): disk_warn = int(cfg.get("thresholds", {}).get("disk_warn", 80)) 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} - state = {"disk_high": False, "load_high": False, "disk_na": False} + state = {"disk_high": False, "disk_na": False, "load_level": 0} while True: now = time.time() @@ -42,15 +43,23 @@ async def monitor_resources(cfg, notify, bot, chat_id): state["disk_high"] = False load = psutil.getloadavg()[0] - if load >= load_warn: - if not state["load_high"] or now - last_sent["load"] >= cooldown: - await notify(bot, chat_id, f"🟡 Load high: {load:.2f}") - state["load_high"] = True - last_sent["load"] = now + if load >= high_warn: + level = 2 + elif load >= load_warn: + level = 1 else: - if state["load_high"] and notify_recovery: + level = 0 + + if level == 0: + if state["load_level"] > 0 and notify_recovery: await notify(bot, chat_id, f"🟢 Load OK: {load:.2f}") - state["load_high"] = False + state["load_level"] = 0 + else: + if level != state["load_level"] or now - last_sent["load"] >= cooldown: + icon = "🔴" if level == 2 else "🟡" + await notify(bot, chat_id, f"{icon} Load high: {load:.2f}") + last_sent["load"] = now + state["load_level"] = level await asyncio.sleep(interval)