web修复

This commit is contained in:
Alex Yang
2025-12-03 10:27:45 +08:00
parent dd01058b32
commit bd70dc1748
9 changed files with 437 additions and 134 deletions

View File

@@ -29,15 +29,18 @@ type Config struct {
APIPort int `json:"api_port"` // API端口
}
// NetworkInterfaceMetrics 网卡监控指标
type NetworkInterfaceMetrics struct {
BytesSent uint64 `json:"bytes_sent"`
BytesReceived uint64 `json:"bytes_received"`
}
// Metrics 监控指标
type Metrics 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"`
}
// 全局配置
@@ -47,10 +50,14 @@ var config Config
var parsedInterval time.Duration
// 保存上一次网络流量采集的数据
type NetworkStats struct {
BytesSent uint64
BytesReceived uint64
}
var (
lastBytesSent uint64
lastBytesReceived uint64
lastCollectTime time.Time
lastNetworkStats = make(map[string]NetworkStats)
lastCollectTime time.Time
)
// 保存采集到的数据点
@@ -316,44 +323,67 @@ func collectDisk() (map[string]float64, error) {
}
// 采集网络流量
func collectNetwork() (uint64, uint64, error) {
ioCounters, err := net.IOCounters(false)
func collectNetwork() (map[string]NetworkInterfaceMetrics, error) {
// 获取所有网卡的统计数据
ioCounters, err := net.IOCounters(true)
if err != nil {
return 0, 0, err
return nil, err
}
if len(ioCounters) == 0 {
return 0, 0, nil
return make(map[string]NetworkInterfaceMetrics), nil
}
// 获取当前时间和流量
// 获取当前时间
currentTime := time.Now()
currentBytesSent := ioCounters[0].BytesSent
currentBytesReceived := ioCounters[0].BytesRecv
// 计算速率
var bytesSentRate, bytesReceivedRate uint64
if !lastCollectTime.IsZero() {
// 计算时间差(秒)
timeDiff := currentTime.Sub(lastCollectTime).Seconds()
if timeDiff > 0 {
// 计算流量差
sentDiff := currentBytesSent - lastBytesSent
receivedDiff := currentBytesReceived - lastBytesReceived
// 初始化返回值
networkMetrics := make(map[string]NetworkInterfaceMetrics)
// 计算速率bytes/s
bytesSentRate = uint64(float64(sentDiff) / timeDiff)
bytesReceivedRate = uint64(float64(receivedDiff) / timeDiff)
// 遍历所有网卡
for _, counter := range ioCounters {
// 获取当前网卡的流量
currentBytesSent := counter.BytesSent
currentBytesReceived := counter.BytesRecv
// 计算速率
var bytesSentRate, bytesReceivedRate uint64
if !lastCollectTime.IsZero() {
// 计算时间差(秒)
timeDiff := currentTime.Sub(lastCollectTime).Seconds()
if timeDiff > 0 {
// 获取上一次该网卡的流量
lastStats, exists := lastNetworkStats[counter.Name]
if exists {
// 计算流量差
sentDiff := currentBytesSent - lastStats.BytesSent
receivedDiff := currentBytesReceived - lastStats.BytesReceived
// 计算速率bytes/s
bytesSentRate = uint64(float64(sentDiff) / timeDiff)
bytesReceivedRate = uint64(float64(receivedDiff) / timeDiff)
}
}
}
// 更新上一次采集的值
lastNetworkStats[counter.Name] = NetworkStats{
BytesSent: currentBytesSent,
BytesReceived: currentBytesReceived,
}
// 保存当前网卡的速率
networkMetrics[counter.Name] = NetworkInterfaceMetrics{
BytesSent: bytesSentRate,
BytesReceived: bytesReceivedRate,
}
}
// 更新上一次采集的值
lastBytesSent = currentBytesSent
lastBytesReceived = currentBytesReceived
// 更新上一次采集时间
lastCollectTime = currentTime
// 返回速率而不是累计流量
return bytesSentRate, bytesReceivedRate, nil
// 返回所有网卡的速率
return networkMetrics, nil
}
// 采集所有监控指标
@@ -382,13 +412,12 @@ func collectMetrics() (*Metrics, error) {
metrics.Disk = diskUsageMap
// 采集网络流量
bytesSent, bytesReceived, err := collectNetwork()
networkMetrics, err := collectNetwork()
if err != nil {
return nil, fmt.Errorf("failed to collect network metrics: %w", err)
}
// 直接使用采集到的累计流量
metrics.Network.BytesSent = bytesSent
metrics.Network.BytesReceived = bytesReceived
// 直接使用采集到的网卡流量
metrics.Network = networkMetrics
return metrics, nil
}
@@ -407,7 +436,7 @@ func sendMetrics(metricsList []*Metrics) error {
}
// 创建请求
req, err := http.NewRequest("POST", fmt.Sprintf("%s/metrics", config.ServerURL), bytes.NewBuffer(jsonData))
req, err := http.NewRequest("POST", fmt.Sprintf("%s/metrics/", config.ServerURL), bytes.NewBuffer(jsonData))
if err != nil {
return err
}