From 43d805cf5daac29531a2980c2ee3c4365f63b434 Mon Sep 17 00:00:00 2001 From: Ties de Wit Date: Thu, 25 Aug 2022 09:55:04 +0200 Subject: [PATCH 1/4] 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 bdb62b24d..6d2c3fd93 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 } From 705a63f1b232878b7994e9dcc3e6572e4787e454 Mon Sep 17 00:00:00 2001 From: Ties de Wit Date: Mon, 29 Aug 2022 15:44:23 +0200 Subject: [PATCH 2/4] Update disk_unix.go --- disk/disk_unix.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/disk/disk_unix.go b/disk/disk_unix.go index 6d2c3fd93..4cef8cbac 100644 --- a/disk/disk_unix.go +++ b/disk/disk_unix.go @@ -26,7 +26,7 @@ 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 { From e4409ef6a181430127b2de639353acb994253ef0 Mon Sep 17 00:00:00 2001 From: Ties de Wit Date: Thu, 1 Sep 2022 09:17:10 +0200 Subject: [PATCH 3/4] Update net_linux.go --- net/net_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/net/net_linux.go b/net/net_linux.go index c08997196..c7cd0db18 100644 --- a/net/net_linux.go +++ b/net/net_linux.go @@ -161,7 +161,7 @@ var netProtocols = []string{ // If protocols is empty then all protocols are returned, otherwise // just the protocols in the list are returned. // Available protocols: -// ip,icmp,icmpmsg,tcp,udp,udplite +// [ip,icmp,icmpmsg,tcp,udp,udplite] func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) { return ProtoCountersWithContext(context.Background(), protocols) } From f253d81d1bd0c80a4dae993e3f0ba76ee470adef Mon Sep 17 00:00:00 2001 From: Ties de Wit Date: Thu, 1 Sep 2022 09:18:20 +0200 Subject: [PATCH 4/4] Update common.go --- internal/common/common.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/common/common.go b/internal/common/common.go index adc4922bd..db93b6711 100644 --- a/internal/common/common.go +++ b/internal/common/common.go @@ -114,8 +114,8 @@ func ReadLines(filename string) ([]string, error) { // ReadLinesOffsetN reads contents from file and splits them by new line. // The offset tells at which line number to start. // The count determines the number of lines to read (starting from offset): -// n >= 0: at most n lines -// n < 0: whole file +// n >= 0: at most n lines +// n < 0: whole file func ReadLinesOffsetN(filename string, offset uint, n int) ([]string, error) { f, err := os.Open(filename) if err != nil {