3 Ohai.plugin(:Hardware) do
6 def read_sysctl_link(file)
7 File.basename(File.readlink(file))
8 rescue Errno::ENOENT, Errno::ENOTDIR
11 def read_sysctl_file(file)
13 rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EINVAL
16 def parse_memory_size(size)
17 if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
18 Regexp.last_match(1).to_f * 2**30
19 elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
20 Regexp.last_match(1).to_f * 2**20
21 elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
22 Regexp.last_match(1).to_f * 2**10
26 def format_disk_size(kb)
30 kblog10 = Math.log10(kb).floor
32 kb = kb.to_f * 2 / 10**kblog10
33 kb = kb.round.to_f / 2
36 format "%gTB", kb * 10**(kblog10 - 9)
38 format "%dGB", kb * 10**(kblog10 - 6)
40 format "%dMB", kb * 10**(kblog10 - 3)
45 def memory_to_disk_size(size)
46 format_disk_size(parse_memory_size(size))
49 def find_sas_device(address)
50 file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
51 read_sysctl_file(file) == "0x#{address}"
55 dir = File.dirname(file)
56 device = Dir.glob("#{dir}/block/*").first ||
57 Dir.glob("#{dir}/scsi_generic/*").first
59 "/dev/#{File.basename(device)}"
67 IO.popen(["lspci", "-Dkvmm"]).each do |line|
68 if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
70 :slot => Regexp.last_match(1),
71 :domain => Regexp.last_match(2),
72 :bus => Regexp.last_match(3),
73 :device => Regexp.last_match(4),
74 :function => Regexp.last_match(5)
76 elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
77 case Regexp.last_match(1)
78 when "Class" then device[:class_name] = Regexp.last_match(2)
79 when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
80 when "Device" then device[:device_name] = Regexp.last_match(2)
81 when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
82 when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
83 when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
84 when "Rev" then device[:revision] = Regexp.last_match(2)
85 when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
86 when "Driver" then device[:driver] = Regexp.last_match(2)
87 when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
89 elsif device && line =~ /^\s*$/
90 devices[device[:slot]] = device
95 IO.popen(["lspci", "-Dkvmmn"]).each do |line|
96 if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
97 device = devices[Regexp.last_match(1)]
98 elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
99 case Regexp.last_match(1)
100 when "Class" then device[:class_id] = Regexp.last_match(2)
101 when "Vendor" then device[:vendor_id] = Regexp.last_match(2)
102 when "Device" then device[:device_id] = Regexp.last_match(2)
103 when "SVendor" then device[:subsystem_vendor_id] = Regexp.last_match(2)
104 when "SDevice" then device[:subsystem_device_id] = Regexp.last_match(2)
106 elsif device && line =~ /^\s*$/
115 Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
116 name = File.basename(device)
119 :device => read_sysctl_link("#{device}/device"),
120 :duplex => read_sysctl_file("#{device}/duplex"),
121 :speed => read_sysctl_file("#{device}/speed")
122 }.delete_if { |_, v| v.nil? }
129 IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
130 if line =~ /^Memory Device\s*$/
132 elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
133 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
134 elsif device && line =~ /^\s*$/
144 disk[:controllers] = []
148 find_direct_disks(disk)
149 find_nvme_disks(disk)
151 find_hp_disks(disk) if File.exist?("/usr/sbin/ssacli")
152 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
153 find_mpt1_disks(disk) if File.exist?("/usr/sbin/lsiutil")
154 find_mpt2_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
155 find_adaptec_disks(disk) if File.exist?("/usr/sbin/arcconf")
156 find_areca_disks(disk) if File.exist?("/opt/areca/x86_64/cli64")
160 disk[:disks].each do |disk|
161 if disk[:vendor] =~ /^(BTWA|CVPR|PHDV)/ && disk[:model] == "INTEL"
162 disk[:model] = disk[:serial_number]
163 disk[:serial_number] = disk[:vendor]
164 disk[:vendor] = "INTEL"
167 if disk[:vendor].nil? && disk[:model] =~ /^ATA\s+(.*)$/
168 disk[:vendor] = "ATA"
169 disk[:model] = Regexp.last_match(1)
172 if disk[:vendor].nil? || disk[:vendor] == "ATA"
173 if disk[:model] =~ /^(\S+)\s+(.*)$/
174 disk[:vendor] = Regexp.last_match(1)
175 disk[:model] = Regexp.last_match(2)
176 elsif disk[:model] =~ /^ST/
177 disk[:vendor] = "SEAGATE"
178 elsif disk[:model] =~ /^C300-(.*)$/
179 disk[:vendor] = "CRUCIAL"
180 disk[:model] = Regexp.last_match(1)
184 disk[:model].sub!(/-.*$/, "") if disk[:model]
190 def find_direct_disks(devices)
191 Dir.glob("/sys/class/scsi_host/host*") do |host|
192 driver = read_sysctl_file("#{host}/proc_name")
194 if %w(ahci mptsas sata_mv sata_nv).include?(driver)
195 bus = host.sub("/sys/class/scsi_host/host", "")
197 Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
198 next unless File.exist?("#{device}/scsi_disk")
200 block = Dir.glob("#{device}/block/*").first
201 size = read_sysctl_file("#{block}/size").to_f / 2
204 :id => devices[:disks].count,
205 :device => "/dev/#{File.basename(block)}",
206 :vendor => read_sysctl_file("#{device}/vendor"),
207 :model => read_sysctl_file("#{device}/model"),
208 :firmware_version => read_sysctl_file("#{device}/rev"),
209 :size => format_disk_size(size),
217 def find_nvme_disks(devices)
218 Dir.glob("/sys/class/nvme/nvme*") do |device|
220 :id => devices[:controllers].count,
221 :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
226 devices[:controllers] << controller
228 IO.popen(["lspci", "-Dkvmm", "-s", controller[:pci_slot]]).each do |line|
229 if line =~ /^SVendor:\s+(\S.*\S)\s*$/
230 controller[:vendor] = Regexp.last_match(1)
231 elsif line =~ /^SDevice:\s+(\S.*\S)\s*$/
232 controller[:model] = Regexp.last_match(1)
236 Dir.glob("#{device}/nvme*").each do |block|
237 size = read_sysctl_file("#{block}/size").to_f / 2
240 :id => devices[:disks].count,
241 :controller => controller[:id],
242 :device => "/dev/#{File.basename(block)}",
243 :vendor => controller[:vendor],
244 :model => controller[:model],
245 :size => format_disk_size(size),
249 devices[:disks] << disk
250 controller[:disks] << disk[:id]
255 def find_md_arrays(devices)
258 File.new("/proc/mdstat", "r").each do |line|
259 if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]\d*|nvme\d+n\d+(?:p\d+)?)\[\d+\](?:\([A-Z]\))*)+)$/
261 :id => devices[:arrays].count,
262 :device => "/dev/#{Regexp.last_match(1)}",
263 :status => "optimal",
264 :raid_level => Regexp.last_match(2),
268 Regexp.last_match(3).split(" ").each do |member|
269 if member =~ /^(sd[a-z]+|nvme\d+n\d+).*/
270 device = Regexp.last_match(1)
272 if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
274 disk[:status] = "failed"
275 elsif member =~ /\(S\)/
276 disk[:status] = "hotspare"
278 disk[:status] = "online"
281 disk[:arrays] << array[:id]
282 array[:disks] << disk[:id]
287 devices[:arrays] << array
288 elsif array && line =~ /^\s+(\d+) blocks.*(?:\[([U_]+)\])?/
289 array[:size] = format_disk_size(Regexp.last_match(1).to_i)
290 array[:status] = "degraded" if Regexp.last_match(2) =~ /_/
291 elsif array && line =~ /^\s+\[.*\]\s+(\S+)\s+=/
292 case Regexp.last_match(1)
293 when "recovery" then array[:status] = "rebuilding"
294 when "resync" then array[:status] = "rebuilding"
295 when "checking" then array[:status] = "checking"
301 def find_hp_disks(devices)
309 IO.popen(%w(ssacli controller all show config detail)).each do |line|
310 next unless line.valid_encoding?
312 if line =~ /^Smart (?:Array|HBA) (\S+) /
314 :id => devices[:controllers].count,
316 :model => Regexp.last_match(1),
321 devices[:controllers] << controller
323 controllers << controller
327 elsif controller && line =~ /^ (\S.*):\s+(.*)$/
328 case Regexp.last_match(1)
329 when "Slot" then controller[:slot] = Regexp.last_match(2)
330 when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
331 when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
332 when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
333 when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
335 elsif controller && line =~ /^ Logical Drive: (\d+)$/
337 :id => devices[:arrays].count,
338 :controller => controller[:id],
339 :number => Regexp.last_match(1),
343 devices[:arrays] << array
344 controller[:arrays] << array[:id]
347 elsif array && line =~ /^ physicaldrive (\S+)$/
349 :id => devices[:disks].count,
350 :controller => controller[:id],
351 :arrays => [array[:id]],
352 :location => Regexp.last_match(1)
355 devices[:disks] << disk
356 controller[:disks] << disk[:id]
357 array[:disks] << disk[:id]
358 elsif disk && line =~ /^ (\S[^:]+):\s+(.*\S)\s*$/
359 case Regexp.last_match(1)
360 when "Status" then disk[:status] = Regexp.last_match(2)
361 when "Drive Type" then disk[:drive_type] = Regexp.last_match(2)
362 when "Interface Type" then disk[:interface] = Regexp.last_match(2)
363 when "Size" then disk[:size] = Regexp.last_match(2)
364 when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
365 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
366 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
367 when "Model" then disk[:model] = Regexp.last_match(2)
369 elsif array && line =~ /^ Status:\s+(.*\S)\s*$/
370 case Regexp.last_match(1)
371 when "OK" then array[:status] = "optimal"
372 when "Interim Recovery Mode" then array[:status] = "degraded"
373 else array[:status] = "unknown"
375 elsif array && line =~ /^ (\S[^:]+):\s+(.*\S)\s*$/
376 case Regexp.last_match(1)
377 when "Size" then array[:size] = Regexp.last_match(2)
378 when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
379 when "Status" then array[:status] = Regexp.last_match(2)
380 when "Disk Name" then array[:device] = Regexp.last_match(2).strip
381 when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
382 when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
387 controllers.each do |controller|
388 slot = controller[:slot]
390 IO.popen(%W(ssacli controller slot=#{slot} pd all show status)).each do |line|
391 if line =~ /^ physicaldrive (\S+) /
392 disks << Regexp.last_match(1)
396 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
397 controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
398 elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:3:0/*:3:0:0/scsi_generic/sg*").first
399 controller[:device] = "/dev/#{File.basename(device)}"
400 elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:1:0/*:1:0:*/scsi_generic/sg*").first
401 controller[:device] = "/dev/#{File.basename(device)}"
405 devices[:disks].each do |disk|
406 disk[:smart_device] = "cciss,#{disks.find_index(disk[:location])}"
408 if disk[:status] == "Failed"
409 disk[:status] = "failed"
410 elsif disk[:status] == "Predictive Failure"
411 disk[:status] = "failed"
412 elsif disk[:status] == "OK" && disk[:drive_type] == "Data Drive"
413 disk[:status] = "online"
414 elsif disk[:status] == "OK" && disk[:drive_type] == "Spare Drive"
415 disk[:status] = "hotspare"
416 elsif disk[:drive_type] == "Unassigned Drive"
417 disk[:status] = "unconfigured"
419 disk[:status] = "unknown"
422 disk.delete(:drive_type)
426 def find_megaraid_disks(devices)
434 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
435 if line =~ /^PCI information for Controller (\d+)$/
437 :id => devices[:controllers].count,
443 devices[:controllers] << controller
445 controllers << controller
446 elsif line =~ /^Bus Number\s+:\s+([0-9a-f]+)$/i
447 controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
448 elsif line =~ /^Device Number\s+:\s+([0-9a-f]+)$/i
449 controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
450 elsif line =~ /^Function Number\s+:\s+([0-9a-f]+)$/i
451 controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
455 IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
456 if line =~ /^Adapter #(\d+)$/
457 controller = controllers[Regexp.last_match(1).to_i]
458 elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
459 case Regexp.last_match(1)
460 when "Product Name" then controller[:model] = Regexp.last_match(2)
461 when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
462 when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
467 IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
468 if line =~ /^Adapter #(\d+)$/
469 controller = controllers[Regexp.last_match(1).to_i]
470 elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
471 pci_slot = controller[:pci_slot]
472 target = Regexp.last_match(2)
473 device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
476 :id => devices[:arrays].count,
477 :controller => controller[:id],
478 :number => Regexp.last_match(1),
479 :device => "/dev/#{File.basename(device)}",
483 devices[:arrays] << array
484 controller[:arrays] << array[:id]
489 elsif array && line =~ /^PD: (\d+) Information$/
491 :id => devices[:disks].count,
492 :controller => controller[:id],
493 :arrays => [array[:id]]
496 devices[:disks] << disk
497 controller[:disks] << disk[:id]
498 array[:disks] << disk[:id]
499 elsif disk && line =~ /^Firmware state:\s+(\S.*)$/
500 status, state = Regexp.last_match(1).split(/,\s*/)
502 when "Unconfigured(good)" then disk[:status] = "unconfigured"
503 when "Unconfigured(bad)" then disk[:status] = "unconfigured"
504 when "Hotspare" then disk[:status] = "hotspare"
505 when "Offline" then disk[:status] = "offline"
506 when "Online" then disk[:status] = "online"
507 when "Rebuild" then disk[:status] = "rebuilding"
508 when "Failed" then disk[:status] = "failed"
509 when "Copyback" then disk[:status] = "rebuilding"
510 else disk[:status] = "unknown"
513 when "Spun Up" then disk[:state] = "spun_up"
514 when "Spun down" then disk[:state] = "spun_down"
515 else disk[:state] = "unknown"
517 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
518 case Regexp.last_match(1)
519 when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
520 when "WWN" then disk[:wwn] = Regexp.last_match(2)
521 when "PD Type" then disk[:interface] = Regexp.last_match(2)
522 when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
523 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
525 elsif array && line =~ /^State\s*:\s+(.*\S)\s*$/
526 case Regexp.last_match(1)
527 when "Partially Degraded" then array[:status] = "degraded"
528 when "Degraded" then array[:status] = "degraded"
529 when "Optimal" then array[:status] = "optimal"
530 when "Consistency Check" then array[:status] = "checking"
531 when "Background Initialization" then array[:status] = "initialising"
532 when "Initialization" then array[:status] = "initialising"
533 when "Reconstruction" then array[:status] = "rebuilding"
534 else array[:status] = "unknown"
536 elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
537 case Regexp.last_match(1)
538 when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
539 when "Size" then array[:size] = Regexp.last_match(2)
544 IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
545 if line =~ /^Adapter #(\d+)$/
546 controller = controllers[Regexp.last_match(1).to_i]
547 elsif controller && line =~ /^Enclosure Device ID: \d+$/
549 :controller => controller[:id]
551 elsif disk && line =~ /^WWN:\s+(\S+)$/
552 unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
553 disk[:id] = devices[:disks].count
554 disk[:wwn] = Regexp.last_match(1)
556 devices[:disks] << disk
558 elsif disk && line =~ /^Firmware state:\s+(\S.*)$/
559 status, state = Regexp.last_match(1).split(/,\s*/)
561 when "Unconfigured(good)" then disk[:status] = "unconfigured"
562 when "Unconfigured(bad)" then disk[:status] = "unconfigured"
563 when "Hotspare" then disk[:status] = "hotspare"
564 when "Offline" then disk[:status] = "offline"
565 when "Online" then disk[:status] = "online"
566 when "Rebuild" then disk[:status] = "rebuilding"
567 when "Failed" then disk[:status] = "failed"
568 when "Copyback" then disk[:status] = "rebuilding"
569 else disk[:status] = "unknown"
572 when "Spun Up" then disk[:state] = "spun_up"
573 when "Spun down" then disk[:state] = "spun_down"
574 else disk[:state] = "unknown"
576 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
577 case Regexp.last_match(1)
578 when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
579 when "PD Type" then disk[:interface] = Regexp.last_match(2)
580 when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
581 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
586 controllers.each do |controller|
587 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
588 controller[:device] = "/dev/#{File.basename(device)}"
593 def find_mpt1_disks(devices)
599 IO.popen(%w(lsiutil -s)).each do |line|
600 if line =~ /^\/proc\/mpt\/ioc(\d+)\s+LSI Logic\s+(\S+)\s+/
602 :id => devices[:controllers].count,
604 :model => Regexp.last_match(1),
609 controllers << controller
610 devices[:controllers] << controller
611 elsif line =~ /^\s+(\d+)\s+(\d+)\s+PhysDisk (\d+)\s+(\S+)\s+(\S+)\s+\d+\s+(\S+)\s+/
612 disks[Regexp.last_match(3).to_i] = {
613 :id => devices[:disks].count,
614 :controller => controller[:id],
615 :vendor => Regexp.last_match(4),
616 :model => Regexp.last_match(5),
617 :sas_address => Regexp.last_match(6),
621 controller[:disks] << devices[:disks].count
622 devices[:disks] << disks[Regexp.last_match(3).to_i]
626 controllers.each_with_index do |controller, index|
630 IO.popen(["lsiutil", "-p", port.to_s, "-a", "69,0"]).each do |line|
631 if line =~ /^ (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+RAID/
632 seg = Regexp.last_match(1).to_i
633 bus = Regexp.last_match(2).to_i
634 dev = Regexp.last_match(3).to_i
635 fun = Regexp.last_match(4).to_i
637 controller[:pci_slot] = sprintf("%04x:%02x:%02x.%01x", seg, bus, dev, fun)
641 IO.popen(["lsiutil", "-p", port.to_s, "-a", "21,1,0,0"]).each do |line|
642 if line =~ /^Volume (\d+) is/
644 :id => devices[:arrays].count,
645 :controller => controller[:id],
646 :number => Regexp.last_match(1),
650 devices[:arrays] << array
651 controller[:arrays] << array[:id]
652 elsif line =~ /^ Member \d+ is PhysDisk (\d+) /
653 array[:disks] << disks[Regexp.last_match(1).to_i][:id]
654 disks[Regexp.last_match(1).to_i][:arrays] << array[:id]
660 slot = controllers[disk[:controller]][:pci_slot]
661 sas_address = "0x#{disk[:sas_address]}"
663 Dir.glob("/sys/bus/pci/devices/#{slot}/host*/port-*:*/end_device-*:*/sas_device/end_device-*:*").each do |sas_device|
664 if read_sysctl_file("#{sas_device}/sas_address") == sas_address
665 if device = Dir.glob("#{sas_device}/device/target*:0:*/*:0:*:0/scsi_generic/sg*").first
666 disk[:device] = "/dev/#{File.basename(device)}"
673 def find_mpt2_disks(devices)
676 IO.popen(%w(sas2ircu list)).each do |line|
677 next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
678 controllers[Regexp.last_match(1).to_i] = {
679 :id => devices[:controllers].count,
681 :model => Regexp.last_match(2),
682 :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
687 devices[:controllers] << controllers[Regexp.last_match(1).to_i]
690 controllers.each_with_index do |controller, index|
697 IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
698 if line =~ /^IR volume (\d+)$/
700 :id => devices[:arrays].count,
701 :controller => controller[:id],
702 :number => Regexp.last_match(1),
706 devices[:arrays] << array
707 controller[:arrays] << array[:id]
710 elsif line =~ /^Device is a Hard disk$/
712 :id => devices[:disks].count,
713 :controller => controller[:id],
717 devices[:disks] << disk
718 controller[:disks] << disk[:id]
721 elsif disk && line =~ /^ State\s+:\s+(.*\S)\s*$/
722 Regexp.last_match(1).split(/,\s*/).each do |state|
724 when "Online (ONL)" then disk[:status] = "online"
725 when "Hot Spare (HSP)" then disk[:status] = "hotspare"
726 when "Ready (RDY)" then disk[:status] = "unconfigured"
727 when "Available (AVL)" then disk[:status] = "unconfigured"
728 when "Failed (FLD)" then disk[:status] = "failed"
729 when "Missing (MIS)" then disk[:status] = "missing"
730 when "Standby (SBY)" then disk[:status] = "unconfigured"
731 when "Out of Sync (OSY)" then disk[:status] = "degraded"
732 when "Degraded (DGD)" then disk[:status] = "degraded"
733 when "Rebuilding (RBLD)" then disk[:status] = "rebuilding"
734 when "Optimal (OPT)" then disk[:status] = "online"
735 else disk[:status] = "unknown"
738 elsif disk && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
739 case Regexp.last_match(1)
740 when "Enclosure #" then disk[:location] = Regexp.last_match(2)
741 when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
742 when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
743 when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
744 when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
745 when "Model Number" then disk[:model] = Regexp.last_match(2)
746 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
747 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
748 when "Protocol" then disk[:interface] = Regexp.last_match(2)
750 elsif array && line =~ /^ PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
751 array[:disks] << Regexp.last_match(1)
752 elsif array && line =~ /^ Status of volume\s+:\s+(.*\S)\s*$/
753 Regexp.last_match(1).split(/,\s*/).each do |state|
755 when "Okay (OKY)" then array[:status] = "optimal"
756 when "Degraded (DGD)" then array[:status] = "degraded"
757 when "Failed (FLD)" then array[:status] = "failed"
758 when "Missing (MIS)" then array[:status] = "missing"
759 when "Initializing (INIT)" then array[:status] = "initialising"
760 when "Online (ONL)" then array[:status] = "optimal"
761 else array[:status] = "unknown"
764 elsif array && line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
765 case Regexp.last_match(1)
766 when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
767 when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
768 when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
770 elsif line =~ /^ (\S.*\S)\s+:\s+(.*\S)\s*$/
771 case Regexp.last_match(1)
772 when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
773 when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
778 arrays.each do |array|
779 array[:disks].map! do |location|
780 disk = disks.find { |disk| disk[:location] == location }
782 disk[:arrays] << array[:id]
789 def find_adaptec_disks(devices)
790 controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers Found: (\d+)$/i).first.first.to_i
792 1.upto(controller_count).each do |controller_number|
794 :id => devices[:controllers].count,
795 :number => controller_number,
801 devices[:controllers] << controller
809 IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
810 if line =~ /^Logical Device Number (\d+)$/i
812 :id => devices[:arrays].count,
813 :controller => controller[:id],
814 :number => Regexp.last_match(1).to_i,
818 devices[:arrays] << array
819 controller[:arrays] << array[:id]
822 elsif line =~ /^ Device #(\d+)$/
824 elsif line =~ /^ Device is a Hard drive$/
826 :id => devices[:disks].count,
827 :controller => controller[:id],
831 devices[:disks] << disk
832 controller[:disks] << disk[:id]
835 elsif disk && line =~ /^ Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
836 disk[:channel_number] = Regexp.last_match(1)
837 disk[:device_number] = Regexp.last_match(2)
838 elsif disk && line =~ /^ State\s*:\s+(\S.*\S)\s*$/
839 case Regexp.last_match(1)
840 when "Online" then disk[:status] = "online"
841 when "Online (JBOD)" then disk[:status] = "online"
842 when "Hot Spare" then disk[:status] = "hotspare"
843 when "Ready" then disk[:status] = "unconfigured"
844 when "Global Hot-Spare" then disk[:status] = "hostspare"
845 when "Dedicated Hot-Spare" then disk[:status] = "hotspare"
846 else disk[:status] = "unknown"
848 elsif disk && line =~ /^ (\S.*\S)\s*:\s+(\S.*\S)\s*$/
849 case Regexp.last_match(1)
850 when "Reported Location" then disk[:location] = Regexp.last_match(2)
851 when "Vendor" then disk[:vendor] = Regexp.last_match(2)
852 when "Model" then disk[:model] = Regexp.last_match(2)
853 when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
854 when "Serial number" then disk[:serial_number] = Regexp.last_match(2)
855 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
856 when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
857 when "World-wide Name" then disk[:wwn] = Regexp.last_match(2)
858 when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
859 when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
861 elsif array && line =~ / Present \(.*((?:Connector|Enclosure):\d+,\s*(?:Device|Slot):\d+)\) /
862 array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(/,\s*/, ", ")
863 elsif array && line =~ /^ Status of Logical Device\s*:\s+(\S.*\S)\s*$/
864 case Regexp.last_match(1)
865 when "Optimal" then array[:status] = "optimal"
866 else array[:status] = "unknown"
868 elsif array && line =~ /^ (\S.*\S)\s*:\s+(\S.*\S)\s*$/
869 case Regexp.last_match(1)
870 when "RAID level" then array[:raid_level] = Regexp.last_match(2)
871 when "RAID Level" then array[:raid_level] = Regexp.last_match(2)
872 when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
874 elsif line =~ /^ (\S.*\S)\s*:\s+(\S.*\S)\s*$/
875 case Regexp.last_match(1)
876 when "Controller Model" then controller[:model] = Regexp.last_match(2)
877 when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
878 when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
879 when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
880 when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
882 elsif line =~ /^ Serial Number\s*:\s+(\S.*\S)\s*$/
883 controller[:serial_number] = Regexp.last_match(1)
887 host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
888 read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
891 arrays.each do |array|
892 array_number = array[:number]
893 device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
895 array[:device] = "/dev/#{File.basename(device)}"
897 array[:disks].map! do |location|
898 disk = disks.find { |disk| disk[:location] == location }
900 controller_number = controller[:number] - 1
901 device_number = disk[:device_number]
903 disk[:device] = "/dev/#{File.basename(device)}"
904 disk[:smart_device] = "aacraid,#{controller_number},0,#{device_number}"
906 disk[:arrays] << array[:id]
913 def find_areca_disks(devices)
915 :id => devices[:controllers].count,
921 devices[:controllers] << controller
923 IO.popen(%w(/opt/areca/x86_64/cli64 sys info)).each do |line|
924 next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
926 case Regexp.last_match(1)
927 when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
928 when "BOOT ROM Version" then controller[:bios_version] = Regexp.last_match(2)
929 when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
930 when "Controller Name" then controller[:model] = Regexp.last_match(2)
934 path = Dir.glob("/sys/bus/pci/devices/*/host*/scsi_host/host*/host_fw_model").find do |file|
935 read_sysctl_file(file) == controller[:model]
938 controller[:pci_slot] = File.basename(File.expand_path("#{path}/../../../.."))
939 controller[:device] = File.basename(Dir.glob(File.expand_path("#{path}/../../../target0:0:16/0:0:16:0/scsi_generic/*")).first)
943 IO.popen(%w(/opt/areca/x86_64/cli64 vsf info)).each do |line|
944 next unless line =~ /^\s+(\d+)\s+/
946 :id => devices[:arrays].count,
947 :number => Regexp.last_match(1),
948 :controller => controller[:id],
952 devices[:arrays] << array
953 controller[:arrays] << array[:id]
958 arrays.each do |array|
959 IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
960 if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
961 pci_slot = controller[:pci_slot]
962 channel = Regexp.last_match(1).to_i
963 id = Regexp.last_match(2).to_i
964 lun = Regexp.last_match(3).to_i
966 device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
968 array[:device] = "/dev/#{File.basename(device)}"
969 elsif line =~ /^Volume State\s+:\s+(.*\S)\s*$/
970 case Regexp.last_match(1)
971 when "Normal" then array[:status] = "optimal"
972 else array[:status] = "unknown"
974 elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
975 case Regexp.last_match(1)
976 when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
977 when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
978 when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
979 when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
987 IO.popen(%w(/opt/areca/x86_64/cli64 disk info)).each do |line|
988 next unless line =~ /^\s+(\d+)\s+.*\s+\d+\.\d+GB\s+(\S.*\S)\s*$/
989 next if Regexp.last_match(2) == "N.A."
992 :id => devices[:disks].count,
993 :number => Regexp.last_match(1),
994 :controller => controller[:id],
998 devices[:disks] << disk
999 controller[:disks] << disk[:id]
1001 if array = arrays.find { |array| array[:raid_set] == Regexp.last_match(2) }
1002 disk[:arrays] << array[:id]
1003 array[:disks] << disk[:id]
1009 disks.each do |disk|
1010 IO.popen(["/opt/areca/x86_64/cli64", "disk", "info", "drv=#{disk[:number]}"]).each do |line|
1011 if line =~ /^IDE Channel\s+:\s+(\d+)\s*$/i
1012 disk[:smart_device] = "areca,#{Regexp.last_match(1)}"
1013 elsif line =~ /^Device Location\s+:\s+Enclosure#(\d+) Slot#?\s*0*(\d+)\s*$/i
1014 disk[:smart_device] = "areca,#{Regexp.last_match(2)}/#{Regexp.last_match(1)}"
1015 elsif line =~ /^Device State\s+:\s+(.*\S)\s*$/
1016 case Regexp.last_match(1)
1017 when "NORMAL" then disk[:status] = "online"
1018 else disk[:status] = "unknown"
1020 elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
1021 case Regexp.last_match(1)
1022 when "Model Name" then disk[:vendor], disk[:model] = Regexp.last_match(2).split
1023 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
1024 when "Disk Capacity" then disk[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
1033 :pvs => find_lvm_pvs,
1034 :vgs => find_lvm_vgs,
1035 :lvs => find_lvm_lvs
1040 IO.popen(["pvdisplay", "-c"]).each_with_object({}) do |line, pvs|
1041 fields = line.strip.split(":")
1045 :pv_size => fields[2],
1046 :pv_status => fields[4],
1047 :pe_size => fields[7],
1048 :pe_total => fields[8],
1049 :pe_free => fields[9],
1050 :pe_allocated => fields[10],
1051 :pv_uuid => fields[11]
1057 IO.popen(["vgdisplay", "-c"]).each_with_object({}) do |line, vgs|
1058 fields = line.strip.split(":")
1061 :vg_access => fields[1],
1062 :vg_status => fields[2],
1063 :lv_maximum => fields[4],
1064 :lv_count => fields[5],
1065 :lv_open => fields[6],
1066 :pv_maximum => fields[8],
1067 :pv_current => fields[9],
1068 :pv_actual => fields[10],
1069 :vg_size => fields[11],
1070 :pe_size => fields[12],
1071 :pe_total => fields[13],
1072 :pe_allocated => fields[14],
1073 :pe_free => fields[15],
1074 :vg_uuid => fields[16]
1080 IO.popen(["lvdisplay", "-c"]).each_with_object({}) do |line, lvs|
1081 fields = line.strip.split(":")
1085 :lv_access => fields[2],
1086 :lv_status => fields[3],
1087 :lv_open => fields[5],
1088 :lv_size => fields[6],
1089 :le_count => fields[7],
1090 :lv_minor => fields[11],
1091 :lv_major => fields[12]
1099 IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
1100 if line =~ /^System Power Supply\s*$/
1102 elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
1103 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
1104 elsif device && line =~ /^\s*$/
1114 IO.popen(["ipmitool", "mc", "info"]).each_with_object([]) do |line, devices|
1115 if line =~ /(Manufacturer [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1116 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1117 elsif line =~ /(Product [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1118 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1119 elsif line =~ /([A-Z ]+[A-Z])\s*:\s+(.*\S)\s*$/i
1120 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1124 IO.popen(["ipmitool", "mc", "guid"]).each_with_object([]) do |line, devices|
1125 if line =~ /^System GUID\s*:\s+(\S+)\s*$/
1126 device[:system_guid] = Regexp.last_match(1)
1133 collect_data(:default) do
1136 hardware[:pci] = pci_devices
1137 hardware[:network] = network_devices
1138 hardware[:memory] = memory_devices
1139 hardware[:disk] = disk_devices
1140 hardware[:lvm] = lvm_devices
1141 hardware[:psu] = psu_devices
1142 hardware[:mc] = mc_device