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

23
main.go
View File

@@ -4,6 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"io"
"komari/config"
"komari/monitoring"
"log"
@@ -181,8 +182,14 @@ func uploadBasicInfo(endpoint string, token string) error {
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
message := string(body)
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("status code: %d", resp.StatusCode)
return fmt.Errorf("status code: %d,%s", resp.StatusCode, message)
}
return nil
@@ -229,13 +236,15 @@ func report(localConfig config.LocalConfig, remoteConfig config.RemoteConfig) []
}
}
if remoteConfig.Network {
networkUp, networkDown, err := monitoring.NetworkSpeed()
totalUp, totalDown, networkUp, networkDown, err := monitoring.NetworkSpeed(remoteConfig.Interval)
if err != nil {
message += fmt.Sprintf("failed to get network speed: %v\n", err)
}
data["network"] = map[string]interface{}{
"up": networkUp,
"down": networkDown,
"up": networkUp,
"down": networkDown,
"totalUp": totalUp,
"totalDown": totalDown,
}
}
if remoteConfig.Connections {
@@ -243,7 +252,7 @@ func report(localConfig config.LocalConfig, remoteConfig config.RemoteConfig) []
if err != nil {
message += fmt.Sprintf("failed to get connections: %v\n", err)
}
data["network"] = map[string]interface{}{
data["connections"] = map[string]interface{}{
"tcp": tcpCount,
"udp": udpCount,
}
@@ -255,6 +264,10 @@ func report(localConfig config.LocalConfig, remoteConfig config.RemoteConfig) []
}
data["uptime"] = uptime
}
if remoteConfig.Process {
processcount := monitoring.ProcessCount()
data["process"] = processcount
}
data["message"] = message
s, err := json.Marshal(data)