mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
Refactor monitoring package: remove platform-specific files and consolidate OS detection and process counting logic
- Deleted os_windows.go and process_windows.go, replacing them with platform-agnostic implementations in unit directory. - Removed Linux-specific process counting logic from process_linux.go and integrated it into unit. - Consolidated uptime and OS name retrieval into unit files for better organization. - Updated update mechanism to use global variables for current version and repository. - Introduced command-line flags for configuration, including disabling auto-update and web SSH. - Implemented WebSocket connection handling and terminal interaction for both Unix and Windows systems. - Added basic info upload functionality to server package, enhancing monitoring capabilities.
This commit is contained in:
84
monitoring/monitoring.go
Normal file
84
monitoring/monitoring.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
monitoring "github.com/komari-monitor/komari-agent/monitoring/unit"
|
||||
)
|
||||
|
||||
func GenerateReport() []byte {
|
||||
message := ""
|
||||
data := map[string]interface{}{}
|
||||
|
||||
cpu := monitoring.Cpu()
|
||||
cpuUsage := cpu.CPUUsage
|
||||
if cpuUsage <= 0.001 {
|
||||
cpuUsage = 0.001
|
||||
}
|
||||
data["cpu"] = map[string]interface{}{
|
||||
"usage": cpuUsage,
|
||||
}
|
||||
|
||||
ram := monitoring.Ram()
|
||||
data["ram"] = map[string]interface{}{
|
||||
"total": ram.Total,
|
||||
"used": ram.Used,
|
||||
}
|
||||
|
||||
swap := monitoring.Swap()
|
||||
data["swap"] = map[string]interface{}{
|
||||
"total": swap.Total,
|
||||
"used": swap.Used,
|
||||
}
|
||||
load := monitoring.Load()
|
||||
data["load"] = map[string]interface{}{
|
||||
"load1": load.Load1,
|
||||
"load5": load.Load5,
|
||||
"load15": load.Load15,
|
||||
}
|
||||
|
||||
disk := monitoring.Disk()
|
||||
data["disk"] = map[string]interface{}{
|
||||
"total": disk.Total,
|
||||
"used": disk.Used,
|
||||
}
|
||||
|
||||
totalUp, totalDown, networkUp, networkDown, err := monitoring.NetworkSpeed()
|
||||
if err != nil {
|
||||
message += fmt.Sprintf("failed to get network speed: %v\n", err)
|
||||
}
|
||||
data["network"] = map[string]interface{}{
|
||||
"up": networkUp,
|
||||
"down": networkDown,
|
||||
"totalUp": totalUp,
|
||||
"totalDown": totalDown,
|
||||
}
|
||||
|
||||
tcpCount, udpCount, err := monitoring.ConnectionsCount()
|
||||
if err != nil {
|
||||
message += fmt.Sprintf("failed to get connections: %v\n", err)
|
||||
}
|
||||
data["connections"] = map[string]interface{}{
|
||||
"tcp": tcpCount,
|
||||
"udp": udpCount,
|
||||
}
|
||||
|
||||
uptime, err := monitoring.Uptime()
|
||||
if err != nil {
|
||||
message += fmt.Sprintf("failed to get uptime: %v\n", err)
|
||||
}
|
||||
data["uptime"] = uptime
|
||||
|
||||
processcount := monitoring.ProcessCount()
|
||||
data["process"] = processcount
|
||||
|
||||
data["message"] = message
|
||||
|
||||
s, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Println("Failed to marshal data:", err)
|
||||
}
|
||||
return s
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
func ConnectionsCount() (tcpCount, udpCount int, err error) {
|
||||
tcps, err := net.Connections("tcp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get TCP connections: %w", err)
|
||||
}
|
||||
udps, err := net.Connections("udp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get UDP connections: %w", err)
|
||||
}
|
||||
|
||||
return len(tcps), len(udps), nil
|
||||
}
|
||||
|
||||
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, 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
|
||||
}
|
||||
|
||||
if len(ioCounters) == 0 {
|
||||
return 0, 0, 0, 0, fmt.Errorf("no network interfaces found")
|
||||
}
|
||||
|
||||
for _, interfaceStats := range ioCounters {
|
||||
loopbackNames := []string{"lo", "lo0", "localhost", "brd0", "docker0", "docker1", "veth0", "veth1", "veth2", "veth3", "veth4", "veth5", "veth6", "veth7"}
|
||||
isLoopback := false
|
||||
for _, name := range loopbackNames {
|
||||
if interfaceStats.Name == name {
|
||||
isLoopback = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if isLoopback {
|
||||
continue // Skip loopback interface
|
||||
}
|
||||
totalUp += interfaceStats.BytesSent
|
||||
totalDown += interfaceStats.BytesRecv
|
||||
|
||||
}
|
||||
upSpeed = (totalUp - lastUp) / uint64(interval)
|
||||
downSpeed = (totalDown - lastDown) / uint64(interval)
|
||||
|
||||
lastUp = totalUp
|
||||
lastDown = totalDown
|
||||
return totalUp, totalDown, upSpeed, downSpeed, nil
|
||||
}
|
83
monitoring/unit/net.go
Normal file
83
monitoring/unit/net.go
Normal file
@@ -0,0 +1,83 @@
|
||||
package monitoring
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/shirou/gopsutil/net"
|
||||
)
|
||||
|
||||
func ConnectionsCount() (tcpCount, udpCount int, err error) {
|
||||
tcps, err := net.Connections("tcp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get TCP connections: %w", err)
|
||||
}
|
||||
udps, err := net.Connections("udp")
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failed to get UDP connections: %w", err)
|
||||
}
|
||||
|
||||
return len(tcps), len(udps), nil
|
||||
}
|
||||
|
||||
var (
|
||||
// 预定义常见的回环和虚拟接口名称
|
||||
loopbackNames = map[string]struct{}{
|
||||
"lo": {}, "lo0": {}, "localhost": {},
|
||||
"brd0": {}, "docker0": {}, "docker1": {},
|
||||
"veth0": {}, "veth1": {}, "veth2": {}, "veth3": {},
|
||||
"veth4": {}, "veth5": {}, "veth6": {}, "veth7": {},
|
||||
}
|
||||
)
|
||||
|
||||
func NetworkSpeed() (totalUp, totalDown, upSpeed, downSpeed uint64, err error) {
|
||||
// 获取第一次网络IO计数器
|
||||
ioCounters1, err := net.IOCounters(false)
|
||||
if err != nil {
|
||||
return 0, 0, 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
|
||||
}
|
||||
|
||||
if len(ioCounters1) == 0 {
|
||||
return 0, 0, 0, 0, fmt.Errorf("no network interfaces found")
|
||||
}
|
||||
|
||||
// 统计第一次所有非回环接口的流量
|
||||
var totalUp1, totalDown1 uint64
|
||||
for _, interfaceStats := range ioCounters1 {
|
||||
// 使用映射表进行O(1)查找
|
||||
if _, isLoopback := loopbackNames[interfaceStats.Name]; isLoopback {
|
||||
continue // 跳过回环接口
|
||||
}
|
||||
totalUp1 += interfaceStats.BytesSent
|
||||
totalDown1 += interfaceStats.BytesRecv
|
||||
}
|
||||
|
||||
// 等待1秒
|
||||
time.Sleep(time.Second)
|
||||
|
||||
// 获取第二次网络IO计数器
|
||||
ioCounters2, err := net.IOCounters(false)
|
||||
if err != nil {
|
||||
return 0, 0, 0, 0, fmt.Errorf("failed to get network IO counters: %w", err)
|
||||
}
|
||||
|
||||
if len(ioCounters2) == 0 {
|
||||
return 0, 0, 0, 0, fmt.Errorf("no network interfaces found")
|
||||
}
|
||||
|
||||
// 统计第二次所有非回环接口的流量
|
||||
var totalUp2, totalDown2 uint64
|
||||
for _, interfaceStats := range ioCounters2 {
|
||||
if _, isLoopback := loopbackNames[interfaceStats.Name]; isLoopback {
|
||||
continue // 跳过回环接口
|
||||
}
|
||||
totalUp2 += interfaceStats.BytesSent
|
||||
totalDown2 += interfaceStats.BytesRecv
|
||||
}
|
||||
|
||||
// 计算速度 (每秒的速率)
|
||||
upSpeed = totalUp2 - totalUp1
|
||||
downSpeed = totalDown2 - totalDown1
|
||||
|
||||
return totalUp2, totalDown2, upSpeed, downSpeed, nil
|
||||
}
|
@@ -1,5 +1,5 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package monitoring
|
||||
|
@@ -1,5 +1,5 @@
|
||||
//go:build linux
|
||||
// +build linux
|
||||
//go:build !windows
|
||||
// +build !windows
|
||||
|
||||
package monitoring
|
||||
|
Reference in New Issue
Block a user