告警中心 - 新增统一告警(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>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
// 审计日志按保留天数清理 — 操作审计 / 登录日志 / 终端会话 / 会话命令 // Logan.Li
|
|
package auditcleaner
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"ezops/model"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Result 单次清理统计 // Logan.Li
|
|
type Result struct {
|
|
RetentionDays int `json:"retention_days"`
|
|
CutoffTime time.Time `json:"cutoff_time"`
|
|
AuditLogs int64 `json:"audit_logs"`
|
|
LoginLogs int64 `json:"login_logs"`
|
|
TerminalSessions int64 `json:"terminal_sessions"`
|
|
SessionCommands int64 `json:"session_commands"`
|
|
Total int64 `json:"total"`
|
|
}
|
|
|
|
// SummaryJSON 生成可持久化的摘要 JSON // Logan.Li
|
|
func (r Result) SummaryJSON() string {
|
|
b, _ := json.Marshal(r)
|
|
return string(b)
|
|
}
|
|
|
|
// GetRetentionDays 读取当前保留天数,无配置时默认 180 天 // Logan.Li
|
|
func GetRetentionDays(db *gorm.DB) int {
|
|
var s model.AuditSettings
|
|
if err := db.First(&s).Error; err != nil || s.RetentionDays <= 0 {
|
|
return 180
|
|
}
|
|
return s.RetentionDays
|
|
}
|
|
|
|
// Run 删除 cutoff 之前的审计相关记录 // Logan.Li
|
|
func Run(db *gorm.DB, retentionDays int) (Result, error) {
|
|
if !model.IsValidRetentionDays(retentionDays) {
|
|
return Result{}, fmt.Errorf("无效的保留天数: %d", retentionDays)
|
|
}
|
|
cutoff := time.Now().AddDate(0, 0, -retentionDays)
|
|
res := Result{RetentionDays: retentionDays, CutoffTime: cutoff}
|
|
|
|
if r := db.Where("created_at < ?", cutoff).Delete(&model.SessionCommand{}); r.Error != nil {
|
|
return res, r.Error
|
|
} else {
|
|
res.SessionCommands = r.RowsAffected
|
|
}
|
|
|
|
if r := db.Where("start_time < ?", cutoff).Delete(&model.TerminalSession{}); r.Error != nil {
|
|
return res, r.Error
|
|
} else {
|
|
res.TerminalSessions = r.RowsAffected
|
|
}
|
|
|
|
if r := db.Where("login_at < ?", cutoff).Delete(&model.LoginLog{}); r.Error != nil {
|
|
return res, r.Error
|
|
} else {
|
|
res.LoginLogs = r.RowsAffected
|
|
}
|
|
|
|
if r := db.Where("created_at < ?", cutoff).Delete(&model.AuditLog{}); r.Error != nil {
|
|
return res, r.Error
|
|
} else {
|
|
res.AuditLogs = r.RowsAffected
|
|
}
|
|
|
|
res.Total = res.AuditLogs + res.LoginLogs + res.TerminalSessions + res.SessionCommands
|
|
return res, nil
|
|
}
|
|
|
|
// RunFromSettings 按库中配置执行清理 // Logan.Li
|
|
func RunFromSettings(db *gorm.DB) (Result, error) {
|
|
return Run(db, GetRetentionDays(db))
|
|
} |