Skip to content

Commit

Permalink
Ohai darwin hardware plugin should catch command timeouts.
Browse files Browse the repository at this point in the history
On Big Sur, the ohai darwin hardwqare plugin can timeout when running
`system_profiler SPHardwareDataType -xml`. (On my laptop, it takes 6
minutes for this command to complete and return no data.) When this
command times out the plugin aborts and doesn't return any hardware
data. When this command times out the plugin continue so that
in can return some data.

Signed-off-by: Edward Pilatowicz [email protected]
  • Loading branch information
epilatow committed Dec 29, 2020
1 parent b90f861 commit d87c67d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/ohai/plugins/darwin/hardware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
def system_profiler(datatype)
sp_cmd = "system_profiler #{datatype} -xml"
# Hardware queries
sp_std = shell_out(sp_cmd)
Plist.parse_xml(sp_std.stdout)
begin
sp_std = shell_out(sp_cmd)
rescue Ohai::Exceptions::Exec => e
logger.error("Plugin Hardware: system_profiler #{datatype} query timeout: #{e.to_s}")
return nil
end
return Plist.parse_xml(sp_std.stdout)
end

collect_data(:darwin) do
Expand All @@ -38,8 +43,10 @@ def system_profiler(datatype)
require "plist"

hw_hash = system_profiler("SPHardwareDataType")
hw_hash[0]["_items"][0].delete("_name")
hardware.merge!(hw_hash[0]["_items"][0])
if hw_hash
hw_hash[0]["_items"][0].delete("_name")
hardware.merge!(hw_hash[0]["_items"][0])
end

# ProductName: Mac OS X
# ProductVersion: 10.15.6
Expand All @@ -60,7 +67,7 @@ def system_profiler(datatype)
# Storage queries
storage = []
storage_hash = system_profiler("SPStorageDataType")
drives = storage_hash[0]["_items"]
drives = storage_hash ? storage_hash[0]["_items"] : []
drives.each do |drive_entry|
drive = Mash.new
drive[:name] = drive_entry["_name"]
Expand All @@ -78,7 +85,7 @@ def system_profiler(datatype)

# Battery queries
battery_hash = system_profiler("SPPowerDataType")
power_entries = battery_hash[0]["_items"]
power_entries = battery_hash ? battery_hash[0]["_items"] : []
battery = Mash.new
power_entries.each do |entry|
if entry.value?("spbattery_information")
Expand Down

0 comments on commit d87c67d

Please sign in to comment.