正确获取Linux磁盘下的挂载盘符以及磁盘使用率

dev
truthhun 10 months ago
parent b7b6a31688
commit abc27ea5aa

@ -3,6 +3,7 @@ package device
import ( import (
"fmt" "fmt"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/shirou/gopsutil/v3/cpu" "github.com/shirou/gopsutil/v3/cpu"
@ -51,28 +52,30 @@ func GetMemory() (memInfo MemInfo) {
func GetDisk() (diskInfos []DiskInfo) { func GetDisk() (diskInfos []DiskInfo) {
stats, _ := disk.Partitions(true) stats, _ := disk.Partitions(true)
if runtime.GOOS != "windows" {
usage, _ := disk.Usage("/")
diskInfos = append(diskInfos, DiskInfo{
DiskName: "/",
Total: usage.Total,
Used: usage.Used,
Free: usage.Free,
Percent: usage.UsedPercent,
})
return
}
for _, stat := range stats { for _, stat := range stats {
usage, _ := disk.Usage(stat.Mountpoint) usage, _ := disk.Usage(stat.Mountpoint)
if usage != nil { if usage == nil {
continue
}
if strings.ToLower(runtime.GOOS) == "windows" {
diskInfos = append(diskInfos, DiskInfo{ diskInfos = append(diskInfos, DiskInfo{
DiskName: stat.Mountpoint, DiskName: usage.Path,
Total: usage.Total, Total: usage.Total,
Used: usage.Used, Used: usage.Used,
Free: usage.Free, Free: usage.Free,
Percent: usage.UsedPercent, Percent: usage.UsedPercent,
}) })
} else {
fstype := strings.ToLower(usage.Fstype)
if strings.Contains(fstype, "ext") || strings.Contains(fstype, "ntfs") || strings.Contains(fstype, "fat") {
diskInfos = append(diskInfos, DiskInfo{
DiskName: usage.Path,
Total: usage.Total,
Used: usage.Used,
Free: usage.Free,
Percent: usage.UsedPercent,
})
}
} }
} }
return return

@ -481,7 +481,7 @@ export default {
}, },
{ {
label: 'Mhz', label: 'Mhz',
value: res.data.cpu.mhz, value: (res.data.cpu.mhz || 0).toFixed(0),
}, },
{ {
label: '', label: '',

Loading…
Cancel
Save