Tag incidents with categories for summaries
This commit is contained in:
@@ -32,7 +32,7 @@ async def _handle_alerts(msg: Message, action: str, args: list[str]):
|
|||||||
key = f"test:{level}:{int(time.time())}"
|
key = f"test:{level}:{int(time.time())}"
|
||||||
await notify(bot, msg.chat.id, f"[TEST] {level.upper()} alert", level=level, key=key, category="test")
|
await notify(bot, msg.chat.id, f"[TEST] {level.upper()} alert", level=level, key=key, category="test")
|
||||||
await msg.answer(f"Sent test alert: {level}")
|
await msg.answer(f"Sent test alert: {level}")
|
||||||
log_incident(cfg, f"alert_test level={level} by {msg.from_user.id}")
|
log_incident(cfg, f"alert_test level={level} by {msg.from_user.id}", category="test")
|
||||||
return
|
return
|
||||||
|
|
||||||
if action == "mute":
|
if action == "mute":
|
||||||
@@ -49,7 +49,7 @@ async def _handle_alerts(msg: Message, action: str, args: list[str]):
|
|||||||
until = set_mute(category, minutes * 60)
|
until = set_mute(category, minutes * 60)
|
||||||
dt = datetime.fromtimestamp(until, tz=timezone.utc).astimezone()
|
dt = datetime.fromtimestamp(until, tz=timezone.utc).astimezone()
|
||||||
await msg.answer(f"🔕 Muted {category} for {minutes}m (until {dt:%Y-%m-%d %H:%M:%S})")
|
await msg.answer(f"🔕 Muted {category} for {minutes}m (until {dt:%Y-%m-%d %H:%M:%S})")
|
||||||
log_incident(cfg, f"alert_mute category={category} minutes={minutes} by {msg.from_user.id}")
|
log_incident(cfg, f"alert_mute category={category} minutes={minutes} by {msg.from_user.id}", category=category)
|
||||||
return
|
return
|
||||||
|
|
||||||
if action == "unmute":
|
if action == "unmute":
|
||||||
@@ -59,7 +59,7 @@ async def _handle_alerts(msg: Message, action: str, args: list[str]):
|
|||||||
category = args[0].lower()
|
category = args[0].lower()
|
||||||
clear_mute(category)
|
clear_mute(category)
|
||||||
await msg.answer(f"🔔 Unmuted {category}")
|
await msg.answer(f"🔔 Unmuted {category}")
|
||||||
log_incident(cfg, f"alert_unmute category={category} by {msg.from_user.id}")
|
log_incident(cfg, f"alert_unmute category={category} by {msg.from_user.id}", category=category)
|
||||||
return
|
return
|
||||||
|
|
||||||
if action in ("list", "mutes"):
|
if action in ("list", "mutes"):
|
||||||
|
|||||||
@@ -367,7 +367,7 @@ async def schedule_backup(msg: Message):
|
|||||||
await msg.answer(f"🕓 Backup queued (#{pos})", reply_markup=backup_kb)
|
await msg.answer(f"🕓 Backup queued (#{pos})", reply_markup=backup_kb)
|
||||||
try:
|
try:
|
||||||
from services.incidents import log_incident
|
from services.incidents import log_incident
|
||||||
log_incident(cfg, f"backup_queued by {msg.from_user.id}")
|
log_incident(cfg, f"backup_queued by {msg.from_user.id}", category="backup")
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ async def cmd_docker_status(msg: Message):
|
|||||||
lines.append(f"{icon} {alias}: {status} ({up})")
|
lines.append(f"{icon} {alias}: {status} ({up})")
|
||||||
|
|
||||||
await msg.answer("\n".join(lines), reply_markup=docker_kb)
|
await msg.answer("\n".join(lines), reply_markup=docker_kb)
|
||||||
log_incident(cfg, f"docker_status by {msg.from_user.id}")
|
log_incident(cfg, f"docker_status by {msg.from_user.id}", category="docker")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# ⬅️ КРИТИЧЕСКИ ВАЖНО
|
# ⬅️ КРИТИЧЕСКИ ВАЖНО
|
||||||
@@ -122,7 +122,7 @@ async def docker_health(msg: Message):
|
|||||||
out_line = entry.get("Output", "").strip()
|
out_line = entry.get("Output", "").strip()
|
||||||
lines.append(f"- {ts} rc={exitc} {out_line}")
|
lines.append(f"- {ts} rc={exitc} {out_line}")
|
||||||
await msg.answer("\n".join(lines), reply_markup=docker_kb)
|
await msg.answer("\n".join(lines), reply_markup=docker_kb)
|
||||||
log_incident(cfg, f"docker_health alias={alias} by {msg.from_user.id}")
|
log_incident(cfg, f"docker_health alias={alias} by {msg.from_user.id}", category="docker")
|
||||||
|
|
||||||
|
|
||||||
@dp.message(F.text == "📈 Stats")
|
@dp.message(F.text == "📈 Stats")
|
||||||
|
|||||||
2
main.py
2
main.py
@@ -39,7 +39,7 @@ def _handle_async_exception(_loop, context):
|
|||||||
else:
|
else:
|
||||||
text = f"❌ {msg}"
|
text = f"❌ {msg}"
|
||||||
try:
|
try:
|
||||||
log_incident(cfg, text)
|
log_incident(cfg, text, category="system")
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
logging.getLogger("asyncio").error(text)
|
logging.getLogger("asyncio").error(text)
|
||||||
|
|||||||
@@ -44,9 +44,11 @@ def _get_logger(cfg: dict[str, Any]) -> logging.Logger:
|
|||||||
return logger
|
return logger
|
||||||
|
|
||||||
|
|
||||||
def log_incident(cfg: dict[str, Any], text: str) -> None:
|
def log_incident(cfg: dict[str, Any], text: str, category: str | None = None) -> None:
|
||||||
if not cfg.get("incidents", {}).get("enabled", True):
|
if not cfg.get("incidents", {}).get("enabled", True):
|
||||||
return
|
return
|
||||||
|
if category and "category=" not in text:
|
||||||
|
text = f"category={category} {text}"
|
||||||
logger = _get_logger(cfg)
|
logger = _get_logger(cfg)
|
||||||
logger.info(text)
|
logger.info(text)
|
||||||
|
|
||||||
|
|||||||
@@ -70,6 +70,6 @@ async def notify(
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
try:
|
try:
|
||||||
log_incident(cfg, text)
|
log_incident(cfg, text, category=category)
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
|
|||||||
Reference in New Issue
Block a user