From 5f3c9184b1fbaf1b9698741df9e12e3c719c5056 Mon Sep 17 00:00:00 2001 From: benya Date: Sat, 7 Feb 2026 23:59:49 +0300 Subject: [PATCH] Alert on container health changes --- services/docker.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/services/docker.py b/services/docker.py index 2f8f1be..a14517d 100644 --- a/services/docker.py +++ b/services/docker.py @@ -103,25 +103,33 @@ async def docker_watchdog(container_map, notify, bot, chat_id): while True: if not last: for alias, real in container_map.items(): - rc, state = await docker_cmd( - ["inspect", "-f", "{{.State.Status}}", real], + rc, raw = await docker_cmd( + ["inspect", "-f", "{{.State.Status}}|{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}", real], timeout=10 ) if rc != 0: - state = "error" - last[alias] = state.strip() + last[alias] = ("error", "n/a") + continue + parts = raw.strip().split("|", 1) + status = parts[0] if parts else "unknown" + health = parts[1] if len(parts) > 1 else "n/a" + last[alias] = (status, health) await asyncio.sleep(120) continue for alias, real in container_map.items(): - rc, state = await docker_cmd( - ["inspect", "-f", "{{.State.Status}}", real], + rc, raw = await docker_cmd( + ["inspect", "-f", "{{.State.Status}}|{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}", real], timeout=10 ) if rc != 0: - state = "error" - state = state.strip() - if last.get(alias) != state: - if state != "running": + status, health = "error", "n/a" + else: + parts = raw.strip().split("|", 1) + status = parts[0] if parts else "unknown" + health = parts[1] if len(parts) > 1 else "n/a" + + if last.get(alias) != (status, health): + if status != "running": kb = InlineKeyboardMarkup( inline_keyboard=[[ InlineKeyboardButton( @@ -132,10 +140,12 @@ async def docker_watchdog(container_map, notify, bot, chat_id): ) await bot.send_message( chat_id, - f"🐳 {alias}: {state}", + f"🐳 {alias}: {status}", reply_markup=kb, ) + elif health not in ("healthy", "n/a"): + await notify(bot, chat_id, f"⚠️ {alias} health: {health}") else: - await notify(bot, chat_id, f"🐳 {alias}: {state}") - last[alias] = state + await notify(bot, chat_id, f"🐳 {alias}: {status}") + last[alias] = (status, health) await asyncio.sleep(120)