From c0130fdb7cfa8bf2b1f949807c15dc3236eed7a9 Mon Sep 17 00:00:00 2001 From: TaterLi Date: Thu, 25 Nov 2021 18:50:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=A3=81=E7=9B=98=E7=A9=BA?= =?UTF-8?q?=E9=97=B4=E8=8E=B7=E5=8F=96=E7=9A=84Fallback=E6=96=B9=E6=B3=95,?= =?UTF-8?q?=E5=BA=94=E5=AF=B9OVZ=E6=97=A0=E6=B3=95=E7=BB=9F=E8=AE=A1?= =?UTF-8?q?=E9=97=AE=E9=A2=98!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/agent/monitor/monitor.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/cmd/agent/monitor/monitor.go b/cmd/agent/monitor/monitor.go index 5f79d28..855601b 100644 --- a/cmd/agent/monitor/monitor.go +++ b/cmd/agent/monitor/monitor.go @@ -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 }