mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 02:59:23 +08:00
feat: 优化 CPU 和磁盘信息获取,增强对 Server 版本的支持,改进终端关闭逻辑
This commit is contained in:
@@ -2,7 +2,6 @@ package monitoring
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -22,24 +21,27 @@ func Cpu() CpuInfo {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
cpuinfo.CPUName = "Unknown"
|
cpuinfo.CPUName = "Unknown"
|
||||||
}
|
}
|
||||||
// multiple CPU
|
/*
|
||||||
// 多个 CPU
|
// multiple CPU
|
||||||
if len(info) > 1 {
|
// 多个 CPU
|
||||||
cpuCountMap := make(map[string]int)
|
if len(info) > 1 {
|
||||||
for _, cpu := range info {
|
cpuCountMap := make(map[string]int)
|
||||||
cpuCountMap[cpu.ModelName]++
|
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 + ", "
|
|
||||||
}
|
}
|
||||||
|
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)
|
cpuinfo.CPUName = strings.TrimSpace(cpuinfo.CPUName)
|
||||||
|
|
||||||
|
@@ -36,6 +36,10 @@ func Disk() DiskInfo {
|
|||||||
|
|
||||||
// isPhysicalDisk 判断分区是否为物理磁盘
|
// isPhysicalDisk 判断分区是否为物理磁盘
|
||||||
func isPhysicalDisk(part disk.PartitionStat) bool {
|
func isPhysicalDisk(part disk.PartitionStat) bool {
|
||||||
|
// 对于LXC等基于loop的根文件系统,始终包含根挂载点
|
||||||
|
if part.Mountpoint == "/" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
mountpoint := strings.ToLower(part.Mountpoint)
|
mountpoint := strings.ToLower(part.Mountpoint)
|
||||||
// 临时文件系统
|
// 临时文件系统
|
||||||
if mountpoint == "/tmp" || mountpoint == "/var/tmp" || mountpoint == "/dev/shm" ||
|
if mountpoint == "/tmp" || mountpoint == "/var/tmp" || mountpoint == "/dev/shm" ||
|
||||||
|
@@ -21,6 +21,10 @@ func OSName() string {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "Microsoft Windows"
|
return "Microsoft Windows"
|
||||||
}
|
}
|
||||||
|
// 如果是 Server 版本,直接返回原始名称
|
||||||
|
if strings.Contains(productName, "Server") {
|
||||||
|
return productName
|
||||||
|
}
|
||||||
|
|
||||||
// Windows 11
|
// Windows 11
|
||||||
majorVersion, _, err := key.GetIntegerValue("CurrentMajorVersionNumber")
|
majorVersion, _, err := key.GetIntegerValue("CurrentMajorVersionNumber")
|
||||||
|
@@ -8,6 +8,7 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/creack/pty"
|
"github.com/creack/pty"
|
||||||
)
|
)
|
||||||
@@ -94,9 +95,28 @@ type unixTerminal struct {
|
|||||||
func (t *unixTerminal) Close() error {
|
func (t *unixTerminal) Close() error {
|
||||||
pgid, err := syscall.Getpgid(t.cmd.Process.Pid)
|
pgid, err := syscall.Getpgid(t.cmd.Process.Pid)
|
||||||
if err != nil {
|
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) {
|
func (t *unixTerminal) Read(p []byte) (int, error) {
|
||||||
|
Reference in New Issue
Block a user