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:
Akizon77
2025-05-16 19:56:22 +08:00
parent b156e93409
commit e34367de69
26 changed files with 752 additions and 461 deletions

62
monitoring/unit/cpu.go Normal file
View File

@@ -0,0 +1,62 @@
package monitoring
import (
"runtime"
"strconv"
"strings"
"time"
"github.com/shirou/gopsutil/cpu"
)
type CpuInfo struct {
CPUName string `json:"cpu_name"`
CPUArchitecture string `json:"cpu_architecture"`
CPUCores int `json:"cpu_cores"`
CPUUsage float64 `json:"cpu_usage"`
}
func Cpu() CpuInfo {
cpuinfo := CpuInfo{}
info, err := cpu.Info()
if err != nil {
cpuinfo.CPUName = "Unknown"
}
// multiple CPU
// 多个 CPU
if len(info) > 1 {
cpuCountMap := make(map[string]int)
for _, cpu := range info {
cpuCountMap[cpu.ModelName]++
}
for modelName, count := range cpuCountMap {
if count > 1 {
cpuinfo.CPUName += modelName + " x " + strconv.Itoa(count) + ", "
} else {
cpuinfo.CPUName += modelName + ", "
}
}
cpuinfo.CPUName = cpuinfo.CPUName[:len(cpuinfo.CPUName)-2] // Remove trailing comma and space
} else if len(info) == 1 {
cpuinfo.CPUName = info[0].ModelName
}
cpuinfo.CPUName = strings.TrimSpace(cpuinfo.CPUName)
cpuinfo.CPUArchitecture = runtime.GOARCH
cores, err := cpu.Counts(true)
if err != nil {
cpuinfo.CPUCores = 1 // Error case
}
cpuinfo.CPUCores = cores
// Get CPU Usage
percentages, err := cpu.Percent(1*time.Second, false)
if err != nil {
cpuinfo.CPUUsage = 0.0 // Error case
} else {
cpuinfo.CPUUsage = percentages[0]
}
return cpuinfo
}

35
monitoring/unit/disk.go Normal file
View File

@@ -0,0 +1,35 @@
package monitoring
import (
"github.com/shirou/gopsutil/disk"
)
type DiskInfo struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
}
func Disk() DiskInfo {
diskinfo := DiskInfo{}
usage, err := disk.Partitions(true)
if err != nil {
diskinfo.Total = 0
diskinfo.Used = 0
} else {
for _, part := range usage {
if part.Mountpoint != "/tmp" && part.Mountpoint != "/var/tmp" && part.Mountpoint != "/dev/shm" {
// Skip /tmp, /var/tmp, and /dev/shm
// 获取磁盘使用情况
u, err := disk.Usage(part.Mountpoint)
if err != nil {
diskinfo.Total = 0
diskinfo.Used = 0
} else {
diskinfo.Total += u.Total
diskinfo.Used += u.Used
}
}
}
}
return diskinfo
}

1
monitoring/unit/gpu.go Normal file
View File

@@ -0,0 +1 @@
package monitoring

80
monitoring/unit/ip.go Normal file
View File

@@ -0,0 +1,80 @@
package monitoring
import (
"io"
"net/http"
"regexp"
)
var userAgent = "curl/8.0.1"
func GetIPv4Address() (string, error) {
webAPIs := []string{"https://api.bilibili.com/x/web-interface/zone", "https://ip.sb", "https://api.ipify.org?format=json"}
for _, api := range webAPIs {
// get ipv4
req, err := http.NewRequest("GET", api, nil)
if err != nil {
continue
}
req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
continue
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
continue
}
// 使用正则表达式从响应体中提取IPv4地址
re := regexp.MustCompile(`\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}`)
ipv4 := re.FindString(string(body))
if ipv4 != "" {
return ipv4, nil
}
}
return "", nil
}
func GetIPv6Address() (string, error) {
webAPIs := []string{"https://api6.ipify.org?format=json", "https://ipv6.icanhazip.com"}
for _, api := range webAPIs {
// get ipv6
req, err := http.NewRequest("GET", api, nil)
if err != nil {
continue
}
req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultClient.Do(req)
if err != nil {
continue
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
continue
}
// 使用正则表达式从响应体中提取IPv6地址
re := regexp.MustCompile(`([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])`)
ipv6 := re.FindString(string(body))
if ipv6 != "" {
return ipv6, nil
}
}
return "", nil
}
func GetIPAddress() (ipv4, ipv6 string, err error) {
ipv4, err = GetIPv4Address()
if err != nil {
ipv4 = ""
}
ipv6, err = GetIPv6Address()
if err != nil {
ipv6 = ""
}
return ipv4, ipv6, nil
}

25
monitoring/unit/load.go Normal file
View File

@@ -0,0 +1,25 @@
package monitoring
import (
"github.com/shirou/gopsutil/load"
)
type LoadInfo struct {
Load1 float64 `json:"load_1"`
Load5 float64 `json:"load_5"`
Load15 float64 `json:"load_15"`
}
func Load() LoadInfo {
avg, err := load.Avg()
if err != nil {
return LoadInfo{Load1: 0, Load5: 0, Load15: 0}
}
return LoadInfo{
Load1: avg.Load1,
Load5: avg.Load5,
Load15: avg.Load15,
}
}

35
monitoring/unit/mem.go Normal file
View File

@@ -0,0 +1,35 @@
package monitoring
import (
"github.com/shirou/gopsutil/mem"
)
type RamInfo struct {
Total uint64 `json:"total"`
Used uint64 `json:"used"`
}
func Ram() RamInfo {
raminfo := RamInfo{}
v, err := mem.VirtualMemory()
if err != nil {
raminfo.Total = 0
raminfo.Used = 0
} else {
raminfo.Total = v.Total
raminfo.Used = v.Used
}
return raminfo
}
func Swap() RamInfo {
swapinfo := RamInfo{}
s, err := mem.SwapMemory()
if err != nil {
swapinfo.Total = 0
swapinfo.Used = 0
} else {
swapinfo.Total = s.Total
swapinfo.Used = s.Used
}
return swapinfo
}

83
monitoring/unit/net.go Normal file
View 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
}

View File

@@ -0,0 +1,32 @@
//go:build !windows
// +build !windows
package monitoring
import (
"bufio"
"os"
"strings"
)
func OSName() string {
file, err := os.Open("/etc/os-release")
if err != nil {
return "Linux"
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "PRETTY_NAME=") {
return strings.Trim(line[len("PRETTY_NAME="):], `"`)
}
}
if err := scanner.Err(); err != nil {
return "Linux"
}
return "Linux"
}

View File

@@ -0,0 +1,23 @@
//go:build windows
// +build windows
package monitoring
import (
"golang.org/x/sys/windows/registry"
)
func OSName() string {
key, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return "Microsoft Windows"
}
defer key.Close()
productName, _, err := key.GetStringValue("ProductName")
if err != nil {
return "Microsoft Windows"
}
return productName
}

View File

@@ -0,0 +1,33 @@
//go:build !windows
// +build !windows
package monitoring
import (
"os"
"strconv"
)
// ProcessCount returns the number of running processes
func ProcessCount() (count int) {
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
}

View File

@@ -0,0 +1,56 @@
//go:build windows
// +build windows
package monitoring
import (
"syscall"
"unsafe"
)
// ProcessCount returns the number of running processes
func ProcessCount() (count int) {
return processCountWindows()
}
// 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
}

11
monitoring/unit/uptime.go Normal file
View File

@@ -0,0 +1,11 @@
package monitoring
import (
"github.com/shirou/gopsutil/host"
)
func Uptime() (uint64, error) {
return host.Uptime()
}