F1 告警静默规则 [done]: - 后端 alert_silence.go: CRUD + Toggle (onetime/daily/weekly 三模式) - 告警引擎 isSilenced 集成 (alert_engine.go) - 前端 SilenceRuleTab + SilenceDialog - 路由 /alerts/silences 已注册 F2 审计日志导出 [done]: - 重写 ExportAuditLogs: 真正的 CSV 导出 (encoding/csv + UTF-8 BOM) - 支持 module/action/username/startTime/endTime 筛选 - 导出限制 10000 条防溢出,自动记录审计日志 - JSON 格式向后兼容 (format=json) - 前端 exportLogs 传递查询参数 同时包含累积的前期工作: - 目录重构 internal/ -> controller/model/router (v0.55-v0.57) - 后端分层架构 dto/repository/service/utils - 审计模块全功能 (终端审计/操作审计/登录日志) - asciicast v2 录制格式 - SFTP 文件浏览器 - StatStrip 紧凑统计条 - 在线会话自动刷新 - LiveMonitor WebSocket 诊断修复 验证: go build ✅ | npm run build ✅ | Docker 部署 ✅ | 10 回归端点 ✅ | CSV 导出 ✅ | 静默 CRUD ✅
151 lines
4.2 KiB
Go
151 lines
4.2 KiB
Go
package controller
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"ezops/model"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// LogHandler 日志监控处理器 // Logan.Li
|
|
type LogHandler struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewLogHandler(db *gorm.DB) *LogHandler {
|
|
return &LogHandler{db: db}
|
|
}
|
|
|
|
// QueryLogs 查询日志条目(支持 level/service/host/keyword 过滤) // Logan.Li
|
|
func (h *LogHandler) QueryLogs(c *gin.Context) {
|
|
var query model.PageQuery
|
|
c.ShouldBindQuery(&query)
|
|
|
|
db := h.db.Model(&model.LogEntry{})
|
|
|
|
// 过滤
|
|
if level := c.Query("level"); level != "" {
|
|
db = db.Where("level = ?", level)
|
|
}
|
|
if service := c.Query("service"); service != "" {
|
|
db = db.Where("service = ?", service)
|
|
}
|
|
if host := c.Query("host"); host != "" {
|
|
db = db.Where("host = ?", host)
|
|
}
|
|
if kw := c.Query("keyword"); kw != "" {
|
|
db = db.Where("message LIKE ?", "%"+kw+"%")
|
|
}
|
|
if sourceID := c.Query("source_id"); sourceID != "" {
|
|
db = db.Where("source_id = ?", sourceID)
|
|
}
|
|
|
|
var total int64
|
|
db.Count(&total)
|
|
|
|
var logs []model.LogEntry
|
|
db.Order("timestamp DESC").Offset(query.GetOffset()).Limit(query.GetLimit()).Find(&logs)
|
|
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: logs, Total: total}})
|
|
}
|
|
|
|
// ListSources 获取日志源列表 // Logan.Li
|
|
func (h *LogHandler) ListSources(c *gin.Context) {
|
|
var sources []model.LogSource
|
|
h.db.Order("updated_at DESC").Find(&sources)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: model.ListResponse{List: sources, Total: int64(len(sources))}})
|
|
}
|
|
|
|
// CreateSource 创建日志源 // Logan.Li
|
|
func (h *LogHandler) CreateSource(c *gin.Context) {
|
|
var req struct {
|
|
Name string `json:"name" binding:"required"`
|
|
Type string `json:"type" binding:"required"` // application/syslog/docker/kubernetes
|
|
Config map[string]interface{} `json:"config"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
configJSON, _ := json.Marshal(req.Config)
|
|
now := time.Now()
|
|
item := model.LogSource{
|
|
Name: req.Name,
|
|
Type: req.Type,
|
|
Status: "active",
|
|
Config: string(configJSON),
|
|
LastSeen: &now,
|
|
}
|
|
h.db.Create(&item)
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Data: item})
|
|
}
|
|
|
|
// DeleteSource 删除日志源 // Logan.Li
|
|
func (h *LogHandler) DeleteSource(c *gin.Context) {
|
|
if err := h.db.Delete(&model.LogSource{}, c.Param("id")); err.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Response{Code: 500, Message: "删除失败"})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "已删除"})
|
|
}
|
|
|
|
// IngestLogs Agent 上报日志(无需 JWT,供 Agent 调用) // Logan.Li
|
|
func (h *LogHandler) IngestLogs(c *gin.Context) {
|
|
var req struct {
|
|
Source string `json:"source"`
|
|
Host string `json:"host"`
|
|
Service string `json:"service"`
|
|
Entries []map[string]interface{} `json:"entries"`
|
|
}
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, model.Response{Code: 400, Message: err.Error()})
|
|
return
|
|
}
|
|
|
|
// 查找或创建日志源
|
|
var source model.LogSource
|
|
h.db.Where("name = ?", req.Source).FirstOrCreate(&source, model.LogSource{
|
|
Name: req.Source, Type: "agent", Status: "active",
|
|
})
|
|
|
|
count := 0
|
|
for _, entry := range req.Entries {
|
|
level, _ := entry["level"].(string)
|
|
msg, _ := entry["message"].(string)
|
|
raw, _ := entry["raw"].(string)
|
|
traceID, _ := entry["trace_id"].(string)
|
|
ts := time.Now()
|
|
if t, ok := entry["timestamp"].(string); ok {
|
|
if parsed, err := time.Parse(time.RFC3339, t); err == nil {
|
|
ts = parsed
|
|
}
|
|
}
|
|
|
|
log := model.LogEntry{
|
|
SourceID: source.ID,
|
|
Timestamp: ts,
|
|
Level: level,
|
|
Service: req.Service,
|
|
Host: req.Host,
|
|
Message: msg,
|
|
TraceID: traceID,
|
|
Raw: raw,
|
|
}
|
|
if err := h.db.Create(&log); err.Error == nil {
|
|
count++
|
|
}
|
|
}
|
|
|
|
// 更新日志源统计
|
|
now := time.Now()
|
|
h.db.Model(&source).Updates(map[string]interface{}{
|
|
"last_seen": &now,
|
|
}).UpdateColumn("log_count", source.LogCount+int64(count))
|
|
|
|
c.JSON(http.StatusOK, model.Response{Code: 200, Message: "成功写入", Data: map[string]int{"count": count}})
|
|
}
|