Files
vps_monitoring/server/services/alerter.py
Андрей Бобырев cc01667879 feat: add Synology NAS, Home Assistant monitoring, multi-channel notifications
New tabs:
- Synology NAS: CPU, RAM, temp, volumes, VMs, Docker containers
- Home Assistant: sensors, automations, persons, climate, battery, doors

New features:
- Personalized PC agent download with custom name
- Notification settings: per-category Telegram/Email/WhatsApp toggles
- Email via SMTP, WhatsApp via CallMeBot API
- Test notification buttons for each channel
- Unified notifier replaces direct Telegram calls in alerter

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-05-20 13:59:57 +03:00

77 lines
2.5 KiB
Python

import logging
from datetime import datetime
from typing import Dict
from server.config import load_settings, load_servers
from server.services.monitor import get_all_metrics
logger = logging.getLogger(__name__)
previous_states: Dict[str, bool] = {}
async def check_alerts():
"""Check metrics against thresholds and send alerts."""
from server.services.notifier import send_notification
from server.api.auth_routes import mute_until
# Check mute
if mute_until and datetime.now() < mute_until:
return
settings = load_settings()
servers = load_servers()
metrics = get_all_metrics()
cpu_threshold = settings.get("alert_cpu_threshold", 90)
ram_threshold = settings.get("alert_ram_threshold", 90)
disk_threshold = settings.get("alert_disk_threshold", 90)
for srv in servers:
host = srv["host"]
m = metrics.get(host, {})
name = srv.get("name", host)
current_online = m.get("online", False)
prev_online = previous_states.get(host)
# Online/Offline state change
if prev_online is not None and prev_online != current_online:
if current_online:
await send_notification(
f"🟢 **{name}** ({host}) — снова онлайн",
subject=f"{name} Online", category="servers"
)
else:
await send_notification(
f"🔴 **{name}** ({host}) — OFFLINE!",
subject=f"{name} OFFLINE", category="servers"
)
previous_states[host] = current_online
if not current_online:
continue
# Threshold alerts
cpu = m.get("cpu_percent", 0)
if cpu >= cpu_threshold:
await send_notification(
f"⚠️ **{name}** — CPU: {cpu}% (порог: {cpu_threshold}%)",
subject=f"{name} CPU {cpu}%", category="servers"
)
ram = m.get("ram_percent", 0)
if ram >= ram_threshold:
await send_notification(
f"⚠️ **{name}** — RAM: {ram}% (порог: {ram_threshold}%)",
subject=f"{name} RAM {ram}%", category="servers"
)
disk = m.get("disk_percent", 0)
if disk >= disk_threshold:
await send_notification(
f"⚠️ **{name}** — Disk: {disk}% (порог: {disk_threshold}%)",
subject=f"{name} Disk {disk}%", category="servers"
)