diff options
author | SandroCasagrande <SandroCasagrande@users.noreply.github.com> | 2022-05-13 22:42:01 +0000 |
---|---|---|
committer | Sandro Casagrande <sc.casagrande@gmail.com> | 2022-05-14 00:34:56 +0000 |
commit | 5b2d5c365b41b35de0bda1b8a486740529458fc8 (patch) | |
tree | 31862ae848be84ab302d2405365ff8d5d8288751 /libpod/info.go | |
parent | bde8dba877fe97130faf9c8bee678b9cfc23219a (diff) | |
download | podman-5b2d5c365b41b35de0bda1b8a486740529458fc8.tar.gz podman-5b2d5c365b41b35de0bda1b8a486740529458fc8.tar.bz2 podman-5b2d5c365b41b35de0bda1b8a486740529458fc8.zip |
Robust whitespace split of cpu utilization line from /proc/stat
Signed-off-by: Sandro Casagrande <sc.casagrande@gmail.com>
Diffstat (limited to 'libpod/info.go')
-rw-r--r-- | libpod/info.go | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/libpod/info.go b/libpod/info.go index 321680a81..bc49a6cc9 100644 --- a/libpod/info.go +++ b/libpod/info.go @@ -406,26 +406,25 @@ func getCPUUtilization() (*define.CPUUsage, error) { } defer f.Close() scanner := bufio.NewScanner(f) - // Read firt line of /proc/stat + // Read first line of /proc/stat that has entries for system ("cpu" line) for scanner.Scan() { break } // column 1 is user, column 3 is system, column 4 is idle - stats := strings.Split(scanner.Text(), " ") + stats := strings.Fields(scanner.Text()) return statToPercent(stats) } func statToPercent(stats []string) (*define.CPUUsage, error) { - // There is always an extra space between cpu and the first metric - userTotal, err := strconv.ParseFloat(stats[2], 64) + userTotal, err := strconv.ParseFloat(stats[1], 64) if err != nil { return nil, errors.Wrapf(err, "unable to parse user value %q", stats[1]) } - systemTotal, err := strconv.ParseFloat(stats[4], 64) + systemTotal, err := strconv.ParseFloat(stats[3], 64) if err != nil { return nil, errors.Wrapf(err, "unable to parse system value %q", stats[3]) } - idleTotal, err := strconv.ParseFloat(stats[5], 64) + idleTotal, err := strconv.ParseFloat(stats[4], 64) if err != nil { return nil, errors.Wrapf(err, "unable to parse idle value %q", stats[4]) } |