From 1da46b62a128e481d657250325eb62e6fa129199 Mon Sep 17 00:00:00 2001 From: spiritlhl <103393591+spiritLHLS@users.noreply.github.com> Date: Sat, 4 May 2024 11:54:12 +0000 Subject: [PATCH] update --- system/cpu.go | 2 +- system/disk.go | 55 +++++++++++++++++++++++++++++++++++------------- system/system.go | 3 ++- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/system/cpu.go b/system/cpu.go index 3d5b532..ee6b520 100644 --- a/system/cpu.go +++ b/system/cpu.go @@ -47,7 +47,7 @@ func getCpuInfo(ret *model.SystemInfo, cpuType string) (*model.SystemInfo, error if len(ret.CpuModel) < len(ci[i].ModelName) { ret.CpuModel = ci[i].ModelName + fmt.Sprintf(" %d %s Core", len(ci), cpuType) + " @ " + strconv.FormatFloat(ci[i].Mhz, 'f', 2, 64) + " MHz" - ret.CpuCores = fmt.Sprintf("%d vCPU(s)", int(ci[i].Cores)) + ret.CpuCores = fmt.Sprintf("%d vCPU(s)", runtime.NumCPU()) if ci[i].CacheSize != 0 { // Windows查不到CPU的三缓 ret.CpuCache = string(ci[i].CacheSize) } diff --git a/system/disk.go b/system/disk.go index b10cb4e..68089a0 100644 --- a/system/disk.go +++ b/system/disk.go @@ -1,13 +1,15 @@ package system import ( - "github.com/shirou/gopsutil/disk" "os/exec" "runtime" "strconv" "strings" + + "github.com/shirou/gopsutil/disk" ) +// getDiskInfo 获取硬盘信息 func getDiskInfo() (string, string, string, error) { var diskTotalStr, diskUsageStr, bootPath string tempDiskTotal, tempDiskUsage := getDiskTotalAndUsed() @@ -24,21 +26,44 @@ func getDiskInfo() (string, string, string, error) { } else { diskUsageStr = strconv.FormatFloat(diskUsageGB, 'f', 2, 64) + " GB" } - parts, err := disk.Partitions(true) - if err != nil { - bootPath = "" + if runtime.GOOS == "windows" { + parts, err := disk.Partitions(true) + if err != nil { + bootPath = "" + } else { + for _, part := range parts { + if part.Fstype == "tmpfs" { + continue + } + usageStat, err := disk.Usage(part.Mountpoint) + if err != nil { + continue + } + if usageStat.Total > 0 { + bootPath = part.Mountpoint + break + } + } + } } else { - for _, part := range parts { - if part.Fstype == "tmpfs" { - continue - } - usageStat, err := disk.Usage(part.Mountpoint) - if err != nil { - continue - } - if usageStat.Total > 0 { - bootPath = part.Mountpoint - break + cmd := exec.Command("df", "-x", "tmpfs", "/") + output, err := cmd.Output() + if err == nil { + awkCmd := exec.Command("awk", "NR>1") + awkCmd.Stdin = strings.NewReader(string(output)) + awkOutput, err := awkCmd.Output() + if err == nil { + sedCmd := exec.Command("sed", ":a;N;s/\\n//g;ta") + sedCmd.Stdin = strings.NewReader(string(awkOutput)) + sedOutput, err := sedCmd.Output() + if err != nil { + finalAwkCmd := exec.Command("awk", "{print $1}") + finalAwkCmd.Stdin = strings.NewReader(string(sedOutput)) + finalOutput, err := finalAwkCmd.Output() + if err != nil { + bootPath = string(finalOutput) + } + } } } } diff --git a/system/system.go b/system/system.go index 9667f04..ed494d5 100644 --- a/system/system.go +++ b/system/system.go @@ -2,9 +2,10 @@ package system import ( "fmt" + "strconv" + "github.com/oneclickvirt/basics/system/model" "github.com/oneclickvirt/basics/system/utils" - "strconv" ) var (