From 43d805cf5daac29531a2980c2ee3c4365f63b434 Mon Sep 17 00:00:00 2001 From: Ties de Wit Date: Thu, 25 Aug 2022 09:55:04 +0200 Subject: [PATCH] Fix for diskusage Ceph mount This sets `used` and `usedPercent` correctly in #1344 --- disk/disk_unix.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/disk/disk_unix.go b/disk/disk_unix.go index bdb62b2..6d2c3fd 100644 --- a/disk/disk_unix.go +++ b/disk/disk_unix.go @@ -26,6 +26,16 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { InodesTotal: (uint64(stat.Files)), InodesFree: (uint64(stat.Ffree)), } + + ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize) + + if (ret.Used + ret.Free) == 0 { + ret.UsedPercent = 0 + } else { + // We don't use ret.Total to calculate percent. + // see https://github.com/shirou/gopsutil/issues/562 + ret.UsedPercent = (float64(ret.Used) / float64(ret.Used+ret.Free)) * 100.0 + } // if could not get InodesTotal, return empty if ret.InodesTotal < ret.InodesFree { @@ -33,7 +43,6 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { } ret.InodesUsed = (ret.InodesTotal - ret.InodesFree) - ret.Used = (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(bsize) if ret.InodesTotal == 0 { ret.InodesUsedPercent = 0 @@ -41,14 +50,6 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) { ret.InodesUsedPercent = (float64(ret.InodesUsed) / float64(ret.InodesTotal)) * 100.0 } - if (ret.Used + ret.Free) == 0 { - ret.UsedPercent = 0 - } else { - // We don't use ret.Total to calculate percent. - // see https://github.com/shirou/gopsutil/issues/562 - ret.UsedPercent = (float64(ret.Used) / float64(ret.Used+ret.Free)) * 100.0 - } - return ret, nil }