web修复
This commit is contained in:
@@ -67,15 +67,18 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
}
|
||||
}
|
||||
|
||||
// NetworkInterfaceMetrics 网卡监控指标
|
||||
type NetworkInterfaceMetrics struct {
|
||||
BytesSent uint64 `json:"bytes_sent"`
|
||||
BytesReceived uint64 `json:"bytes_received"`
|
||||
}
|
||||
|
||||
// MetricsRequest 指标请求结构
|
||||
type MetricsRequest struct {
|
||||
CPU float64 `json:"cpu"`
|
||||
Memory float64 `json:"memory"`
|
||||
Disk map[string]float64 `json:"disk"`
|
||||
Network struct {
|
||||
BytesSent uint64 `json:"bytes_sent"`
|
||||
BytesReceived uint64 `json:"bytes_received"`
|
||||
} `json:"network"`
|
||||
CPU float64 `json:"cpu"`
|
||||
Memory float64 `json:"memory"`
|
||||
Disk map[string]float64 `json:"disk"`
|
||||
Network map[string]NetworkInterfaceMetrics `json:"network"`
|
||||
}
|
||||
|
||||
// HandleMetricsPost 处理Agent发送的指标数据
|
||||
@@ -127,11 +130,20 @@ func HandleMetricsPost(c *gin.Context) {
|
||||
// 处理请求体,支持单指标对象和指标数组
|
||||
var metricsList []MetricsRequest
|
||||
|
||||
// 读取请求体到缓冲区,以便多次解析
|
||||
body, err := c.GetRawData()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Failed to read request body",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 首先尝试解析为数组
|
||||
if err := c.ShouldBindJSON(&metricsList); err != nil {
|
||||
if err := json.Unmarshal(body, &metricsList); err != nil {
|
||||
// 如果解析数组失败,尝试解析为单个对象
|
||||
var singleMetric MetricsRequest
|
||||
if err := c.ShouldBindJSON(&singleMetric); err != nil {
|
||||
if err := json.Unmarshal(body, &singleMetric); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Invalid request body",
|
||||
})
|
||||
@@ -173,16 +185,33 @@ func HandleMetricsPost(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// 写入网络发送指标
|
||||
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_sent", float64(req.Network.BytesSent), baseTags); err != nil {
|
||||
// 只记录警告,不影响后续指标处理
|
||||
log.Printf("Warning: Failed to write network sent metrics: %v", err)
|
||||
}
|
||||
// 写入网络指标,支持多个网卡
|
||||
var totalBytesSent, totalBytesReceived uint64
|
||||
for interfaceName, networkMetrics := range req.Network {
|
||||
// 为每个网卡创建标签,包含基础标签和网卡名称
|
||||
interfaceTags := make(map[string]string)
|
||||
// 复制基础标签
|
||||
for k, v := range baseTags {
|
||||
interfaceTags[k] = v
|
||||
}
|
||||
// 添加网卡标签
|
||||
interfaceTags["interface"] = interfaceName
|
||||
|
||||
// 写入网络接收指标
|
||||
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_received", float64(req.Network.BytesReceived), baseTags); err != nil {
|
||||
// 只记录警告,不影响后续指标处理
|
||||
log.Printf("Warning: Failed to write network received metrics: %v", err)
|
||||
// 写入网络发送指标
|
||||
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_sent", float64(networkMetrics.BytesSent), interfaceTags); err != nil {
|
||||
// 只记录警告,不影响后续指标处理
|
||||
log.Printf("Warning: Failed to write network sent metrics for interface %s: %v", interfaceName, err)
|
||||
}
|
||||
|
||||
// 写入网络接收指标
|
||||
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_received", float64(networkMetrics.BytesReceived), interfaceTags); err != nil {
|
||||
// 只记录警告,不影响后续指标处理
|
||||
log.Printf("Warning: Failed to write network received metrics for interface %s: %v", interfaceName, err)
|
||||
}
|
||||
|
||||
// 累加总流量
|
||||
totalBytesSent += networkMetrics.BytesSent
|
||||
totalBytesReceived += networkMetrics.BytesReceived
|
||||
}
|
||||
|
||||
// 广播指标更新消息,只广播最后一个指标
|
||||
@@ -192,9 +221,10 @@ func HandleMetricsPost(c *gin.Context) {
|
||||
"memory": req.Memory,
|
||||
"disk": req.Disk,
|
||||
"network": map[string]uint64{
|
||||
"bytes_sent": req.Network.BytesSent,
|
||||
"bytes_received": req.Network.BytesReceived,
|
||||
"bytes_sent": totalBytesSent,
|
||||
"bytes_received": totalBytesReceived,
|
||||
},
|
||||
"network_interfaces": req.Network,
|
||||
}
|
||||
broadcastMetricsUpdate(deviceID, metrics)
|
||||
}
|
||||
@@ -440,15 +470,61 @@ func GetNetworkMetrics(c *gin.Context) {
|
||||
receivedPoints = []storage.MetricPoint{}
|
||||
}
|
||||
|
||||
// 处理数据,传递interval、startTime和endTime参数
|
||||
processedSentData := ProcessMetrics(sentPoints, aggregation, interval, startTime, endTime)
|
||||
processedReceivedData := ProcessMetrics(receivedPoints, aggregation, interval, startTime, endTime)
|
||||
// 按网卡名称分组发送和接收的指标
|
||||
sentByInterface := make(map[string][]storage.MetricPoint)
|
||||
receivedByInterface := make(map[string][]storage.MetricPoint)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": map[string][]MetricData{
|
||||
// 分组发送的网络指标
|
||||
for _, point := range sentPoints {
|
||||
// 获取网卡名称,默认使用"all"表示所有网卡
|
||||
interfaceName := point.Tags["interface"]
|
||||
if interfaceName == "" {
|
||||
interfaceName = "all"
|
||||
}
|
||||
sentByInterface[interfaceName] = append(sentByInterface[interfaceName], point)
|
||||
}
|
||||
|
||||
// 分组接收的网络指标
|
||||
for _, point := range receivedPoints {
|
||||
// 获取网卡名称,默认使用"all"表示所有网卡
|
||||
interfaceName := point.Tags["interface"]
|
||||
if interfaceName == "" {
|
||||
interfaceName = "all"
|
||||
}
|
||||
receivedByInterface[interfaceName] = append(receivedByInterface[interfaceName], point)
|
||||
}
|
||||
|
||||
// 处理数据,为每个网卡创建独立的数据集
|
||||
result := make(map[string]map[string][]MetricData)
|
||||
|
||||
// 合并所有网卡名称
|
||||
allInterfaces := make(map[string]bool)
|
||||
for iface := range sentByInterface {
|
||||
allInterfaces[iface] = true
|
||||
}
|
||||
for iface := range receivedByInterface {
|
||||
allInterfaces[iface] = true
|
||||
}
|
||||
|
||||
// 为每个网卡处理数据
|
||||
for iface := range allInterfaces {
|
||||
// 获取该网卡的发送和接收指标
|
||||
ifaceSentPoints := sentByInterface[iface]
|
||||
ifaceReceivedPoints := receivedByInterface[iface]
|
||||
|
||||
// 处理数据
|
||||
processedSentData := ProcessMetrics(ifaceSentPoints, aggregation, interval, startTime, endTime)
|
||||
processedReceivedData := ProcessMetrics(ifaceReceivedPoints, aggregation, interval, startTime, endTime)
|
||||
|
||||
// 保存结果
|
||||
result[iface] = map[string][]MetricData{
|
||||
"sent": processedSentData,
|
||||
"received": processedReceivedData,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"data": result,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user