-
Notifications
You must be signed in to change notification settings - Fork 3
/
disk_gopsutil.go
75 lines (67 loc) · 1.99 KB
/
disk_gopsutil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//go:build 386 || amd64 || arm || arm64
package diskKit
import (
"fmt"
"github.com/richelieu-yang/chimera/v3/src/core/conditionKit"
"github.com/richelieu-yang/chimera/v3/src/core/osKit"
"github.com/richelieu-yang/chimera/v3/src/dataSizeKit"
"github.com/shirou/gopsutil/v3/disk"
"github.com/sirupsen/logrus"
)
func GetDiskUsageStats() (*DiskUsageStats, error) {
path := conditionKit.TernaryOperator(osKit.IsWindows(), "C:", "/")
return GetDiskUsageStatsByPath(path)
}
// GetDiskUsageStatsByPath
/*
PS:
(1) Mac(Linux),查看磁盘空间的命令: df -h
golang 获取cpu 内存 硬盘 使用率 信息 进程信息
https://blog.csdn.net/whatday/article/details/109620192
*/
func GetDiskUsageStatsByPath(path string) (*DiskUsageStats, error) {
usageStat, err := disk.Usage(path)
if err != nil {
return nil, err
}
return &DiskUsageStats{
Path: path,
Free: usageStat.Free,
Used: usageStat.Used,
Total: usageStat.Total,
UsedPercent: usageStat.UsedPercent,
}, nil
//parts, err := disk.Partitions(true)
//if err != nil {
// return nil, err
//}
//for _, part := range parts {
// if part.Mountpoint != "/" {
// continue
// }
// usageStat, err := disk.Usage(part.Mountpoint)
// if err != nil {
// return nil, err
// }
// return (*DiskUsageStats)(usageStat), nil
//}
//return nil, errorKit.Newf("fail to get disk usageStat with parts(length: %d)", len(parts))
}
func PrintBasicDetails(logger *logrus.Logger) {
if logger == nil {
logger = logrus.StandardLogger()
}
stats, err := GetDiskUsageStats()
if err != nil {
logger.WithError(err).Warn("[CHIMERA, DISK] Fail to get disk usage stats.")
return
}
str := fmt.Sprintf("path: %s, free: %s, used: %s, total: %s, used percent: %.2f%%",
stats.Path,
dataSizeKit.ToReadableIecString(float64(stats.Free)),
dataSizeKit.ToReadableIecString(float64(stats.Used)),
dataSizeKit.ToReadableIecString(float64(stats.Total)),
stats.UsedPercent,
)
logger.Infof("[CHIMERA, DISK] disk usage stats: [%s].", str)
}