From ef2c63893218f646de9356820de478cb454d725b Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Mon, 9 Jun 2025 00:57:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20CPU=20=E5=92=8C?= =?UTF-8?q?=E7=A3=81=E7=9B=98=E4=BF=A1=E6=81=AF=E8=8E=B7=E5=8F=96=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E5=AF=B9=20Server=20=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=94=AF=E6=8C=81=EF=BC=8C=E6=94=B9=E8=BF=9B=E7=BB=88?= =?UTF-8?q?=E7=AB=AF=E5=85=B3=E9=97=AD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- monitoring/unit/cpu.go | 36 ++++++++++++++++++----------------- monitoring/unit/disk.go | 4 ++++ monitoring/unit/os_windows.go | 4 ++++ terminal/terminal_unix.go | 24 +++++++++++++++++++++-- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/monitoring/unit/cpu.go b/monitoring/unit/cpu.go index 29ea7e3..e86fa6e 100644 --- a/monitoring/unit/cpu.go +++ b/monitoring/unit/cpu.go @@ -2,7 +2,6 @@ package monitoring import ( "runtime" - "strconv" "strings" "time" @@ -22,24 +21,27 @@ func Cpu() CpuInfo { 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 + ", " + /* + // 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 = cpuinfo.CPUName[:len(cpuinfo.CPUName)-2] // Remove trailing comma and space - } else if len(info) == 1 { - cpuinfo.CPUName = info[0].ModelName - } + */ + cpuinfo.CPUName = info[0].ModelName cpuinfo.CPUName = strings.TrimSpace(cpuinfo.CPUName) diff --git a/monitoring/unit/disk.go b/monitoring/unit/disk.go index 5222d9f..6a3886b 100644 --- a/monitoring/unit/disk.go +++ b/monitoring/unit/disk.go @@ -36,6 +36,10 @@ func Disk() DiskInfo { // isPhysicalDisk 判断分区是否为物理磁盘 func isPhysicalDisk(part disk.PartitionStat) bool { + // 对于LXC等基于loop的根文件系统,始终包含根挂载点 + if part.Mountpoint == "/" { + return true + } mountpoint := strings.ToLower(part.Mountpoint) // 临时文件系统 if mountpoint == "/tmp" || mountpoint == "/var/tmp" || mountpoint == "/dev/shm" || diff --git a/monitoring/unit/os_windows.go b/monitoring/unit/os_windows.go index 68c3b43..7136e34 100644 --- a/monitoring/unit/os_windows.go +++ b/monitoring/unit/os_windows.go @@ -21,6 +21,10 @@ func OSName() string { if err != nil { return "Microsoft Windows" } + // 如果是 Server 版本,直接返回原始名称 + if strings.Contains(productName, "Server") { + return productName + } // Windows 11 majorVersion, _, err := key.GetIntegerValue("CurrentMajorVersionNumber") diff --git a/terminal/terminal_unix.go b/terminal/terminal_unix.go index 1c36477..b33a806 100644 --- a/terminal/terminal_unix.go +++ b/terminal/terminal_unix.go @@ -8,6 +8,7 @@ import ( "os/exec" "strings" "syscall" + "time" "github.com/creack/pty" ) @@ -94,9 +95,28 @@ type unixTerminal struct { func (t *unixTerminal) Close() error { pgid, err := syscall.Getpgid(t.cmd.Process.Pid) if err != nil { - return t.cmd.Process.Kill() + pgid = t.cmd.Process.Pid } - return syscall.Kill(-pgid, syscall.SIGKILL) + _ = syscall.Kill(-pgid, syscall.SIGTERM) + done := make(chan error, 1) + go func() { + done <- t.cmd.Wait() + }() + select { + case err := <-done: + if err == nil { + } else { + if exitErr, ok := err.(*exec.ExitError); ok && exitErr.Exited() { + return nil + } + return fmt.Errorf("process group did not exit gracefully: %v", err) + } + return err + case <-time.After(5 * time.Second): + } + // 超时未退出,强制 SIGKILL + _ = syscall.Kill(-pgid, syscall.SIGKILL) + return <-done } func (t *unixTerminal) Read(p []byte) (int, error) {