feat(monitoring): 增加网络总量和进程计数监控

- 在 net.go 中添加总量统计功能,记录上次采样值
- 在 main.go 中添加进程计数监控
- 修改 remote.go 中的 JSON 字段名称
- 优化 report 函数,增加网络总量和进程计数数据
This commit is contained in:
Akizon77
2025-04-12 22:50:56 +08:00
parent c2a9148d4c
commit f785c0044c
4 changed files with 113 additions and 14 deletions

View File

@@ -19,15 +19,20 @@ func ConnectionsCount() (tcpCount, udpCount int, err error) {
return len(tcps), len(udps), nil
}
func NetworkSpeed() (upSpeed, downSpeed float64, err error) {
var (
lastUp uint64
lastDown uint64
)
func NetworkSpeed(interval int) (totalUp, totalDown, upSpeed, downSpeed uint64, err error) {
// Get the network IO counters
ioCounters, err := net.IOCounters(false)
if err != nil {
return 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
return 0, 0, 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
}
if len(ioCounters) == 0 {
return 0, 0, fmt.Errorf("no network interfaces found")
return 0, 0, 0, 0, fmt.Errorf("no network interfaces found")
}
for _, interfaceStats := range ioCounters {
@@ -42,10 +47,14 @@ func NetworkSpeed() (upSpeed, downSpeed float64, err error) {
if isLoopback {
continue // Skip loopback interface
}
upSpeed += float64(interfaceStats.BytesSent) / float64(interfaceStats.PacketsSent)
downSpeed += float64(interfaceStats.BytesRecv) / float64(interfaceStats.PacketsRecv)
totalUp += interfaceStats.BytesSent
totalDown += interfaceStats.BytesRecv
}
upSpeed = (totalUp - lastUp) / uint64(interval)
downSpeed = (totalDown - lastDown) / uint64(interval)
return upSpeed, downSpeed, nil
lastUp = totalUp
lastDown = totalDown
return totalUp, totalDown, upSpeed, downSpeed, nil
}