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
+3
-2
@@ -125,7 +125,7 @@ func main() {
|
||||
|
||||
// 初始化缓存(Redka 内嵌 / Redis 外部)// Logan.Li
|
||||
cache, err := ezredis.New(ezredis.Config{
|
||||
Driver: "redka", // 默认内嵌 Redka,零依赖
|
||||
Driver: cfg.Redis.Driver, // 从配置读取 driver
|
||||
Host: cfg.Redis.Host,
|
||||
Port: cfg.Redis.Port,
|
||||
Password: cfg.Redis.Password,
|
||||
@@ -136,7 +136,7 @@ func main() {
|
||||
log.Warn("缓存初始化失败,继续启动", zap.Error(err))
|
||||
cache = nil
|
||||
} else {
|
||||
log.Info("缓存已就绪", zap.String("driver", "redka"))
|
||||
log.Info("缓存已就绪", zap.String("driver", cfg.Redis.Driver))
|
||||
defer cache.Close()
|
||||
}
|
||||
|
||||
@@ -222,6 +222,7 @@ func loadConfig() *config.Config {
|
||||
|
||||
viper.SetDefault("redis.host", "localhost")
|
||||
viper.SetDefault("redis.port", 6379)
|
||||
viper.SetDefault("redis.driver", "redka")
|
||||
viper.SetDefault("redis.password", "")
|
||||
viper.SetDefault("redis.db", 0)
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// MetricsUpdate 单个主机的指标更新 // Logan.Li
|
||||
type MetricsUpdate struct {
|
||||
Type string `json:"type"` // "metrics_update"
|
||||
Hostname string `json:"hostname"`
|
||||
CPU float64 `json:"cpu_usage"`
|
||||
Memory float64 `json:"memory_usage"`
|
||||
Disk float64 `json:"disk_usage"`
|
||||
Status string `json:"status"`
|
||||
TCPEstablished int `json:"tcp_established,omitempty"`
|
||||
TCPTimeWait int `json:"tcp_time_wait,omitempty"`
|
||||
NetRxBPS float64 `json:"net_rx_bps,omitempty"`
|
||||
NetTxBPS float64 `json:"net_tx_bps,omitempty"`
|
||||
DiskReadBPS float64 `json:"disk_read_bps,omitempty"`
|
||||
DiskWriteBPS float64 `json:"disk_write_bps,omitempty"`
|
||||
}
|
||||
|
||||
// MetricsHub 管理所有 WebSocket 客户端连接,广播指标更新 // Logan.Li
|
||||
type MetricsHub struct {
|
||||
mu sync.RWMutex
|
||||
clients map[*websocket.Conn]bool
|
||||
}
|
||||
|
||||
// NewMetricsHub 创建 MetricsHub 实例
|
||||
func NewMetricsHub() *MetricsHub {
|
||||
return &MetricsHub{
|
||||
clients: make(map[*websocket.Conn]bool),
|
||||
}
|
||||
}
|
||||
|
||||
// Register 注册新的 WebSocket 连接
|
||||
func (h *MetricsHub) Register(conn *websocket.Conn) {
|
||||
h.mu.Lock()
|
||||
h.clients[conn] = true
|
||||
h.mu.Unlock()
|
||||
log.Printf("[MetricsWS] 客户端已连接,当前连接数: %d", h.Count())
|
||||
}
|
||||
|
||||
// Unregister 移除断开的 WebSocket 连接
|
||||
func (h *MetricsHub) Unregister(conn *websocket.Conn) {
|
||||
h.mu.Lock()
|
||||
delete(h.clients, conn)
|
||||
h.mu.Unlock()
|
||||
log.Printf("[MetricsWS] 客户端已断开,当前连接数: %d", h.Count())
|
||||
}
|
||||
|
||||
// Count 返回当前连接数
|
||||
func (h *MetricsHub) Count() int {
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
return len(h.clients)
|
||||
}
|
||||
|
||||
// Broadcast 向所有连接的客户端广播指标更新
|
||||
func (h *MetricsHub) Broadcast(update MetricsUpdate) {
|
||||
data, err := json.Marshal(update)
|
||||
if err != nil {
|
||||
log.Printf("[MetricsWS] JSON 序列化失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
h.mu.RLock()
|
||||
defer h.mu.RUnlock()
|
||||
|
||||
for conn := range h.clients {
|
||||
if err := conn.WriteMessage(websocket.TextMessage, data); err != nil {
|
||||
log.Printf("[MetricsWS] 发送失败: %v", err)
|
||||
go func(c *websocket.Conn) {
|
||||
h.Unregister(c)
|
||||
c.Close()
|
||||
}(conn)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MetricsWSHandler 处理 /ws/metrics 的 WebSocket 连接 // Logan.Li
|
||||
type MetricsWSHandler struct {
|
||||
hub *MetricsHub
|
||||
}
|
||||
|
||||
// NewMetricsWSHandler 创建 WebSocket metrics 处理器
|
||||
func NewMetricsWSHandler(hub *MetricsHub) *MetricsWSHandler {
|
||||
return &MetricsWSHandler{hub: hub}
|
||||
}
|
||||
|
||||
// ServeWS 升级 HTTP → WebSocket,注册到 Hub,保持连接
|
||||
func (h *MetricsWSHandler) ServeWS(c *gin.Context) {
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
log.Printf("[MetricsWS] WebSocket 升级失败: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
h.hub.Register(conn)
|
||||
defer func() {
|
||||
h.hub.Unregister(conn)
|
||||
conn.Close()
|
||||
}()
|
||||
|
||||
// 保持连接,读取消息(客户端可能发 ping)
|
||||
for {
|
||||
_, _, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseNormalClosure) {
|
||||
log.Printf("[MetricsWS] 连接异常关闭: %v", err)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,6 +59,11 @@ func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config, c
|
||||
webShellHandler := handlers.NewWebShellHandler(db)
|
||||
r.GET("/ws/terminal", middleware.JWTAuth(&cfg.JWT), webShellHandler.Terminal)
|
||||
|
||||
// WebSocket 实时指标推送(JWT 鉴权) // Logan.Li
|
||||
metricsHub := handlers.NewMetricsHub()
|
||||
metricsWSHandler := handlers.NewMetricsWSHandler(metricsHub)
|
||||
r.GET("/ws/metrics", middleware.JWTAuth(&cfg.JWT), metricsWSHandler.ServeWS)
|
||||
|
||||
// 公开路由
|
||||
authHandler := handlers.NewUserHandler(db, cfg)
|
||||
api.POST("/auth/login", authHandler.Login)
|
||||
@@ -414,7 +419,7 @@ func Register(r *gin.Engine, db *gorm.DB, log *zap.Logger, cfg *config.Config, c
|
||||
|
||||
// Agent 上报路由(无需 JWT)
|
||||
redisCache, _ := cache.(ezredis.Cache)
|
||||
agentHandler := handlers.NewAgentHandler(db, redisCache)
|
||||
agentHandler := handlers.NewAgentHandler(db, redisCache, metricsHub)
|
||||
taskHandler := handlers.NewAgentTaskHandler(db)
|
||||
agentGroup := api.Group("/agent")
|
||||
{
|
||||
|
||||
@@ -39,6 +39,7 @@ type JWTConfig struct {
|
||||
|
||||
// RedisConfig Redis配置
|
||||
type RedisConfig struct {
|
||||
Driver string `mapstructure:"driver"` // redka (内嵌) / redis (外部)
|
||||
Host string `mapstructure:"host"`
|
||||
Port int `mapstructure:"port"`
|
||||
Password string `mapstructure:"password"`
|
||||
@@ -72,6 +73,7 @@ func Load() *Config {
|
||||
viper.SetDefault("jwt.expire_hour", 24)
|
||||
viper.SetDefault("redis.host", "localhost")
|
||||
viper.SetDefault("redis.port", 6379)
|
||||
viper.SetDefault("redis.driver", "redka")
|
||||
viper.SetDefault("websocket.read_buffer_size", 1024)
|
||||
viper.SetDefault("websocket.write_buffer_size", 1024)
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ type AgentReport struct {
|
||||
MemUsage float64 `gorm:"type:decimal(6,2)" json:"mem_usage"` // 内存使用率 %
|
||||
DiskInfo string `gorm:"type:text" json:"disk_info"` // 磁盘信息 JSON
|
||||
PortInfo string `gorm:"type:text" json:"port_info"` // 端口信息 JSON
|
||||
NetInfo string `gorm:"type:text" json:"net_info"` // 网络指标 JSON
|
||||
DiskIOInfo string `gorm:"type:text" json:"disk_io_info"` // 磁盘 IO JSON
|
||||
ReportTime time.Time `json:"report_time"` // 上报时间
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
@@ -32,12 +34,14 @@ func (AgentReport) TableName() string {
|
||||
|
||||
// AgentReportRequest Agent 上报请求体 // Logan.Li
|
||||
type AgentReportRequest struct {
|
||||
Hostname string `json:"hostname"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
CPU CPUMetric `json:"cpu"`
|
||||
Memory MemMetric `json:"memory"`
|
||||
Hostname string `json:"hostname" binding:"required"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
CPU CPUMetric `json:"cpu"`
|
||||
Memory MemMetric `json:"memory"`
|
||||
Disks []DiskMetric `json:"disks"`
|
||||
Ports []PortMetric `json:"ports"`
|
||||
Network NetworkStat `json:"network"` // 网络指标
|
||||
DiskIO DiskIOStat `json:"disk_io"` // 磁盘 IO
|
||||
}
|
||||
|
||||
type CPUMetric struct {
|
||||
@@ -69,3 +73,21 @@ type PortMetric struct {
|
||||
Process string `json:"process"`
|
||||
PID int `json:"pid"`
|
||||
}
|
||||
|
||||
// NetworkStat 网络指标(Agent 上报)
|
||||
type NetworkStat struct {
|
||||
TCPEstablished int `json:"tcp_established"`
|
||||
TCPTimeWait int `json:"tcp_time_wait"`
|
||||
TCPTotal int `json:"tcp_total"`
|
||||
NicName string `json:"nic_name"`
|
||||
NetRxBPS float64 `json:"net_rx_bps"`
|
||||
NetTxBPS float64 `json:"net_tx_bps"`
|
||||
}
|
||||
|
||||
// DiskIOStat 磁盘 IO 指标(Agent 上报)
|
||||
type DiskIOStat struct {
|
||||
ReadBPS float64 `json:"read_bps"`
|
||||
WriteBPS float64 `json:"write_bps"`
|
||||
ReadIOPS float64 `json:"read_iops"`
|
||||
WriteIOPS float64 `json:"write_iops"`
|
||||
}
|
||||
Reference in new issue
Block a user