Skip to content

Commit

Permalink
Added serial_number to system_profile per OHAI-395. The current code is
Browse files Browse the repository at this point in the history
a little ugly, it sets the attribute to an array and uses the 'mini'
detailLevel.  I defined a hash to choose the detail level per dataType,
iterate through an array for each detail level (this is to only shell
out once per detail level rather than each dataType) and finally sort
the array to make sure it's in the same order as before my changes
compatibility.

The result is this is compatible with the old structure and:
  node['system_profile'][10]["_items"][0]["serial_number"]
will return the serial number.  Changing this to:
  node['system_profile'][SPHardwareDataType"]["serial_number"]
would be preferable but break compatibility.
  • Loading branch information
Paul Mooring authored and btm committed Dec 6, 2012
1 parent cf6e0d4 commit 4019448
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions lib/ohai/plugins/darwin/system_profiler.rb
Expand Up @@ -22,12 +22,51 @@
require 'plist'

system_profile Array.new
popen4("system_profiler -xml -detailLevel mini") do |pid, stdin, stdout, stderr|
stdin.close
Plist::parse_xml(stdout.read).each do |e|
system_profile << e

detail_level = {
'mini' => [
"SPParallelATAData",
"SPAudioData",
"SPBluetoothData",
"SPCardReaderData",
"SPDiagnosticsData",
"SPDiscBurningData",
"SPEthernetData",
"SPFibreChannelData",
"SPFireWireData",
"SPDisplaysData",
"SPHardwareRAIDData",
"SPMemoryData",
"SPModemData",
"SPNetworkData",
"SPPCIData",
"SPParallelSCSIData",
"SPPrintersSoftwareData",
"SPPrintersData",
"SPSASData",
"SPSerialATAData",
"SPSoftwareData",
"SPThunderboltData",
"SPUSBData",
"SPWWANData",
"SPAirPortData"
],
'full' => [
"SPHardwareDataType"
]
}

detail_level.each do |level, data_types|
popen4("system_profiler -xml -detailLevel #{level} #{data_types.join(' ')}") do |pid, stdin, stdout, stderr|
stdin.close
Plist::parse_xml(stdout.read).each do |e|
system_profile << e
end
end
end

system_profile.sort_by! { |h| h['_dataType'] }

rescue LoadError => e
Ohai::Log.debug("Can't load gem: #{e})")
end
end

0 comments on commit 4019448

Please sign in to comment.