]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Fix collection of PSU details from dmidecode
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
1 require "pathname"
2
3 Ohai.plugin(:Hardware) do
4   provides "hardware"
5
6   def read_sysctl_link(file)
7     File.basename(File.readlink(file))
8   rescue Errno::ENOENT
9   end
10
11   def read_sysctl_file(file)
12     IO.read(file).strip
13   rescue Errno::ENOENT, Errno::EINVAL
14   end
15
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
23     end
24   end
25
26   def format_disk_size(kb)
27     if kb == 0
28       ""
29     else
30       kblog10 = Math.log10(kb).floor
31
32       kb = kb.to_f * 2 / 10**kblog10
33       kb = kb.round.to_f / 2
34
35       if kblog10 >= 9
36         format "%gTB", kb * 10**(kblog10 - 9)
37       elsif kblog10 >= 6
38         format "%dGB", kb * 10**(kblog10 - 6)
39       else
40         format "%dMB", kb * 10**(kblog10 - 3)
41       end
42     end
43   end
44
45   def memory_to_disk_size(size)
46     format_disk_size(parse_memory_size(size))
47   end
48
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}"
52     end
53
54     if file
55       dir = File.dirname(file)
56       device = Dir.glob("#{dir}/block/*").first ||
57                Dir.glob("#{dir}/scsi_generic/*").first
58
59       "/dev/#{File.basename(device)}"
60     end
61   end
62
63   def pci_devices
64     devices = {}
65     device = nil
66
67     IO.popen(["lspci", "-Dkvmm"]).each do |line|
68       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
69         device = {
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)
75         }
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)
88         end
89       elsif device && line =~ /^\s*$/
90         devices[device[:slot]] = device
91         device = nil
92       end
93     end
94
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)
105         end
106       elsif device && line =~ /^\s*$/
107         device = nil
108       end
109     end
110
111     devices
112   end
113
114   def network_devices
115     Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
116       name = File.basename(device)
117
118       devices[name] = {
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? }
123     end
124   end
125
126   def memory_devices
127     device = nil
128
129     IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
130       if line =~ /^Memory Device\s*$/
131         device = {}
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*$/
135         devices << device
136         device = nil
137       end
138     end
139   end
140
141   def disk_devices
142     disk = Mash.new
143
144     disk[:controllers] = []
145     disk[:arrays] = []
146     disk[:disks] = []
147
148     find_direct_disks(disk)
149     find_nvme_disks(disk)
150
151     find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
152     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
153     find_mpt_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
154     find_adaptec_disks(disk) if File.exist?("/usr/sbin/arcconf")
155     find_areca_disks(disk) if File.exist?("/opt/areca/x86_64/cli64")
156
157     find_md_arrays(disk)
158
159     disk[:disks].each do |disk|
160       if disk[:vendor] =~ /^CVPR/ && disk[:model] == "INTEL"
161         disk[:model] = disk[:serial_number]
162         disk[:serial_number] = disk[:vendor]
163         disk[:vendor] = "INTEL"
164       end
165
166       if disk[:vendor].nil? && disk[:model] =~ /^ATA\s+(.*)$/
167         disk[:vendor] = "ATA"
168         disk[:model] = Regexp.last_match(1)
169       end
170
171       if disk[:vendor].nil? || disk[:vendor] == "ATA"
172         if disk[:model] =~ /^(\S+)\s+(.*)$/
173           disk[:vendor] = Regexp.last_match(1)
174           disk[:model] = Regexp.last_match(2)
175         elsif disk[:model] =~ /^ST/
176           disk[:vendor] = "SEAGATE"
177         elsif disk[:model] =~ /^C300-(.*)$/
178           disk[:vendor] = "CRUCIAL"
179           disk[:model] = Regexp.last_match(1)
180         end
181       end
182
183       disk[:model].sub!(/-.*$/, "")
184     end
185
186     disk
187   end
188
189   def find_direct_disks(devices)
190     Dir.glob("/sys/class/scsi_host/host*") do |host|
191       driver = read_sysctl_file("#{host}/proc_name")
192
193       if %w(ahci mptsas sata_mv sata_nv).include?(driver)
194         bus = host.sub("/sys/class/scsi_host/host", "")
195
196         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
197           next unless File.exist?("#{device}/scsi_disk")
198
199           block = Dir.glob("#{device}/block/*").first
200           size = read_sysctl_file("#{block}/size").to_f / 2
201
202           devices[:disks] << {
203             :id => devices[:disks].count,
204             :device => "/dev/#{File.basename(block)}",
205             :vendor => read_sysctl_file("#{device}/vendor"),
206             :model => read_sysctl_file("#{device}/model"),
207             :firmware_version => read_sysctl_file("#{device}/rev"),
208             :size => format_disk_size(size),
209             :arrays => []
210           }
211         end
212       end
213     end
214   end
215
216   def find_nvme_disks(devices)
217     Dir.glob("/sys/class/misc/nvme*") do |device|
218       controller = {
219         :id => devices[:controllers].count,
220         :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
221         :arrays => [],
222         :disks => []
223       }
224
225       devices[:controllers] << controller
226
227       IO.popen(["lspci", "-Dkvmm", "-s", controller[:pci_slot]]).each do |line|
228         if line =~ /^SVendor:\s+(\S.*\S)\s*$/
229           controller[:vendor] = Regexp.last_match(1)
230         elsif line =~ /^SDevice:\s+(\S.*\S)\s*$/
231           controller[:model] = Regexp.last_match(1)
232         end
233       end
234
235       Dir.glob("#{device}/device/block/*").each do |block|
236         size = read_sysctl_file("#{block}/size").to_f / 2
237
238         disk = {
239           :id => devices[:disks].count,
240           :controller => controller[:id],
241           :device => "/dev/#{File.basename(block)}",
242           :vendor => controller[:vendor],
243           :model => controller[:model],
244           :size => format_disk_size(size),
245           :arrays => []
246         }
247
248         devices[:disks] << disk
249         controller[:disks] << disk[:id]
250       end
251     end
252   end
253
254   def find_md_arrays(devices)
255     array = nil
256
257     File.new("/proc/mdstat", "r").each do |line|
258       if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*)+)$/
259         array = {
260           :id => devices[:arrays].count,
261           :device => "/dev/#{Regexp.last_match(1)}",
262           :raid_level => Regexp.last_match(2),
263           :disks => []
264         }
265
266         Regexp.last_match(3).scan(/ (sd[a-z]+|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
267           if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
268             disk[:arrays] << array[:id]
269             array[:disks] << disk[:id]
270           end
271         end
272
273         devices[:arrays] << array
274       elsif array && line =~ /^\s+(\d+) blocks/
275         array[:size] = format_disk_size(Regexp.last_match(1).to_i)
276       end
277     end
278   end
279
280   def find_hp_disks(devices)
281     controllers = []
282     disks = []
283
284     controller = nil
285     array = nil
286     disk = nil
287
288     IO.popen(%w(hpssacli controller all show config detail)).each do |line|
289       if line =~ /^Smart Array (\S+) /
290         controller = {
291           :id => devices[:controllers].count,
292           :model => Regexp.last_match(1),
293           :arrays => [],
294           :disks => []
295         }
296
297         devices[:controllers] << controller
298
299         controllers << controller
300
301         array = nil
302         disk = nil
303       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
304         case Regexp.last_match(1)
305         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
306         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
307         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
308         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
309         end
310       elsif controller && line =~ /^      Logical Drive: (\d+)$/
311         array = {
312           :id => devices[:arrays].count,
313           :controller => controller[:id],
314           :number => Regexp.last_match(1),
315           :disks => []
316         }
317
318         devices[:arrays] << array
319         controller[:arrays] << array[:id]
320
321         disk = nil
322       elsif controller && line =~ /^      physicaldrive (\S+) /
323         disks << Regexp.last_match(1)
324       elsif array && line =~ /^      physicaldrive (\S+)$/
325         disk = {
326           :id => devices[:disks].count,
327           :controller => controller[:id],
328           :arrays => [array[:id]],
329           :location => Regexp.last_match(1),
330           :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
331         }
332
333         devices[:disks] << disk
334         controller[:disks] << disk[:id]
335         array[:disks] << disk[:id]
336       elsif disk && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
337         case Regexp.last_match(1)
338         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
339         when "Size" then disk[:size] = Regexp.last_match(2)
340         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
341         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
342         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
343         when "Model" then disk[:model] = Regexp.last_match(2)
344         end
345       elsif array && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
346         case Regexp.last_match(1)
347         when "Size" then array[:size] = Regexp.last_match(2)
348         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
349         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
350         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
351         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
352         end
353       end
354     end
355
356     controllers.each do |controller|
357       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
358         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
359       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:3:0/*:3:0:0/scsi_generic/sg*").first
360         controller[:device] = "/dev/#{File.basename(device)}"
361       end
362     end
363   end
364
365   def find_megaraid_disks(devices)
366     controllers = []
367     arrays = []
368
369     controller = nil
370     array = nil
371     disk = nil
372
373     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
374       if line =~ /^PCI information for Controller (\d+)$/
375         controller = {
376           :id => devices[:controllers].count,
377           :arrays => [],
378           :disks => []
379         }
380
381         devices[:controllers] << controller
382
383         controllers << controller
384       elsif line =~ /^Bus Number\s+:\s+(\d+)$/
385         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
386       elsif line =~ /^Device Number\s+:\s+(\d+)$/
387         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
388       elsif line =~ /^Function Number\s+:\s+(\d+)$/
389         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
390       end
391     end
392
393     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
394       if line =~ /^Adapter #(\d+)$/
395         controller = controllers[Regexp.last_match(1).to_i]
396       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
397         case Regexp.last_match(1)
398         when "Product Name" then controller[:model] = Regexp.last_match(2)
399         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
400         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
401         end
402       end
403     end
404
405     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
406       if line =~ /^Adapter #(\d+)$/
407         controller = controllers[Regexp.last_match(1).to_i]
408       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
409         pci_slot = controller[:pci_slot]
410         target = Regexp.last_match(2)
411         device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
412
413         array = {
414           :id => devices[:arrays].count,
415           :controller => controller[:id],
416           :number => Regexp.last_match(1),
417           :device => "/dev/#{File.basename(device)}",
418           :disks => []
419         }
420
421         devices[:arrays] << array
422         controller[:arrays] << array[:id]
423
424         arrays << array
425
426         disk = nil
427       elsif array && line =~ /^PD: (\d+) Information$/
428         disk = {
429           :id => devices[:disks].count,
430           :controller => controller[:id],
431           :arrays => [array[:id]]
432         }
433
434         devices[:disks] << disk
435         controller[:disks] << disk[:id]
436         array[:disks] << disk[:id]
437       elsif disk && line =~ /^Firmware state:\s+(.*\S)\s*$/
438         Regexp.last_match(1).split(/,\s*/).each do |state|
439           case state
440           when "Online" then disk[:status] = "online"
441           when "Hotspare" then disk[:status] = "hotspare"
442           when "Spun Up" then disk[:spun_down] = false
443           when "Spun down" then disk[:spun_down] = true
444           end
445         end
446       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
447         case Regexp.last_match(1)
448         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
449         when "WWN" then disk[:wwn] = Regexp.last_match(2)
450         when "PD Type" then disk[:interface] = Regexp.last_match(2)
451         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
452         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
453         end
454       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
455         case Regexp.last_match(1)
456         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
457         when "Size" then array[:size] = Regexp.last_match(2)
458         end
459       end
460     end
461
462     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
463       if line =~ /^Adapter #(\d+)$/
464         controller = controllers[Regexp.last_match(1).to_i]
465       elsif controller && line =~ /^Enclosure Device ID: \d+$/
466         disk = {
467           :controller => controller[:id]
468         }
469       elsif disk && line =~ /^WWN:\s+(\S+)$/
470         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
471           disk[:id] = devices[:disks].count
472           disk[:wwn] = Regexp.last_match(1)
473
474           devices[:disks] << disk
475         end
476       elsif disk && line =~ /^Firmware state:\s+(.*\S)\s*$/
477         Regexp.last_match(1).split(/,\s*/).each do |state|
478           case state
479           when "Online" then disk[:status] = "online"
480           when "Hotspare" then disk[:status] = "hotspare"
481           when "Spun Up" then disk[:state] = "spun_up"
482           when "Spun down" then disk[:state] = "spun_down"
483           end
484         end
485       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
486         case Regexp.last_match(1)
487         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
488         when "PD Type" then disk[:interface] = Regexp.last_match(2)
489         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
490         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
491         end
492       end
493     end
494
495     controllers.each do |controller|
496       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
497         controller[:device] = "/dev/#{File.basename(device)}"
498       end
499     end
500   end
501
502   def find_mpt_disks(devices)
503     controllers = []
504
505     IO.popen(%w(sas2ircu list)).each do |line|
506       next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
507       controllers[Regexp.last_match(1).to_i] = {
508         :id => devices[:controllers].count,
509         :model => Regexp.last_match(2),
510         :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
511         :arrays => [],
512         :disks => []
513       }
514
515       devices[:controllers] << controllers[Regexp.last_match(1).to_i]
516     end
517
518     controllers.each_with_index do |controller, index|
519       arrays = []
520       disks = []
521
522       array = nil
523       disk = nil
524
525       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
526         if line =~ /^IR volume (\d+)$/
527           array = {
528             :id => devices[:arrays].count,
529             :controller => controller[:id],
530             :number => Regexp.last_match(1),
531             :disks => []
532           }
533
534           devices[:arrays] << array
535           controller[:arrays] << array[:id]
536
537           arrays << array
538         elsif line =~ /^Device is a Hard disk$/
539           disk = {
540             :id => devices[:disks].count,
541             :controller => controller[:id],
542             :arrays => []
543           }
544
545           devices[:disks] << disk
546           controller[:disks] << disk[:id]
547
548           disks << disk
549         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
550           case Regexp.last_match(1)
551           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
552           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
553           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
554           when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
555           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
556           when "Model Number" then disk[:model] = Regexp.last_match(2)
557           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
558           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
559           when "Protocol" then disk[:interface] = Regexp.last_match(2)
560           end
561         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
562           array[:disks] << Regexp.last_match(1)
563         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
564           case Regexp.last_match(1)
565           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
566           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
567           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
568           end
569         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
570           case Regexp.last_match(1)
571           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
572           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
573           end
574         end
575       end
576
577       arrays.each do |array|
578         array[:disks].map! do |location|
579           disk = disks.find { |disk| disk[:location] == location }
580
581           disk[:arrays] << array[:id]
582           disk[:id]
583         end
584       end
585     end
586   end
587
588   def find_adaptec_disks(devices)
589     controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers Found: (\d+)$/i).first.first.to_i
590
591     1.upto(controller_count).each do |controller_number|
592       controller = {
593         :id => devices[:controllers].count,
594         :number => controller_number,
595         :arrays => [],
596         :disks => []
597       }
598
599       devices[:controllers] << controller
600
601       arrays = []
602       disks = []
603
604       array = nil
605       disk = nil
606
607       IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
608         if line =~ /^Logical Device Number (\d+)$/i
609           array = {
610             :id => devices[:arrays].count,
611             :controller => controller[:id],
612             :number => Regexp.last_match(1).to_i,
613             :disks => []
614           }
615
616           devices[:arrays] << array
617           controller[:arrays] << array[:id]
618
619           arrays << array
620         elsif line =~ /^      Device #(\d+)$/
621           disk = nil
622         elsif line =~ /^         Device is a Hard drive$/
623           disk = {
624             :id => devices[:disks].count,
625             :controller => controller[:id],
626             :arrays => []
627           }
628
629           devices[:disks] << disk
630           controller[:disks] << disk[:id]
631
632           disks << disk
633         elsif disk && line =~ /^         Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
634           disk[:channel_number] = Regexp.last_match(1)
635           disk[:device_number] = Regexp.last_match(2)
636         elsif disk && line =~ /^         (\S.*\S)\s*:\s+(\S.*\S)\s*$/
637           case Regexp.last_match(1)
638           when "Reported Location" then disk[:location] = Regexp.last_match(2)
639           when "Vendor" then disk[:vendor] = Regexp.last_match(2)
640           when "Model" then disk[:model] = Regexp.last_match(2)
641           when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
642           when "Serial number" then disk[:serial_number] = Regexp.last_match(2)
643           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
644           when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
645           when "World-wide Name" then disk[:wwn] = Regexp.last_match(2)
646           when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
647           when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
648           end
649         elsif array && line =~ / Present \(.*((?:Connector|Enclosure):\d+,\s*(?:Device|Slot):\d+)\) /
650           array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(/,\s*/, ", ")
651         elsif array && line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
652           case Regexp.last_match(1)
653           when "RAID level" then array[:raid_level] = Regexp.last_match(2)
654           when "RAID Level" then array[:raid_level] = Regexp.last_match(2)
655           when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
656           end
657         elsif line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
658           case Regexp.last_match(1)
659           when "Controller Model" then controller[:model] = Regexp.last_match(2)
660           when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
661           when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
662           when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
663           when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
664           end
665         elsif line =~ /^         Serial Number\s*:\s+(\S.*\S)\s*$/
666           controller[:serial_number] = Regexp.last_match(1)
667         end
668       end
669
670       host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
671         read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
672       end
673
674       arrays.each do |array|
675         array_number = array[:number]
676         device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
677
678         array[:device] = "/dev/#{File.basename(device)}"
679
680         array[:disks].map! do |location|
681           disk = disks.find { |disk| disk[:location] == location }
682
683           device_number = disk[:device_number]
684           device = Dir.glob("#{host}/device/target*:1:#{device_number}/*:1:#{device_number}:0/scsi_generic/*").first
685
686           disk[:device] = "/dev/#{File.basename(device)}"
687
688           disk[:arrays] << array[:id]
689           disk[:id]
690         end
691       end
692     end
693   end
694
695   def find_areca_disks(devices)
696     controller = {
697       :id => devices[:controllers].count,
698       :arrays => [],
699       :disks => []
700     }
701
702     devices[:controllers] << controller
703
704     IO.popen(%w(/opt/areca/x86_64/cli64 sys info)).each do |line|
705       next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
706
707       case Regexp.last_match(1)
708       when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
709       when "BOOT ROM Version" then controller[:bios_version] = Regexp.last_match(2)
710       when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
711       when "Controller Name" then controller[:model] = Regexp.last_match(2)
712       end
713     end
714
715     path = Dir.glob("/sys/bus/pci/devices/*/host*/scsi_host/host*/host_fw_model").find do |file|
716       read_sysctl_file(file) == controller[:model]
717     end
718
719     controller[:pci_slot] = File.basename(File.expand_path("#{path}/../../../.."))
720     controller[:device] = File.basename(Dir.glob(File.expand_path("#{path}/../../../target0:0:16/0:0:16:0/scsi_generic/*")).first)
721
722     arrays = []
723
724     IO.popen(%w(/opt/areca/x86_64/cli64 vsf info)).each do |line|
725       next unless line =~ /^\s+(\d+)\s+/
726       array = {
727         :id => devices[:arrays].count,
728         :number => Regexp.last_match(1),
729         :controller => controller[:id],
730         :disks => []
731       }
732
733       devices[:arrays] << array
734       controller[:arrays] << array[:id]
735
736       arrays << array
737     end
738
739     arrays.each do |array|
740       IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
741         if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
742           pci_slot = controller[:pci_slot]
743           channel = Regexp.last_match(1).to_i
744           id = Regexp.last_match(2).to_i
745           lun = Regexp.last_match(3).to_i
746
747           device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
748
749           array[:device] = "/dev/#{File.basename(device)}"
750         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
751           case Regexp.last_match(1)
752           when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
753           when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
754           when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
755           when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
756           end
757         end
758       end
759     end
760
761     disks = []
762
763     IO.popen(%w(/opt/areca/x86_64/cli64 disk info)).each do |line|
764       next unless line =~ /^\s+(\d+)\s+.*\s+\d+\.\d+GB\s+(\S.*\S)\s*$/
765       next if Regexp.last_match(2) == "N.A."
766
767       disk = {
768         :id => devices[:disks].count,
769         :number => Regexp.last_match(1),
770         :controller => controller[:id],
771         :arrays => []
772       }
773
774       devices[:disks] << disk
775       controller[:disks] << disk[:id]
776
777       if array = arrays.find { |array| array[:raid_set] == Regexp.last_match(2) }
778         disk[:arrays] << array[:id]
779         array[:disks] << disk[:id]
780       end
781
782       disks << disk
783     end
784
785     disks.each do |disk|
786       IO.popen(["/opt/areca/x86_64/cli64", "disk", "info", "drv=#{disk[:number]}"]).each do |line|
787         if line =~ /^IDE Channel\s+:\s+(\d+)\s*$/i
788           disk[:smart_device] = "areca,#{Regexp.last_match(1)}"
789         elsif line =~ /^Device Location\s+:\s+Enclosure#(\d+) Slot#?\s*0*(\d+)\s*$/i
790           disk[:smart_device] = "areca,#{Regexp.last_match(2)}/#{Regexp.last_match(1)}"
791         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
792           case Regexp.last_match(1)
793           when "Model Name" then disk[:vendor], disk[:model] = Regexp.last_match(2).split
794           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
795           when "Disk Capacity" then disk[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
796           end
797         end
798       end
799     end
800   end
801
802   def lvm_devices
803     {
804       :pvs => find_lvm_pvs,
805       :vgs => find_lvm_vgs,
806       :lvs => find_lvm_lvs
807     }
808   end
809
810   def find_lvm_pvs
811     IO.popen(["pvdisplay", "-c"]).each_with_object({}) do |line, pvs|
812       fields = line.strip.split(":")
813
814       pvs[fields[0]] = {
815         :vg => fields[1],
816         :pv_size => fields[2],
817         :pv_status => fields[4],
818         :pe_size => fields[7],
819         :pe_total => fields[8],
820         :pe_free => fields[9],
821         :pe_allocated => fields[10],
822         :pv_uuid => fields[11]
823       }
824     end
825   end
826
827   def find_lvm_vgs
828     IO.popen(["vgdisplay", "-c"]).each_with_object({}) do |line, vgs|
829       fields = line.strip.split(":")
830
831       vgs[fields[0]] = {
832         :vg_access => fields[1],
833         :vg_status => fields[2],
834         :lv_maximum => fields[4],
835         :lv_count => fields[5],
836         :lv_open => fields[6],
837         :pv_maximum => fields[8],
838         :pv_current => fields[9],
839         :pv_actual => fields[10],
840         :vg_size => fields[11],
841         :pe_size => fields[12],
842         :pe_total => fields[13],
843         :pe_allocated => fields[14],
844         :pe_free => fields[15],
845         :vg_uuid => fields[16]
846       }
847     end
848   end
849
850   def find_lvm_lvs
851     IO.popen(["lvdisplay", "-c"]).each_with_object({}) do |line, lvs|
852       fields = line.strip.split(":")
853
854       lvs[fields[0]] = {
855         :vg => fields[1],
856         :lv_access => fields[2],
857         :lv_status => fields[3],
858         :lv_open => fields[5],
859         :lv_size => fields[6],
860         :le_count => fields[7],
861         :lv_minor => fields[11],
862         :lv_major => fields[12]
863       }
864     end
865   end
866
867   def psu_devices
868     devices = {}
869     device = nil
870
871     IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
872       if line =~ /^System Power Supply\s*$/
873         device = {}
874       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
875         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
876       elsif device && line =~ /^\s*$/
877         devices << device
878         device = nil
879       end
880     end
881
882     devices
883   end
884
885   def mc_device
886     device = {}
887
888     IO.popen(["ipmitool", "mc", "info"]).each_with_object([]) do |line, devices|
889       if line =~ /(Manufacturer [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
890         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
891       elsif line =~ /(Product [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
892         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
893       elsif line =~ /([A-Z ]+[A-Z])\s*:\s+(.*\S)\s*$/i
894         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
895       end
896     end
897
898     IO.popen(["ipmitool", "mc", "guid"]).each_with_object([]) do |line, devices|
899       if line =~ /^System GUID\s*:\s+(\S+)\s*$/
900         device[:system_guid] = Regexp.last_match(1)
901       end
902     end
903
904     device
905   end
906
907   collect_data(:default) do
908     hardware Mash.new
909
910     hardware[:pci] = pci_devices
911     hardware[:network] = network_devices
912     hardware[:memory] = memory_devices
913     hardware[:disk] = disk_devices
914     hardware[:lvm] = lvm_devices
915     hardware[:psu] = psu_devices
916     hardware[:mc] = mc_device
917   end
918 end