feat(alerts): 告警中心增加飞书机器人告警推送功能

1. 新增 feishu_service.py: 飞书自定义机器人 webhook 推送
   - 支持卡片消息(interactive),按告警级别着色
   - 支持签名校验(HMAC-SHA256)
   - 支持最低级别过滤 + 告警类型白名单
2. alert_service.create_alert 创建新告警时自动推飞书
   - 仅新告警推送,去重告警不刷屏
   - 推送失败不影响告警创建
3. alerts.py 新增两个接口:
   - GET /alerts/notify/feishu/status 查看配置状态
   - POST /alerts/notify/feishu/test 发送测试告警
4. config.py + .env.example 增加 FEISHU_* 配置项
   (WEBHOOK_URL / SECRET / ENABLED / MIN_SEVERITY / ALERT_TYPES)
This commit is contained in:
Your Name
2026-08-12 13:10:08 +08:00
parent 3fd6ba3baf
commit 717fa31456
5 changed files with 315 additions and 0 deletions
+28
View File
@@ -12,6 +12,18 @@ from app.models.alert import (
from app.models.network import Network, IPAddress
from app.models.snmp import NetworkDevice, ARPEntry
# 飞书告警通知(延迟导入避免循环依赖)
_feishu_service = None
def _get_feishu_service():
global _feishu_service
if _feishu_service is None:
from app.services.feishu_service import get_feishu_service
_feishu_service = get_feishu_service()
return _feishu_service
logger = logging.getLogger(__name__)
@@ -70,6 +82,22 @@ class AlertService:
db.refresh(alert)
logger.info(f"创建告警: {alert_type} - {title}")
# 飞书告警通知(仅新创建的告警推送;existing 去重返回的不会走到这里,避免刷屏)
try:
_get_feishu_service().send_alert({
"alert_type": alert_type.value if hasattr(alert_type, "value") else str(alert_type),
"severity": severity.value if hasattr(severity, "value") else str(severity),
"title": title,
"message": message,
"ip_address_str": ip_address_str,
"mac_address": mac_address,
"conflicting_mac": conflicting_mac,
"usage_percent": usage_percent,
})
except Exception as e:
logger.error(f"飞书告警通知失败(不影响告警创建): {e}")
return alert
@staticmethod