Alert on container health changes

This commit is contained in:
2026-02-07 23:59:49 +03:00
parent 4c5c085832
commit 5f3c9184b1

View File

@@ -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)