告警中心 - 新增统一告警(alert_unified)、监控告警规则(monitor_alert_rule)、指标目录(alert_metric_catalog) - 通知策略与消息分发(alert_notify_policy/dispatch/message)、静默规则 - 告警引擎按阈值/SNMP 拆分(alert_engine_monitor/snmp) 监控采集 - SNMP 常量/模板与历史指标(snmp templates/constants、snmp_metrics_history、snmp_collect) - 中间件监控 Redis 采集与种子(middleware_monitor_redis/seed) - 站点探测 pkg(siteprobe) + 站点视图(site_view) 自动化任务 - cron 调度器(cronscheduler)、脚本运行器(scriptrunner)、系统任务(systask)及运行记录(cron_job_run、system_task) 审计与凭证 - 审计设置(audit_setting) + 历史清理(auditcleaner) 前端 - 监控页面(host/middleware/site/snmp)拆分为子模块并大屏化 - 告警中心多 Tab(规则/指标目录/通知策略/静默)、任务管理组件重构、Agent CLI Markdown 渲染 - 移除旧脚本卡片/详情/日志组件,统一为 ScriptTable/ScriptCodeEditor/TerminalOutput Co-Authored-By: Claude <noreply@anthropic.com>
121 lines
3.2 KiB
Go
121 lines
3.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"time"
|
|
|
|
"ezops/model"
|
|
"ezops/pkg/auditcleaner"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// AuditSettingsHandler 审计策略配置 // Logan.Li
|
|
type AuditSettingsHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewAuditSettingsHandler(db *gorm.DB) *AuditSettingsHandler {
|
|
return &AuditSettingsHandler{db: db}
|
|
}
|
|
|
|
func (h *AuditSettingsHandler) getOrCreate() (model.AuditSettings, error) {
|
|
var s model.AuditSettings
|
|
err := h.db.First(&s).Error
|
|
if err == nil {
|
|
return s, nil
|
|
}
|
|
if err != gorm.ErrRecordNotFound {
|
|
return s, err
|
|
}
|
|
s = model.AuditSettings{RetentionDays: 180, RecordAll: true}
|
|
return s, h.db.Create(&s).Error
|
|
}
|
|
|
|
// GetAuditSettings 获取审计策略 // Logan.Li
|
|
// GET /api/v1/admin/audit-settings
|
|
func (h *AuditSettingsHandler) GetAuditSettings(c *gin.Context) {
|
|
s, err := h.getOrCreate()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "读取配置失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: "success",
|
|
Data: gin.H{
|
|
"retention_days": s.RetentionDays,
|
|
"record_all": s.RecordAll,
|
|
"last_cleanup_at": s.LastCleanupAt,
|
|
"last_cleanup_summary": s.LastCleanupSummary,
|
|
"allowed_retention_days": model.AllowedAuditRetentionDays,
|
|
},
|
|
})
|
|
}
|
|
|
|
// ApplyAuditSettings 应用审计策略并立即清理超期日志 // Logan.Li
|
|
// PUT /api/v1/admin/audit-settings
|
|
func (h *AuditSettingsHandler) ApplyAuditSettings(c *gin.Context) {
|
|
var req struct {
|
|
RetentionDays int `json:"retention_days" binding:"required"`
|
|
RecordAll bool `json:"record_all"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: "请求参数错误"})
|
|
return
|
|
}
|
|
if !model.IsValidRetentionDays(req.RetentionDays) {
|
|
c.JSON(http.StatusBadRequest, model.Response{
|
|
Code: 400,
|
|
Message: "保留天数仅支持 7 / 15 / 30 / 90 / 180 / 365",
|
|
})
|
|
return
|
|
}
|
|
|
|
s, err := h.getOrCreate()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "读取配置失败"})
|
|
return
|
|
}
|
|
|
|
s.RetentionDays = req.RetentionDays
|
|
s.RecordAll = req.RecordAll
|
|
|
|
cleanResult, cleanErr := auditcleaner.Run(h.db, req.RetentionDays)
|
|
now := time.Now()
|
|
s.LastCleanupAt = &now
|
|
s.LastCleanupSummary = cleanResult.SummaryJSON()
|
|
|
|
if err := h.db.Save(&s).Error; err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "保存配置失败"})
|
|
return
|
|
}
|
|
|
|
msg := fmt.Sprintf("已应用保留 %d 天策略", req.RetentionDays)
|
|
if cleanErr != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{
|
|
Code: 500,
|
|
Message: "配置已保存,但立即清理失败: " + cleanErr.Error(),
|
|
})
|
|
return
|
|
}
|
|
if cleanResult.Total > 0 {
|
|
msg += fmt.Sprintf(",已清理 %d 条超期记录", cleanResult.Total)
|
|
} else {
|
|
msg += ",暂无超期记录需清理"
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Response{
|
|
Code: 200,
|
|
Message: msg,
|
|
Data: gin.H{
|
|
"retention_days": s.RetentionDays,
|
|
"record_all": s.RecordAll,
|
|
"last_cleanup_at": s.LastCleanupAt,
|
|
"last_cleanup_summary": s.LastCleanupSummary,
|
|
"cleanup": cleanResult,
|
|
},
|
|
})
|
|
} |