feat: 重构监控中心导航,新增资源监控4大模块
- 主菜单「监控告警」→「监控中心」 - 「告警中心」→「告警服务」移至链路追踪下方 - 新增资源监控 Tab 页(主机/中间件/SNMP/站点拨测) - Agent 新增网络采集器(network)和磁盘IO采集器(diskio) - 后端扩展 Agent 上报模型,支持 NetworkStat/DiskIOStat - 前端 ServerTableRow 展示网络和磁盘IO实时指标 - WebSocket MetricsUpdate 推送扩展
This commit is contained in:
1 parent
020b7a9063
commit
f3a9e5e8d0
21 files changed
+1007
-32
No files matched your search
@@ -19,12 +19,13 @@ import (
|
||||
|
||||
// AgentHandler 处理 Agent 上报数据 // Logan.Li
|
||||
type AgentHandler struct {
|
||||
db *gorm.DB
|
||||
cache ezredis.Cache // 可选 Redis 缓存(用于热数据存储)
|
||||
db *gorm.DB
|
||||
cache ezredis.Cache // 可选 Redis 缓存(用于热数据存储)
|
||||
hubMetrics *MetricsHub // WebSocket 广播 metrics 更新
|
||||
}
|
||||
|
||||
func NewAgentHandler(db *gorm.DB, cache ezredis.Cache) *AgentHandler {
|
||||
return &AgentHandler{db: db, cache: cache}
|
||||
func NewAgentHandler(db *gorm.DB, cache ezredis.Cache, hub *MetricsHub) *AgentHandler {
|
||||
return &AgentHandler{db: db, cache: cache, hubMetrics: hub}
|
||||
}
|
||||
|
||||
// ReceiveReport 接收 Agent 上报的指标数据
|
||||
@@ -47,6 +48,8 @@ func (h *AgentHandler) ReceiveReport(c *gin.Context) {
|
||||
// 序列化磁盘和端口信息
|
||||
diskJSON, _ := json.Marshal(req.Disks)
|
||||
portJSON, _ := json.Marshal(req.Ports)
|
||||
netJSON, _ := json.Marshal(req.Network)
|
||||
dioJSON, _ := json.Marshal(req.DiskIO)
|
||||
|
||||
// 存储到数据库
|
||||
report := models.AgentReport{
|
||||
@@ -61,6 +64,8 @@ func (h *AgentHandler) ReceiveReport(c *gin.Context) {
|
||||
MemUsage: req.Memory.UsagePercent,
|
||||
DiskInfo: string(diskJSON),
|
||||
PortInfo: string(portJSON),
|
||||
NetInfo: string(netJSON),
|
||||
DiskIOInfo: string(dioJSON),
|
||||
ReportTime: reportTime,
|
||||
}
|
||||
|
||||
@@ -69,6 +74,35 @@ func (h *AgentHandler) ReceiveReport(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// 同步更新 servers 表的实时指标(通过 hostname 匹配)
|
||||
diskAvg := avgDiskUsage(req.Disks)
|
||||
result := h.db.Model(&models.Server{}).
|
||||
Where("hostname = ? OR ip = ?", req.Hostname, req.Hostname).
|
||||
Updates(map[string]interface{}{
|
||||
"cpu_usage": req.CPU.UsagePercent,
|
||||
"memory_usage": req.Memory.UsagePercent,
|
||||
"disk_usage": diskAvg,
|
||||
"status": "running",
|
||||
})
|
||||
|
||||
// WebSocket 广播:向所有前端客户端推送该主机的最新指标 // Logan.Li
|
||||
if h.hubMetrics != nil && result.RowsAffected > 0 {
|
||||
h.hubMetrics.Broadcast(MetricsUpdate{
|
||||
Type: "metrics_update",
|
||||
Hostname: req.Hostname,
|
||||
CPU: req.CPU.UsagePercent,
|
||||
Memory: req.Memory.UsagePercent,
|
||||
Disk: diskAvg,
|
||||
Status: "running",
|
||||
TCPEstablished: req.Network.TCPEstablished,
|
||||
TCPTimeWait: req.Network.TCPTimeWait,
|
||||
NetRxBPS: req.Network.NetRxBPS,
|
||||
NetTxBPS: req.Network.NetTxBPS,
|
||||
DiskReadBPS: req.DiskIO.ReadBPS,
|
||||
DiskWriteBPS: req.DiskIO.WriteBPS,
|
||||
})
|
||||
}
|
||||
|
||||
// 写入 Redis 有序集合(热数据,保留7天)
|
||||
if h.cache != nil {
|
||||
timestamp := float64(reportTime.Unix())
|
||||
@@ -169,6 +203,7 @@ func (h *AgentHandler) AggregateMetrics(c *gin.Context) {
|
||||
duration time.Duration
|
||||
}{
|
||||
{"1m", 1 * time.Minute},
|
||||
{"5m", 5 * time.Minute},
|
||||
{"3m", 3 * time.Minute},
|
||||
{"1h", 1 * time.Hour},
|
||||
{"6h", 6 * time.Hour},
|
||||
@@ -283,11 +318,20 @@ func (h *AgentHandler) GetKline(c *gin.Context) {
|
||||
}
|
||||
|
||||
var klines []models.MetricsKline
|
||||
h.db.Where("hostname = ? AND metric = ? AND interval = ? AND bucket_time >= ? AND bucket_time <= ?",
|
||||
result := h.db.Where("hostname = ? AND metric = ? AND `interval` = ? AND bucket_time >= ? AND bucket_time <= ?",
|
||||
req.Hostname, req.Metric, req.Interval, from, to).
|
||||
Order("bucket_time ASC").
|
||||
Find(&klines)
|
||||
|
||||
if result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, models.Response{Code: 500, Message: "查询失败: " + result.Error.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
if klines == nil {
|
||||
klines = []models.MetricsKline{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, models.Response{
|
||||
Code: 200,
|
||||
Message: "success",
|
||||
@@ -305,3 +349,15 @@ func parseMemberValue(member string) float64 {
|
||||
v, _ := strconv.ParseFloat(member[:idx], 64)
|
||||
return v
|
||||
}
|
||||
|
||||
// avgDiskUsage 计算磁盘平均使用率
|
||||
func avgDiskUsage(disks []models.DiskMetric) float64 {
|
||||
if len(disks) == 0 {
|
||||
return 0
|
||||
}
|
||||
var sum float64
|
||||
for _, d := range disks {
|
||||
sum += d.UsagePercent
|
||||
}
|
||||
return math.Round(sum/float64(len(disks))*100) / 100
|
||||
}
|
||||
Reference in new issue
Block a user