diff options
author | Tim Smith <tsmith@chef.io> | 2017-07-30 22:21:02 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-07-30 22:21:02 -0700 |
commit | 8edc4a9e1fcf7b0bdf61d30b4b2e253f746c1fc7 (patch) | |
tree | 93b85b881c80977d21b53cf4c3301602d33131ed | |
parent | 96e32f268ed7fa804cb605ba3d6e9acba74f9d9e (diff) | |
download | ohai-8edc4a9e1fcf7b0bdf61d30b4b2e253f746c1fc7.tar.gz |
Handle situations where /proc/cpuinfo lacks core data
This works around failures to collect core data on SLES <= 12 SP1. We need to support SLES and core counts are used in several of our product cookbooks.
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/linux/cpu.rb | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/lib/ohai/plugins/linux/cpu.rb b/lib/ohai/plugins/linux/cpu.rb index 0d6df732..be5667aa 100644 --- a/lib/ohai/plugins/linux/cpu.rb +++ b/lib/ohai/plugins/linux/cpu.rb @@ -82,8 +82,35 @@ Ohai.plugin(:CPU) do end cpu cpuinfo + cpu[:total] = cpu_number - cpu[:real] = real_cpu.keys.length - cpu[:cores] = real_cpu.keys.length * cpu["0"]["cores"].to_i + + # use data we collected unless cpuinfo is lacking core information + # which is the case on older linux distros (specifically SLES <= 12 SP1) + if !real_cpu.empty? && cpu["0"]["cores"] + cpu[:real] = real_cpu.keys.length + cpu[:cores] = real_cpu.keys.length * cpu["0"]["cores"].to_i + else + begin + Ohai::Log.debug("Plugin CPU: Falling back to aggregate data from lscpu as real cpu & core data is missing in /proc/cpuinfo") + so = shell_out("lscpu") + lscpu_data = Mash.new + so.stdout.each_line do |line| + case line + when /^Thread\(s\) per core:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q + lscpu_data[:threads] = $1.to_i + when /^Core\(s\) per socket:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q + lscpu_data[:cores] = $1.to_i + when /^Socket\(s\):\s(.+)/ # http://rubular.com/r/DIzmPtJFvK + lscpu_data[:sockets] = $1.to_i + end + end + cpu[:total] = lscpu_data[:sockets] * lscpu_data[:cores] * lscpu_data[:threads] + cpu[:real] = lscpu_data[:sockets] + cpu[:cores] = lscpu_data[:sockets] * lscpu_data[:cores] + rescue Ohai::Exceptions::Exec # util-linux isn't installed most likely + Ohai::Log.debug("Plugin CPU: Error executing lshw. util-linux may not be installed") + end + end end end |