feat: per-alert notification toggles with switch UI

config.py:
- Add NOTIFY_ALERT_CPU/MEM/DISK, NOTIFY_RECOVERY, NOTIFY_TIME_DRIFT,
  NOTIFY_SYSTEM_REDIS, NOTIFY_SYSTEM_MYSQL, NOTIFY_RESTART settings
  (string 'true'/'false', loaded from MySQL settings table at startup)

telegram/__init__.py:
- _notify_enabled(): check setting string ('false'/'0'/'no'/'off' = disabled)
- send_telegram_alert(): checks NOTIFY_ALERT_{CPU|MEM|DISK} per alert_type
- send_telegram_recovery(): checks NOTIFY_RECOVERY
- send_telegram_system_alert(): new notify_key param, checks matching setting
- send_telegram_restart_*(): check NOTIFY_RESTART

self_monitor.py:
- Pass notify_key='NOTIFY_SYSTEM_REDIS/MYSQL' to send_telegram_system_alert

server/api/agent.py:
- Pass notify_key='NOTIFY_TIME_DRIFT' to time-drift alert

settings.html:
- New '告警通知项目' section with 8 toggle switches (pill style)
- Grouped by category: 服务器资源 / 服务器 / 系统
- Optimistic toggle with revert on API failure
- renderNotifyToggles() reads _allSettings and toggleNotify() saves instantly

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name
2026-05-23 17:12:07 +08:00
parent 08d0baf762
commit b9e8db7a3f
5 changed files with 145 additions and 14 deletions
+5 -4
View File
@@ -225,10 +225,11 @@ async def receive_heartbeat(
try:
from server.infrastructure.telegram import send_telegram_system_alert
await send_telegram_system_alert(
f"🕐 时钟偏差过大 — 服务器 #{server_id} ({server_name})\n"
f"偏差 {time_drift_seconds}s(≥60s 影响 TOTP / cron 精度)\n"
f"请检查 NTP 配置"
)
f"🕐 时钟偏差过大 — 服务器 #{server_id} ({server_name})\n"
f"偏差 {time_drift_seconds}s(≥60s 影响 TOTP / cron 精度)\n"
f"请检查 NTP 配置",
notify_key="NOTIFY_TIME_DRIFT",
)
except Exception:
logger.debug("Failed to send drift alert via Telegram")