数据调优

This commit is contained in:
Alex Yang
2025-12-03 15:05:16 +08:00
parent 8bd13e3af1
commit c854c460a8
7 changed files with 464 additions and 89 deletions

View File

@@ -67,17 +67,26 @@ func RegisterRoutes(r *gin.Engine) {
}
}
// DiskMetrics 磁盘监控指标
type DiskMetrics struct {
UsedPercent float64 `json:"used_percent"` // 使用率百分比
Total uint64 `json:"total"` // 总容量 (bytes)
}
// NetworkInterfaceMetrics 网卡监控指标
type NetworkInterfaceMetrics struct {
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
BytesSent uint64 `json:"bytes_sent"` // 发送速率 (bytes/s)
BytesReceived uint64 `json:"bytes_received"` // 接收速率 (bytes/s)
TxBytes uint64 `json:"tx_bytes"` // 累计发送字节数
RxBytes uint64 `json:"rx_bytes"` // 累计接收字节数
}
// MetricsRequest 指标请求结构
type MetricsRequest struct {
CPU float64 `json:"cpu"`
CPUHz float64 `json:"cpu_hz"` // CPU频率 (MHz)
Memory float64 `json:"memory"`
Disk map[string]float64 `json:"disk"`
Disk map[string]DiskMetrics `json:"disk"`
Network map[string]NetworkInterfaceMetrics `json:"network"`
}
@@ -155,12 +164,20 @@ func HandleMetricsPost(c *gin.Context) {
// 处理所有指标
for i, req := range metricsList {
// 写入CPU指标
// 写入CPU使用率指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "cpu", req.CPU, baseTags); err != nil {
// 只记录警告,不影响后续指标处理
log.Printf("Warning: Failed to write CPU metrics: %v", err)
}
// 写入CPU频率指标如果有
if req.CPUHz > 0 {
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "cpu_hz", req.CPUHz, baseTags); err != nil {
// 只记录警告,不影响后续指标处理
log.Printf("Warning: Failed to write CPU Hz metrics: %v", err)
}
}
// 写入内存指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "memory", req.Memory, baseTags); err != nil {
// 只记录警告,不影响后续指标处理
@@ -168,7 +185,7 @@ func HandleMetricsPost(c *gin.Context) {
}
// 写入磁盘指标,支持多个挂载点
for mountpoint, usage := range req.Disk {
for mountpoint, diskMetrics := range req.Disk {
// 为每个挂载点创建标签,包含基础标签和挂载点
tags := make(map[string]string)
// 复制基础标签
@@ -178,8 +195,8 @@ func HandleMetricsPost(c *gin.Context) {
// 添加挂载点标签
tags["mountpoint"] = mountpoint
// 写入磁盘指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "disk", usage, tags); err != nil {
// 写入磁盘使用率指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "disk", diskMetrics.UsedPercent, tags); err != nil {
// 只记录警告,不影响后续指标处理
log.Printf("Warning: Failed to write disk metrics for mountpoint %s: %v", mountpoint, err)
}
@@ -187,6 +204,7 @@ func HandleMetricsPost(c *gin.Context) {
// 写入网络指标,支持多个网卡
var totalBytesSent, totalBytesReceived uint64
var totalTxBytes, totalRxBytes uint64 // 累计总流量
for interfaceName, networkMetrics := range req.Network {
// 为每个网卡创建标签,包含基础标签和网卡名称
interfaceTags := make(map[string]string)
@@ -197,32 +215,57 @@ func HandleMetricsPost(c *gin.Context) {
// 添加网卡标签
interfaceTags["interface"] = interfaceName
// 写入网络发送指标
// 写入网络发送速率指标
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)
}
// 累加总流量
// 写入累计发送字节数指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_tx_bytes", float64(networkMetrics.TxBytes), interfaceTags); err != nil {
// 只记录警告,不影响后续指标处理
log.Printf("Warning: Failed to write network tx_bytes metrics for interface %s: %v", interfaceName, err)
}
// 写入累计接收字节数指标
if err := globalStorage.WriteMetric(c.Request.Context(), deviceID, "network_rx_bytes", float64(networkMetrics.RxBytes), interfaceTags); err != nil {
// 只记录警告,不影响后续指标处理
log.Printf("Warning: Failed to write network rx_bytes metrics for interface %s: %v", interfaceName, err)
}
// 累加总流量速率
totalBytesSent += networkMetrics.BytesSent
totalBytesReceived += networkMetrics.BytesReceived
// 累加累计总流量
totalTxBytes += networkMetrics.TxBytes
totalRxBytes += networkMetrics.RxBytes
}
// 广播指标更新消息,只广播最后一个指标
if i == len(metricsList)-1 {
// 准备广播的磁盘使用率数据(兼容旧格式)
compatDisk := make(map[string]float64)
for mountpoint, diskMetrics := range req.Disk {
compatDisk[mountpoint] = diskMetrics.UsedPercent
}
metrics := map[string]interface{}{
"cpu": req.CPU,
"cpu_hz": req.CPUHz,
"memory": req.Memory,
"disk": req.Disk,
"disk": compatDisk, // 使用兼容格式的磁盘数据
"network": map[string]uint64{
"bytes_sent": totalBytesSent,
"bytes_received": totalBytesReceived,
"tx_bytes": totalTxBytes,
"rx_bytes": totalRxBytes,
},
"network_interfaces": req.Network,
}