From 61a68e5be13a2b0a935445005325e8d1dd3d7afd Mon Sep 17 00:00:00 2001 From: Henry Dollman Date: Sun, 15 Sep 2024 16:29:55 -0400 Subject: [PATCH] refactor findMaxReadsDevice to use disk.IOCounters --- beszel/internal/agent/agent.go | 39 +++++++++------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/beszel/internal/agent/agent.go b/beszel/internal/agent/agent.go index e8f44cb..0ff563a 100644 --- a/beszel/internal/agent/agent.go +++ b/beszel/internal/agent/agent.go @@ -658,36 +658,17 @@ func (a *Agent) closeIdleConnections(err error) (isTimeout bool) { // Returns the device with the most reads in /proc/diskstats // (fallback in case the root device is not supplied or detected) func findMaxReadsDevice() string { - content, err := os.ReadFile("/proc/diskstats") - if err != nil { - return "/" - } - - lines := strings.Split(string(content), "\n") - var maxReadsSectors int64 - var maxReadsDevice string - - for _, line := range lines { - fields := strings.Fields(line) - if len(fields) < 7 { - continue - } - - deviceName := fields[2] - readsSectors, err := strconv.ParseInt(fields[5], 10, 64) + var maxReadBytes uint64 + maxReadDevice := "/" + counters, err := disk.IOCounters() if err != nil { - continue - } - - if readsSectors > maxReadsSectors { - maxReadsSectors = readsSectors - maxReadsDevice = deviceName + return maxReadDevice + } + for _, d := range counters { + if d.ReadBytes > maxReadBytes { + maxReadBytes = d.ReadBytes + maxReadDevice = d.Name } } - - if maxReadsDevice == "" { - return "/" - } - - return maxReadsDevice + return maxReadDevice }