Merge pull request #143 from nickfox-taterli/master

添加磁盘空间获取的Fallback方法,应对OVZ无法统计问题!

Co-authored-by: nickfox-taterli <19658596+nickfox-taterli@users.noreply.github.com>
This commit is contained in:
naiba 2021-11-25 13:08:30 +01:00 committed by GitHub
commit fd46b28135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,6 +7,8 @@ import (
"strings"
"syscall"
"time"
"strconv"
"os/exec"
"github.com/shirou/gopsutil/v3/cpu"
"github.com/shirou/gopsutil/v3/disk"
@ -190,6 +192,30 @@ func getDiskTotalAndUsed() (total uint64, used uint64) {
}
used += diskUsageOf.Used
}
// Fallback 到这个方法,仅统计根路径,适用于OpenVZ之类的.
if runtime.GOOS == "linux" {
if total == 0 && used == 0 {
cmd := exec.Command("df")
out, err := cmd.CombinedOutput()
if err == nil {
s := strings.Split(string(out), "\n")
for _, c := range s {
info := strings.Fields(c)
if len(info) == 6 {
if info[5] == "/" {
total, _ = strconv.ParseUint(info[1], 0, 64)
used, _ = strconv.ParseUint(info[2], 0, 64)
// 默认获取的是1K块为单位的.
total = total * 1024
used = used * 1024
}
}
}
}
}
}
return
}