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: while True:
if not last: if not last:
for alias, real in container_map.items(): for alias, real in container_map.items():
rc, state = await docker_cmd( rc, raw = await docker_cmd(
["inspect", "-f", "{{.State.Status}}", real], ["inspect", "-f", "{{.State.Status}}|{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}", real],
timeout=10 timeout=10
) )
if rc != 0: if rc != 0:
state = "error" last[alias] = ("error", "n/a")
last[alias] = state.strip() 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) await asyncio.sleep(120)
continue continue
for alias, real in container_map.items(): for alias, real in container_map.items():
rc, state = await docker_cmd( rc, raw = await docker_cmd(
["inspect", "-f", "{{.State.Status}}", real], ["inspect", "-f", "{{.State.Status}}|{{if .State.Health}}{{.State.Health.Status}}{{else}}n/a{{end}}", real],
timeout=10 timeout=10
) )
if rc != 0: if rc != 0:
state = "error" status, health = "error", "n/a"
state = state.strip() else:
if last.get(alias) != state: parts = raw.strip().split("|", 1)
if state != "running": 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( kb = InlineKeyboardMarkup(
inline_keyboard=[[ inline_keyboard=[[
InlineKeyboardButton( InlineKeyboardButton(
@@ -132,10 +140,12 @@ async def docker_watchdog(container_map, notify, bot, chat_id):
) )
await bot.send_message( await bot.send_message(
chat_id, chat_id,
f"🐳 {alias}: {state}", f"🐳 {alias}: {status}",
reply_markup=kb, reply_markup=kb,
) )
elif health not in ("healthy", "n/a"):
await notify(bot, chat_id, f"⚠️ {alias} health: {health}")
else: else:
await notify(bot, chat_id, f"🐳 {alias}: {state}") await notify(bot, chat_id, f"🐳 {alias}: {status}")
last[alias] = state last[alias] = (status, health)
await asyncio.sleep(120) await asyncio.sleep(120)