1 Ohai.plugin(:Hardware) do
4 def read_sysctl_link(file)
5 File.basename(File.readlink(file))
9 def read_sysctl_file(file)
11 rescue Errno::ENOENT, Errno::EINVAL
14 def parse_memory_size(size)
15 if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
16 Regexp.last_match(1).to_f * 2**30
17 elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
18 Regexp.last_match(1).to_f * 2**20
19 elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
20 Regexp.last_match(1).to_f * 2**10
24 def format_disk_size(kb)
28 kblog10 = Math.log10(kb)
31 format "%gTB", 10**(kblog10 - 9)
33 format "%dGB", 10**(kblog10 - 6)
35 format "%dMB", 10**(kblog10 - 3)
40 def memory_to_disk_size(size)
41 format_disk_size(parse_memory_size(size))
44 def find_sas_device(address)
45 file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
46 read_sysctl_file(file) == "0x#{address}"
50 dir = File.dirname(file)
51 device = Dir.glob("#{dir}/block/*").first ||
52 Dir.glob("#{dir}/scsi_generic/*").first
54 "/dev/#{File.basename(device)}"
61 IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
62 if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
64 :slot => Regexp.last_match(1),
65 :domain => Regexp.last_match(2),
66 :bus => Regexp.last_match(3),
67 :device => Regexp.last_match(4),
68 :function => Regexp.last_match(5)
70 elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
71 case Regexp.last_match(1)
72 when "Class" then device[:class_name] = Regexp.last_match(2)
73 when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
74 when "Device" then device[:device_name] = Regexp.last_match(2)
75 when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
76 when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
77 when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
78 when "Rev" then device[:revision] = Regexp.last_match(2)
79 when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
80 when "Driver" then device[:driver] = Regexp.last_match(2)
81 when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
83 elsif device && line =~ /^\s*$/
84 devices[device[:slot]] = device
91 Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
92 name = File.basename(device)
95 :device => read_sysctl_link("#{device}/device"),
96 :duplex => read_sysctl_file("#{device}/duplex"),
97 :speed => read_sysctl_file("#{device}/speed")
98 }.delete_if { |_, v| v.nil? }
105 IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
106 if line =~ /^Memory Device\s*$/
108 elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
109 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
110 elsif device && line =~ /^\s*$/
120 disk[:controllers] = []
124 find_direct_disks(disk)
126 find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
127 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
128 find_mpt_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
130 # areca (/opt/areca/x86_64/cli64)
137 def find_direct_disks(devices)
138 Dir.glob("/sys/class/scsi_host/host*") do |host|
139 driver = read_sysctl_file("#{host}/proc_name")
141 if driver == "ahci" || driver == "mptsas"
142 bus = host.sub("/sys/class/scsi_host/host", "")
144 Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
145 next unless File.exist?("#{device}/scsi_disk")
147 block = Dir.glob("#{device}/block/*").first
148 vendor = read_sysctl_file("#{device}/vendor")
149 model = read_sysctl_file("#{device}/model")
150 size = read_sysctl_file("#{block}/size").to_i * 512
152 if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
153 vendor = Regexp.last_match(1)
154 model = Regexp.last_match(2)
158 :id => devices[:disks].count,
159 :device => "/dev/#{File.basename(block)}",
162 :firmware_version => read_sysctl_file("#{device}/rev"),
163 :size => format_disk_size(size),
171 def find_md_arrays(devices)
174 File.new("/proc/mdstat", "r").each do |line|
175 if line =~ /^(md\d+) : active raid(\d+)((?: [a-z]+\d+\[\d+\](?:\([A-Z]\))*)+)$/
177 :id => devices[:arrays].count,
178 :device => "/dev/#{Regexp.last_match(1)}",
179 :raid_level => Regexp.last_match(2),
183 Regexp.last_match(3).scan(/ ([a-z]+)\d+\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
184 if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
185 disk[:arrays] << array[:id]
186 array[:disks] << disk[:id]
190 devices[:arrays] << array
191 elsif line =~ /^\s+(\d+) blocks/
192 array[:size] = format_disk_size(Regexp.last_match(1).to_i)
197 def find_hp_disks(devices)
205 IO.popen(%w(hpssacli controller all show config detail)).each do |line|
206 if line =~ /^Smart Array (\S+) /
208 :id => devices[:controllers].count,
209 :model => Regexp.last_match(1),
214 devices[:controllers] << controller
216 controllers << controller
220 elsif controller && line =~ /^ (\S.*):\s+(.*)$/
221 case Regexp.last_match(1)
222 when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
223 when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
224 when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
225 when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
227 elsif controller && line =~ /^ Logical Drive: (\d+)$/
229 :id => devices[:arrays].count,
230 :controller => controller[:id],
231 :number => Regexp.last_match(1),
235 devices[:arrays] << array
236 controller[:arrays] << array[:id]
239 elsif controller && line =~ /^ physicaldrive (\S+) /
240 disks << Regexp.last_match(1)
241 elsif array && line =~ /^ physicaldrive (\S+)$/
243 :id => devices[:disks].count,
244 :controller => controller[:id],
245 :arrays => [array[:id]],
246 :location => Regexp.last_match(1),
247 :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
250 devices[:disks] << disk
251 controller[:disks] << disk[:id]
252 array[:disks] << disk[:id]
253 elsif disk && line =~ /^ (\S[^:]+):\s+(.*)$/
254 case Regexp.last_match(1)
255 when "Interface Type" then disk[:interface] = Regexp.last_match(2)
256 when "Size" then disk[:size] = Regexp.last_match(2)
257 when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
258 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
259 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
260 when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
262 elsif array && line =~ /^ (\S[^:]+):\s+(.*)$/
263 case Regexp.last_match(1)
264 when "Size" then array[:size] = Regexp.last_match(2)
265 when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
266 when "Disk Name" then array[:device] = Regexp.last_match(2).strip
267 when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
268 when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
273 controllers.each do |controller|
274 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
275 controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
276 elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
277 controller[:device] = "/dev/#{File.basename(device)}"
282 def find_megaraid_disks(devices)
290 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
291 if line =~ /^PCI information for Controller (\d+)$/
293 :id => devices[:controllers].count,
298 devices[:controllers] << controller
300 controllers << controller
301 elsif line =~ /^Bus Number\s+:\s+(\d+)$/
302 controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
303 elsif line =~ /^Device Number\s+:\s+(\d+)$/
304 controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
305 elsif line =~ /^Function Number\s+:\s+(\d+)$/
306 controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
310 IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
311 if line =~ /^Adapter #(\d+)$/
312 controller = controllers[Regexp.last_match(1).to_i]
313 elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
314 case Regexp.last_match(1)
315 when "Product Name" then controller[:model] = Regexp.last_match(2)
316 when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
317 when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
322 IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
323 if line =~ /^Adapter #(\d+)$/
324 controller = controllers[Regexp.last_match(1).to_i]
325 elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
326 pci_slot = controller[:pci_slot]
327 target = Regexp.last_match(2)
328 device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
331 :id => devices[:arrays].count,
332 :controller => controller[:id],
333 :number => Regexp.last_match(1),
334 :device => "/dev/#{File.basename(device)}",
338 devices[:arrays] << array
339 controller[:arrays] << array[:id]
344 elsif array && line =~ /^PD: (\d+) Information$/
346 :id => devices[:disks].count,
347 :controller => controller[:id],
348 :arrays => [array[:id]]
351 devices[:disks] << disk
352 controller[:disks] << disk[:id]
353 array[:disks] << disk[:id]
354 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
355 case Regexp.last_match(1)
356 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
357 when "WWN" then disk[:wwn] = Regexp.last_match(2)
358 when "PD Type" then disk[:interface] = Regexp.last_match(2)
359 when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
360 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
362 elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
363 case Regexp.last_match(1)
364 when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
365 when "Size" then array[:size] = Regexp.last_match(2)
370 IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
371 if line =~ /^Adapter #(\d+)$/
372 controller = controllers[Regexp.last_match(1).to_i]
373 elsif controller && line =~ /^Enclosure Device ID: \d+$/
375 :controller => controller[:id]
377 elsif disk && line =~ /^WWN:\s+(\S+)$/
378 unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
379 disk[:id] = devices[:disks].count
380 disk[:wwn] = Regexp.last_match(1)
382 devices[:disks] << disk
384 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
385 case Regexp.last_match(1)
386 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
387 when "WWN" then disk[:wwn] = Regexp.last_match(2)
388 when "PD Type" then disk[:interface] = Regexp.last_match(2)
389 when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
390 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
395 controllers.each do |controller|
396 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
397 controller[:device] = "/dev/#{File.basename(device)}"
402 def find_mpt_disks(devices)
405 IO.popen(%w(sas2ircu list)).each do |line|
406 next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
407 controllers[Regexp.last_match(1).to_i] = {
408 :id => devices[:controllers].count,
409 :model => Regexp.last_match(2),
410 :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
415 devices[:controllers] << controllers[Regexp.last_match(1).to_i]
418 controllers.each_with_index do |controller, index|
425 IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
426 if line =~ /^IR volume (\d+)$/
428 :id => devices[:arrays].count,
429 :controller => controller[:id],
430 :number => Regexp.last_match(1),
434 devices[:arrays] << array
435 controller[:arrays] << array[:id]
438 elsif line =~ /^Device is a Hard disk$/
440 :id => devices[:disks].count,
441 :controller => controller[:id],
445 devices[:disks] << disk
446 controller[:disks] << disk[:id]
449 elsif disk && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
450 case Regexp.last_match(1)
451 when "Enclosure #" then disk[:location] = Regexp.last_match(2)
452 when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
453 when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
454 when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
455 when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
456 when "Model Number" then disk[:model] = Regexp.last_match(2)
457 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
458 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
459 when "Protocol" then disk[:interface] = Regexp.last_match(2)
461 elsif array && line =~ /^ PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
462 array[:disks] << Regexp.last_match(1)
463 elsif array && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
464 case Regexp.last_match(1)
465 when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
466 when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
467 when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
469 elsif line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
470 case Regexp.last_match(1)
471 when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
472 when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
477 arrays.each do |array|
478 array[:disks].map! do |location|
479 disk = disks.find { |disk| disk[:location] == location }
481 disk[:arrays] << array[:id]
488 collect_data(:default) do
491 hardware[:pci] = pci_devices
492 hardware[:network] = network_devices
493 hardware[:memory] = memory_devices
494 hardware[:disk] = disk_devices