This commit is contained in:
spiritlhl
2024-05-04 11:54:12 +00:00
parent 81213bfb6b
commit 1da46b62a1
3 changed files with 43 additions and 17 deletions

View File

@@ -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)
}

View File

@@ -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)
}
}
}
}
}

View File

@@ -2,9 +2,10 @@ package system
import (
"fmt"
"strconv"
"github.com/oneclickvirt/basics/system/model"
"github.com/oneclickvirt/basics/system/utils"
"strconv"
)
var (