From a449813c7dc7c75738a7937f252883bb20289268 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Tue, 26 Jul 2022 23:14:30 -0700 Subject: Fall back to /proc/cpuinfo if lscpu doesn't have CPU totals https://github.com/chef/ohai/pull/1454 made `lscpu` the primary way Ohai gathers CPU data, but the parser doesn't handle the output from a Raspberry Pi 4 Cortex-A72. For example, an ARM cluster has the output `Core(s) per cluster` instead of `Core(s) per socket`. We should fall back to `/proc/cpuinfo` if we don't get reasonable data back. It's not enough just to have `lscpu` output. Closes https://github.com/chef/ohai/issues/1760 Signed-off-by: Stan Hu --- lib/ohai/plugins/cpu.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/ohai/plugins/cpu.rb b/lib/ohai/plugins/cpu.rb index e26a7ace..1d37c39f 100644 --- a/lib/ohai/plugins/cpu.rb +++ b/lib/ohai/plugins/cpu.rb @@ -344,15 +344,23 @@ Ohai.plugin(:CPU) do cpuinfo end + # Check if the `lscpu` data looks reasonable + def valid_lscpu?(lscpu) + return false if lscpu.empty? + return false if %i{total real cores}.any? { |key| lscpu[key].to_i == 0 } + + true + end + collect_data(:linux) do cpuinfo = parse_cpuinfo lscpu = parse_lscpu(cpuinfo) - # If we don't have any data from lscpu then get it from /proc/cpuinfo - if lscpu.empty? - cpu cpuinfo - else + # If we don't have any sensible data from lscpu then get it from /proc/cpuinfo + if valid_lscpu?(lscpu) cpu lscpu + else + cpu cpuinfo end end -- cgit v1.2.1