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:
@@ -181,3 +181,45 @@ def detect_offline_devices(db: Session = Depends(get_db)):
|
||||
"count": len(offline_devices),
|
||||
"devices": offline_devices
|
||||
}
|
||||
|
||||
|
||||
# ========== 飞书告警通知 ==========
|
||||
|
||||
@router.get("/notify/feishu/status", summary="获取飞书通知配置状态")
|
||||
def get_feishu_status():
|
||||
"""查看飞书通知是否已配置(不返回密钥等敏感信息)"""
|
||||
from app.services.feishu_service import get_feishu_service
|
||||
svc = get_feishu_service()
|
||||
return {
|
||||
"enabled": svc.enabled,
|
||||
"configured": bool(svc.webhook_url),
|
||||
"webhook_set": bool(svc.webhook_url),
|
||||
"secret_set": bool(svc.secret),
|
||||
"min_severity": svc.min_severity,
|
||||
"alert_types": svc.alert_types,
|
||||
"cani_push": svc.is_configured(),
|
||||
}
|
||||
|
||||
|
||||
@router.post("/notify/feishu/test", summary="发送飞书测试告警")
|
||||
def test_feishu_notification():
|
||||
"""发送一条测试告警到飞书,验证 webhook 配置是否正确"""
|
||||
from app.services.feishu_service import get_feishu_service
|
||||
svc = get_feishu_service()
|
||||
if not svc.is_configured():
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail="飞书通知未启用或未配置 webhook。请在 backend/.env 设置 FEISHU_ENABLED=true 和 FEISHU_WEBHOOK_URL"
|
||||
)
|
||||
ok = svc.send_alert({
|
||||
"alert_type": "new_device_detected",
|
||||
"severity": "warning",
|
||||
"title": "飞书通知测试",
|
||||
"message": "这是一条测试告警,用于验证飞书机器人 webhook 配置是否正常。",
|
||||
"ip_address_str": "172.16.0.1",
|
||||
"mac_address": "00:11:22:33:44:55",
|
||||
})
|
||||
if ok:
|
||||
return {"message": "飞书测试通知发送成功", "success": True}
|
||||
raise HTTPException(status_code=500, detail="飞书测试通知发送失败,请检查 webhook 地址和日志")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user