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_disk_size(size)
15 if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
16 format "%dTB", Regexp.last_match(1).to_f * 2**40 / 1_000_000_000_000
17 elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
18 format "%dGB", Regexp.last_match(1).to_f * 2**30 / 1000000000
19 elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
20 format "%dGB", Regexp.last_match(1).to_f * 2**20 / 1000000000
24 def find_sas_device(address)
25 file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
26 read_sysctl_file(file) == "0x#{address}"
30 dir = File.dirname(file)
31 device = Dir.glob("#{dir}/block/*").first ||
32 Dir.glob("#{dir}/scsi_generic/*").first
34 "/dev/#{File.basename(device)}"
41 IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
42 if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
44 :slot => Regexp.last_match(1),
45 :domain => Regexp.last_match(2),
46 :bus => Regexp.last_match(3),
47 :device => Regexp.last_match(4),
48 :function => Regexp.last_match(5)
50 elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
51 case Regexp.last_match(1)
52 when "Class" then device[:class_name] = Regexp.last_match(2)
53 when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
54 when "Device" then device[:device_name] = Regexp.last_match(2)
55 when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
56 when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
57 when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
58 when "Rev" then device[:revision] = Regexp.last_match(2)
59 when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
60 when "Driver" then device[:driver] = Regexp.last_match(2)
61 when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
63 elsif device && line =~ /^\s*$/
64 devices[device[:slot]] = device
71 Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
72 name = File.basename(device)
75 :device => read_sysctl_link("#{device}/device"),
76 :duplex => read_sysctl_file("#{device}/duplex"),
77 :speed => read_sysctl_file("#{device}/speed")
78 }.delete_if { |_, v| v.nil? }
85 IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
86 if line =~ /^Memory Device\s*$/
88 elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
89 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
90 elsif device && line =~ /^\s*$/
100 disk[:controllers] = []
104 find_direct_disks(disk)
106 find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
107 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
108 find_mpt_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
110 # areca (/opt/areca/x86_64/cli64)
117 def find_direct_disks(devices)
118 Dir.glob("/sys/class/scsi_host/host*") do |host|
119 driver = read_sysctl_file("#{host}/proc_name")
121 if driver == "ahci" || driver == "mptsas"
122 bus = host.sub("/sys/class/scsi_host/host", "")
124 Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
125 next unless File.exist?("#{device}/scsi_disk")
127 block = Dir.glob("#{device}/block/*").first
128 vendor = read_sysctl_file("#{device}/vendor")
129 model = read_sysctl_file("#{device}/model")
130 size = read_sysctl_file("#{block}/size").to_i * 512
132 if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
133 vendor = Regexp.last_match(1)
134 model = Regexp.last_match(2)
137 if size > 1_000_000_000_000
138 size = format "%d TB", size / 1_000_000_000_000
139 elsif size > 1000000000
140 size = format "%d GB", size / 1000000000
144 :id => devices[:disks].count,
145 :device => "/dev/#{File.basename(block)}",
148 :firmware_version => read_sysctl_file("#{device}/rev"),
157 def find_md_arrays(devices)
158 File.new("/proc/mdstat", "r").each do |line|
159 next unless line =~ /^(md\d+) : active raid(\d+)((?: [a-z]+\d+\[\d+\](?:\([A-Z]\))*)+)$/
162 :id => devices[:arrays].count,
163 :device => "/dev/#{Regexp.last_match(1)}",
164 :raid_level => Regexp.last_match(2),
168 Regexp.last_match(3).scan(/ ([a-z]+)\d+\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
169 if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
170 disk[:arrays] << array[:id]
171 array[:disks] << disk[:id]
175 devices[:arrays] << array
179 def find_hp_disks(devices)
187 IO.popen(%w(hpssacli controller all show config detail)).each do |line|
188 if line =~ /^Smart Array (\S+) /
190 :id => devices[:controllers].count,
191 :model => Regexp.last_match(1),
196 devices[:controllers] << controller
198 controllers << controller
202 elsif controller && line =~ /^ (\S.*):\s+(.*)$/
203 case Regexp.last_match(1)
204 when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
205 when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
206 when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
207 when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
209 elsif controller && line =~ /^ Logical Drive: (\d+)$/
211 :id => devices[:arrays].count,
212 :controller => controller[:id],
213 :number => Regexp.last_match(1),
217 devices[:arrays] << array
218 controller[:arrays] << array[:id]
221 elsif controller && line =~ /^ physicaldrive (\S+) /
222 disks << Regexp.last_match(1)
223 elsif array && line =~ /^ physicaldrive (\S+)$/
225 :id => devices[:disks].count,
226 :controller => controller[:id],
227 :arrays => [array[:id]],
228 :location => Regexp.last_match(1),
229 :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
232 devices[:disks] << disk
233 controller[:disks] << disk[:id]
234 array[:disks] << disk[:id]
235 elsif disk && line =~ /^ (\S[^:]+):\s+(.*)$/
236 case Regexp.last_match(1)
237 when "Interface Type" then disk[:interface] = Regexp.last_match(2)
238 when "Size" then disk[:size] = Regexp.last_match(2)
239 when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
240 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
241 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
242 when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
244 elsif array && line =~ /^ (\S[^:]+):\s+(.*)$/
245 case Regexp.last_match(1)
246 when "Size" then array[:size] = Regexp.last_match(2)
247 when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
248 when "Disk Name" then array[:device] = Regexp.last_match(2).strip
249 when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
250 when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
255 controllers.each do |controller|
256 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
257 controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
258 elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
259 controller[:device] = "/dev/#{File.basename(device)}"
264 def find_megaraid_disks(devices)
272 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
273 if line =~ /^PCI information for Controller (\d+)$/
275 :id => devices[:controllers].count,
280 devices[:controllers] << controller
282 controllers << controller
283 elsif line =~ /^Bus Number\s+:\s+(\d+)$/
284 controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
285 elsif line =~ /^Device Number\s+:\s+(\d+)$/
286 controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
287 elsif line =~ /^Function Number\s+:\s+(\d+)$/
288 controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
292 IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
293 if line =~ /^Adapter #(\d+)$/
294 controller = controllers[Regexp.last_match(1).to_i]
295 elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
296 case Regexp.last_match(1)
297 when "Product Name" then controller[:model] = Regexp.last_match(2)
298 when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
299 when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
304 IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
305 if line =~ /^Adapter #(\d+)$/
306 controller = controllers[Regexp.last_match(1).to_i]
307 elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
309 :id => devices[:arrays].count,
310 :controller => controller[:id],
311 :number => Regexp.last_match(1),
315 devices[:arrays] << array
316 controller[:arrays] << array[:id]
321 elsif array && line =~ /^PD: (\d+) Information$/
323 :id => devices[:disks].count,
324 :controller => controller[:id],
325 :arrays => [array[:id]]
328 devices[:disks] << disk
329 controller[:disks] << disk[:id]
330 array[:disks] << disk[:id]
331 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
332 case Regexp.last_match(1)
333 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
334 when "WWN" then disk[:wwn] = Regexp.last_match(2)
335 when "PD Type" then disk[:interface] = Regexp.last_match(2)
336 when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
337 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
339 elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
340 case Regexp.last_match(1)
341 when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
342 when "Size" then array[:size] = Regexp.last_match(2)
347 IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
348 if line =~ /^Adapter #(\d+)$/
349 controller = controllers[Regexp.last_match(1).to_i]
350 elsif controller && line =~ /^Enclosure Device ID: \d+$/
352 :controller => controller[:id]
354 elsif disk && line =~ /^WWN:\s+(\S+)$/
355 unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
356 disk[:id] = devices[:disks].count
357 disk[:wwn] = Regexp.last_match(1)
359 devices[:disks] << disk
361 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
362 case Regexp.last_match(1)
363 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
364 when "WWN" then disk[:wwn] = Regexp.last_match(2)
365 when "PD Type" then disk[:interface] = Regexp.last_match(2)
366 when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
367 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
372 controllers.each do |controller|
373 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
374 controller[:device] = "/dev/#{File.basename(device)}"
379 def find_mpt_disks(devices)
382 IO.popen(%w(sas2ircu list)).each do |line|
383 if line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
384 controllers[$1.to_i] = {
385 :id => devices[:controllers].count,
387 :pci_slot => $3.sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
392 devices[:controllers] << controllers[$1.to_i]
396 controllers.each_with_index do |controller, index|
403 IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
404 if line =~ /^IR volume (\d+)$/
406 :id => devices[:arrays].count,
407 :controller => controller[:id],
408 :number => Regexp.last_match(1),
412 devices[:arrays] << array
413 controller[:arrays] << array[:id]
418 elsif array && line =~ /^Device is a Hard disk$/
420 :id => devices[:disks].count,
421 :controller => controller[:id],
425 devices[:disks] << disk
426 controller[:disks] << disk[:id]
429 elsif disk && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
430 case Regexp.last_match(1)
431 when "Enclosure #" then disk[:location] = Regexp.last_match(2)
432 when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
433 when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
434 when "Size (in MB)/(in sectors)" then disk[:size] = parse_disk_size("#{Regexp.last_match(2).split("/").first} MB")
435 when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
436 when "Model Number" then disk[:model] = Regexp.last_match(2)
437 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
438 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
439 when "Protocol" then disk[:interface] = Regexp.last_match(2)
441 elsif array && line =~ /^ PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
443 elsif array && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
444 case Regexp.last_match(1)
445 when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
446 when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
447 when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
449 elsif line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
450 case Regexp.last_match(1)
451 when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
452 when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
457 arrays.each do |array|
458 array[:disks].map! do |location|
459 disk = disks.find { |disk| disk[:location] == location }
461 disk[:arrays] << array[:id]
470 collect_data(:default) do
473 hardware[:pci] = pci_devices
474 hardware[:network] = network_devices
475 hardware[:memory] = memory_devices
476 hardware[:disk] = disk_devices