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
}

77
monitoring/process.go Normal file
View File

@@ -0,0 +1,77 @@
package monitoring
import (
"os"
"runtime"
"strconv"
"syscall"
"unsafe"
)
// ProcessCount returns the number of running processes
func ProcessCount() (count int) {
if runtime.GOOS == "windows" {
return processCountWindows()
}
return processCountLinux()
}
// processCountLinux counts processes by reading /proc directory
func processCountLinux() (count int) {
procDir := "/proc"
entries, err := os.ReadDir(procDir)
if err != nil {
return 0
}
for _, entry := range entries {
if _, err := strconv.ParseInt(entry.Name(), 10, 64); err == nil {
//if _, err := filepath.ParseInt(entry.Name(), 10, 64); err == nil {
count++
}
}
return count
}
// processCountWindows counts processes using Windows API
func processCountWindows() (count int) {
// Load kernel32.dll
kernel32, err := syscall.LoadLibrary("kernel32.dll")
if err != nil {
return 0
}
defer syscall.FreeLibrary(kernel32)
// Get EnumProcesses function
enumProcesses, err := syscall.GetProcAddress(kernel32, "K32EnumProcesses")
if err != nil {
return 0
}
// Prepare buffer for process IDs
const maxProcesses = 1024
pids := make([]uint32, maxProcesses)
var bytesReturned uint32
// Call EnumProcesses
ret, _, _ := syscall.SyscallN(
uintptr(enumProcesses),
uintptr(unsafe.Pointer(&pids[0])),
uintptr(len(pids)*4),
uintptr(unsafe.Pointer(&bytesReturned)),
)
if ret == 0 {
return 0
}
// Count valid PIDs
count = int(bytesReturned) / 4 // bytesReturned is size in bytes, divide by 4 for uint32 count
if count > maxProcesses {
count = maxProcesses
}
return count
}