]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Fix PCI address identification for megaraid controllers
[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, Errno::ENOTDIR
9   end
10
11   def read_sysctl_file(file)
12     IO.read(file).strip
13   rescue Errno::ENOENT, Errno::ENOTDIR, 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/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")
157
158     find_md_arrays(disk)
159
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"
165       end
166
167       if disk[:vendor].nil? && disk[:model] =~ /^ATA\s+(.*)$/
168         disk[:vendor] = "ATA"
169         disk[:model] = Regexp.last_match(1)
170       end
171
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)
181         end
182       end
183
184       disk[:model].sub!(/-.*$/, "") if disk[:model]
185     end
186
187     disk
188   end
189
190   def find_direct_disks(devices)
191     Dir.glob("/sys/class/scsi_host/host*") do |host|
192       driver = read_sysctl_file("#{host}/proc_name")
193
194       if %w(ahci mptsas sata_mv sata_nv).include?(driver)
195         bus = host.sub("/sys/class/scsi_host/host", "")
196
197         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
198           next unless File.exist?("#{device}/scsi_disk")
199
200           block = Dir.glob("#{device}/block/*").first
201           size = read_sysctl_file("#{block}/size").to_f / 2
202
203           devices[:disks] << {
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),
210             :arrays => []
211           }
212         end
213       end
214     end
215   end
216
217   def find_nvme_disks(devices)
218     Dir.glob("/sys/class/nvme/nvme*") do |device|
219       controller = {
220         :id => devices[:controllers].count,
221         :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
222         :arrays => [],
223         :disks => []
224       }
225
226       devices[:controllers] << controller
227
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)
233         end
234       end
235
236       Dir.glob("#{device}/nvme*").each do |block|
237         size = read_sysctl_file("#{block}/size").to_f / 2
238
239         disk = {
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),
246           :arrays => []
247         }
248
249         devices[:disks] << disk
250         controller[:disks] << disk[:id]
251       end
252     end
253   end
254
255   def find_md_arrays(devices)
256     controller = {
257       :id => devices[:controllers].count,
258       :type => "md",
259       :arrays => [],
260       :disks => []
261     }
262
263     devices[:controllers] << controller
264
265     array = nil
266
267     File.new("/proc/mdstat", "r").each do |line|
268       if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*)+)$/
269         array = {
270           :id => devices[:arrays].count,
271           :device => "/dev/#{Regexp.last_match(1)}",
272           :status => "optimal",
273           :raid_level => Regexp.last_match(2),
274           :disks => []
275         }
276
277         Regexp.last_match(3).split(" ").each do |member|
278           if member =~ /^(sd[a-z]+|nvme\d+n\d+).*/
279             device = Regexp.last_match(1)
280
281             if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
282               if member =~ /\(F\)/
283                 disk[:status] = "failed"
284               elsif member =~ /\(S\)/
285                 disk[:status] = "hotspare"
286               else
287                 disk[:status] = "online"
288               end
289
290               disk[:arrays] << array[:id]
291               array[:disks] << disk[:id]
292             end
293           end
294         end
295
296         devices[:arrays] << array
297         controller[:arrays] << array[:id]
298       elsif array && line =~ /^\s+(\d+) blocks.*(?:\[([U_]+)\])?/
299         array[:size] = format_disk_size(Regexp.last_match(1).to_i)
300         array[:status] = "degraded" if Regexp.last_match(2) =~ /_/
301       elsif array && line =~ /^\s+\[.*\]\s+(\S+)\s+=/
302         case Regexp.last_match(1)
303         when "recovery" then array[:status] = "rebuilding"
304         when "resync" then array[:status] = "rebuilding"
305         when "checking" then array[:status] = "checking"
306         end
307       end
308     end
309   end
310
311   def find_hp_disks(devices)
312     controllers = []
313     disks = []
314
315     controller = nil
316     array = nil
317     disk = nil
318
319     IO.popen(%w(ssacli controller all show config detail)).each do |line|
320       if line =~ /^Smart (?:Array|HBA) (\S+) /
321         controller = {
322           :id => devices[:controllers].count,
323           :type => "hp",
324           :model => Regexp.last_match(1),
325           :arrays => [],
326           :disks => []
327         }
328
329         devices[:controllers] << controller
330
331         controllers << controller
332
333         array = nil
334         disk = nil
335       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
336         case Regexp.last_match(1)
337         when "Slot" then controller[:slot] = Regexp.last_match(2)
338         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
339         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
340         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
341         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
342         end
343       elsif controller && line =~ /^      Logical Drive: (\d+)$/
344         array = {
345           :id => devices[:arrays].count,
346           :controller => controller[:id],
347           :number => Regexp.last_match(1),
348           :disks => []
349         }
350
351         devices[:arrays] << array
352         controller[:arrays] << array[:id]
353
354         disk = nil
355       elsif array && line =~ /^      physicaldrive (\S+)$/
356         disk = {
357           :id => devices[:disks].count,
358           :controller => controller[:id],
359           :arrays => [array[:id]],
360           :location => Regexp.last_match(1)
361         }
362
363         devices[:disks] << disk
364         controller[:disks] << disk[:id]
365         array[:disks] << disk[:id]
366       elsif disk && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
367         case Regexp.last_match(1)
368         when "Status" then disk[:status] = Regexp.last_match(2)
369         when "Drive Type" then disk[:drive_type] = Regexp.last_match(2)
370         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
371         when "Size" then disk[:size] = Regexp.last_match(2)
372         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
373         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
374         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
375         when "Model" then disk[:model] = Regexp.last_match(2)
376         end
377       elsif array && line =~ /^         Status:\s+(.*\S)\s*$/
378         case Regexp.last_match(1)
379         when "OK" then array[:status] = "optimal"
380         else array[:status] = "unknown"
381         end
382       elsif array && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
383         case Regexp.last_match(1)
384         when "Size" then array[:size] = Regexp.last_match(2)
385         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
386         when "Status" then array[:status] = Regexp.last_match(2)
387         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
388         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
389         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
390         end
391       end
392     end
393
394     controllers.each do |controller|
395       slot = controller[:slot]
396
397       IO.popen(%W(ssacli controller slot=#{slot} pd all show status)).each do |line|
398         if line =~ /^   physicaldrive (\S+) /
399           disks << Regexp.last_match(1)
400         end
401       end
402
403       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
404         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
405       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:3:0/*:3:0:0/scsi_generic/sg*").first
406         controller[:device] = "/dev/#{File.basename(device)}"
407       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:1:0/*:1:0:*/scsi_generic/sg*").first
408         controller[:device] = "/dev/#{File.basename(device)}"
409       end
410     end
411
412     devices[:disks].each do |disk|
413       disk[:smart_device] = "cciss,#{disks.find_index(disk[:location])}"
414
415       if disk[:status] == "Failed"
416         disk[:status] = "failed"
417       elsif disk[:status] == "OK" && disk[:drive_type] == "Data Drive"
418         disk[:status] = "online"
419       elsif disk[:status] == "OK" && disk[:drive_type] == "Spare Drive"
420         disk[:status] = "hotspare"
421       else
422         disk[:status] = "unknown"
423       end
424
425       disk.delete(:drive_type)
426     end
427   end
428
429   def find_megaraid_disks(devices)
430     controllers = []
431     arrays = []
432
433     controller = nil
434     array = nil
435     disk = nil
436
437     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
438       if line =~ /^PCI information for Controller (\d+)$/
439         controller = {
440           :id => devices[:controllers].count,
441           :type => "megaraid",
442           :arrays => [],
443           :disks => []
444         }
445
446         devices[:controllers] << controller
447
448         controllers << controller
449       elsif line =~ /^Bus Number\s+:\s+([0-9a-f]+)$/i
450         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
451       elsif line =~ /^Device Number\s+:\s+([0-9a-f]+)$/i
452         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
453       elsif line =~ /^Function Number\s+:\s+([0-9a-f]+)$/i
454         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
455       end
456     end
457
458     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
459       if line =~ /^Adapter #(\d+)$/
460         controller = controllers[Regexp.last_match(1).to_i]
461       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
462         case Regexp.last_match(1)
463         when "Product Name" then controller[:model] = Regexp.last_match(2)
464         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
465         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
466         end
467       end
468     end
469
470     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
471       if line =~ /^Adapter #(\d+)$/
472         controller = controllers[Regexp.last_match(1).to_i]
473       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
474         pci_slot = controller[:pci_slot]
475         target = Regexp.last_match(2)
476         device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
477
478         array = {
479           :id => devices[:arrays].count,
480           :controller => controller[:id],
481           :number => Regexp.last_match(1),
482           :device => "/dev/#{File.basename(device)}",
483           :disks => []
484         }
485
486         devices[:arrays] << array
487         controller[:arrays] << array[:id]
488
489         arrays << array
490
491         disk = nil
492       elsif array && line =~ /^PD: (\d+) Information$/
493         disk = {
494           :id => devices[:disks].count,
495           :controller => controller[:id],
496           :arrays => [array[:id]]
497         }
498
499         devices[:disks] << disk
500         controller[:disks] << disk[:id]
501         array[:disks] << disk[:id]
502       elsif disk && line =~ /^Firmware state:\s+(.*\S),\s*(.*\S)\s*$/
503         case Regexp.last_match(1)
504         when "Unconfigured(good)" then disk[:status] = "unconfigured"
505         when "Unconfigured(bad)" then disk[:status] = "unconfigured"
506         when "Hotspare" then disk[:status] = "hotspare"
507         when "Offline" then disk[:status] = "offline"
508         when "Online" then disk[:status] = "online"
509         when "Rebuild" then disk[:status] = "rebuilding"
510         when "Failed" then disk[:status] = "failed"
511         when "Copyback" then disk[:status] = "rebuilding"
512         else disk[:status] = "unknown"
513         end
514         case Regexp.last_match(2)
515         when "Spun Up" then disk[:state] = "spun_up"
516         when "Spun down" then disk[:state] = "spun_down"
517         else disk[:state] = "unknown"
518         end
519       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
520         case Regexp.last_match(1)
521         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
522         when "WWN" then disk[:wwn] = Regexp.last_match(2)
523         when "PD Type" then disk[:interface] = Regexp.last_match(2)
524         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
525         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
526         end
527       elsif array && line =~ /^State\s*:\s+(.*\S)\s*$/
528         case Regexp.last_match(1)
529         when "Partially Degraded" then array[:status] = "degraded"
530         when "Degraded" then array[:status] = "degraded"
531         when "Optimal" then array[:status] = "optimal"
532         when "Consistency Check" then array[:status] = "checking"
533         when "Background Initialization" then array[:status] = "initialising"
534         when "Initialization" then array[:status] = "initialising"
535         when "Reconstruction" then array[:status] = "rebuilding"
536         else array[:status] = "unknown"
537         end
538       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
539         case Regexp.last_match(1)
540         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
541         when "Size" then array[:size] = Regexp.last_match(2)
542         end
543       end
544     end
545
546     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
547       if line =~ /^Adapter #(\d+)$/
548         controller = controllers[Regexp.last_match(1).to_i]
549       elsif controller && line =~ /^Enclosure Device ID: \d+$/
550         disk = {
551           :controller => controller[:id]
552         }
553       elsif disk && line =~ /^WWN:\s+(\S+)$/
554         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
555           disk[:id] = devices[:disks].count
556           disk[:wwn] = Regexp.last_match(1)
557
558           devices[:disks] << disk
559         end
560       elsif disk && line =~ /^Firmware state:\s+(.*\S),\s*(.*\S)\s*$/
561         case Regexp.last_match(1)
562         when "Unconfigured(good)" then disk[:status] = "unconfigured"
563         when "Unconfigured(bad)" then disk[:status] = "unconfigured"
564         when "Hotspare" then disk[:status] = "hotspare"
565         when "Offline" then disk[:status] = "offline"
566         when "Online" then disk[:status] = "online"
567         when "Rebuild" then disk[:status] = "rebuilding"
568         when "Failed" then disk[:status] = "failed"
569         when "Copyback" then disk[:status] = "rebuilding"
570         else disk[:status] = "unknown"
571         end
572         case Regexp.last_match(2)
573         when "Spun Up" then disk[:state] = "spun_up"
574         when "Spun down" then disk[:state] = "spun_down"
575         else disk[:state] = "unknown"
576         end
577       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
578         case Regexp.last_match(1)
579         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
580         when "PD Type" then disk[:interface] = Regexp.last_match(2)
581         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
582         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
583         end
584       end
585     end
586
587     controllers.each do |controller|
588       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
589         controller[:device] = "/dev/#{File.basename(device)}"
590       end
591     end
592   end
593
594   def find_mpt1_disks(devices)
595     controllers = []
596     disks = []
597
598     controller = nil
599
600     IO.popen(%w(lsiutil -s)).each do |line|
601       if line =~ /^\/proc\/mpt\/ioc(\d+)\s+LSI Logic\s+(\S+)\s+/
602         controller = {
603           :id => devices[:controllers].count,
604           :type => "mpt1",
605           :model => Regexp.last_match(1),
606           :arrays => [],
607           :disks => []
608         }
609
610         controllers << controller
611         devices[:controllers] << controller
612       elsif line =~ /^\s+(\d+)\s+(\d+)\s+PhysDisk (\d+)\s+(\S+)\s+(\S+)\s+\d+\s+(\S+)\s+/
613         disks[Regexp.last_match(3).to_i] = {
614           :id => devices[:disks].count,
615           :controller => controller[:id],
616           :vendor => Regexp.last_match(4),
617           :model => Regexp.last_match(5),
618           :sas_address => Regexp.last_match(6),
619           :arrays => []
620         }
621
622         controller[:disks] << devices[:disks].count
623         devices[:disks] << disks[Regexp.last_match(3).to_i]
624       end
625     end
626
627     controllers.each_with_index do |controller, index|
628       port = index + 1
629       array = nil
630
631       IO.popen(["lsiutil", "-p", port.to_s, "-a", "69,0"]).each do |line|
632         if line =~ /^ (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+RAID/
633           seg = Regexp.last_match(1).to_i
634           bus = Regexp.last_match(2).to_i
635           dev = Regexp.last_match(3).to_i
636           fun = Regexp.last_match(4).to_i
637
638           controller[:pci_slot] = sprintf("%04x:%02x:%02x.%01x", seg, bus, dev, fun)
639         end
640       end
641
642       IO.popen(["lsiutil", "-p", port.to_s, "-a", "21,1,0,0"]).each do |line|
643         if line =~ /^Volume (\d+) is/
644           array = {
645             :id => devices[:arrays].count,
646             :controller => controller[:id],
647             :number => Regexp.last_match(1),
648             :disks => []
649           }
650
651           devices[:arrays] << array
652           controller[:arrays] << array[:id]
653         elsif line =~ /^  Member \d+ is PhysDisk (\d+) /
654           array[:disks] << disks[Regexp.last_match(1).to_i][:id]
655           disks[Regexp.last_match(1).to_i][:arrays] << array[:id]
656         end
657       end
658     end
659
660     disks.each do |disk|
661       slot = controllers[disk[:controller]][:pci_slot]
662       sas_address = "0x#{disk[:sas_address]}"
663
664       Dir.glob("/sys/bus/pci/devices/#{slot}/host*/port-*:*/end_device-*:*/sas_device/end_device-*:*").each do |sas_device|
665         if read_sysctl_file("#{sas_device}/sas_address") == sas_address
666           if device = Dir.glob("#{sas_device}/device/target*:0:*/*:0:*:0/scsi_generic/sg*").first
667             disk[:device] = "/dev/#{File.basename(device)}"            
668           end
669         end
670       end
671     end
672   end
673
674   def find_mpt2_disks(devices)
675     controllers = []
676
677     IO.popen(%w(sas2ircu list)).each do |line|
678       next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
679       controllers[Regexp.last_match(1).to_i] = {
680         :id => devices[:controllers].count,
681         :type => "mpt2",
682         :model => Regexp.last_match(2),
683         :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
684         :arrays => [],
685         :disks => []
686       }
687
688       devices[:controllers] << controllers[Regexp.last_match(1).to_i]
689     end
690
691     controllers.each_with_index do |controller, index|
692       arrays = []
693       disks = []
694
695       array = nil
696       disk = nil
697
698       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
699         if line =~ /^IR volume (\d+)$/
700           array = {
701             :id => devices[:arrays].count,
702             :controller => controller[:id],
703             :number => Regexp.last_match(1),
704             :disks => []
705           }
706
707           devices[:arrays] << array
708           controller[:arrays] << array[:id]
709
710           arrays << array
711         elsif line =~ /^Device is a Hard disk$/
712           disk = {
713             :id => devices[:disks].count,
714             :controller => controller[:id],
715             :arrays => []
716           }
717
718           devices[:disks] << disk
719           controller[:disks] << disk[:id]
720
721           disks << disk
722         elsif disk && line =~ /^  State\s+:\s+(.*\S)\s*$/
723           Regexp.last_match(1).split(/,\s*/).each do |state|
724             case state
725             when "Online (ONL)" then disk[:status] = "online"
726             when "Hot Spare (HSP)" then disk[:status] = "hotspare"
727             when "Ready (RDY)" then disk[:status] = "unconfigured"
728             when "Available (AVL)" then disk[:status] = "unconfigured"
729             when "Failed (FLD)" then disk[:status] = "failed"
730             when "Missing (MIS)" then disk[:status] = "missing"
731             when "Standby (SBY)" then disk[:status] = "unconfigured"
732             when "Out of Sync (OSY)" then disk[:status] = "degraded"
733             when "Degraded (DGD)" then disk[:status] = "degraded"
734             when "Rebuilding (RBLD)" then disk[:status] = "rebuilding"
735             when "Optimal (OPT)" then disk[:status] = "online"
736             else disk[:status] = "unknown"
737             end
738           end
739         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
740           case Regexp.last_match(1)
741           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
742           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
743           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
744           when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
745           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
746           when "Model Number" then disk[:model] = Regexp.last_match(2)
747           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
748           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
749           when "Protocol" then disk[:interface] = Regexp.last_match(2)
750           end
751         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
752           array[:disks] << Regexp.last_match(1)
753         elsif array && line =~ /^  Status of volume\s+:\s+(.*\S)\s*$/
754           Regexp.last_match(1).split(/,\s*/).each do |state|
755             case state
756             when "Okay (OKY)" then array[:status] = "optimal"
757             when "Degraded (DGD)" then array[:status] = "degraded"
758             when "Failed (FLD)" then array[:status] = "failed"
759             when "Missing (MIS)" then array[:status] = "missing"
760             when "Initializing (INIT)" then array[:status] = "initialising"
761             when "Online (ONL)" then array[:status] = "optimal"
762             else array[:status] = "unknown"
763             end
764           end
765         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
766           case Regexp.last_match(1)
767           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
768           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
769           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
770           end
771         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
772           case Regexp.last_match(1)
773           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
774           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
775           end
776         end
777       end
778
779       arrays.each do |array|
780         array[:disks].map! do |location|
781           disk = disks.find { |disk| disk[:location] == location }
782
783           disk[:arrays] << array[:id]
784           disk[:id]
785         end
786       end
787     end
788   end
789
790   def find_adaptec_disks(devices)
791     controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers Found: (\d+)$/i).first.first.to_i
792
793     1.upto(controller_count).each do |controller_number|
794       controller = {
795         :id => devices[:controllers].count,
796         :number => controller_number,
797         :type => "adaptec",
798         :arrays => [],
799         :disks => []
800       }
801
802       devices[:controllers] << controller
803
804       arrays = []
805       disks = []
806
807       array = nil
808       disk = nil
809
810       IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
811         if line =~ /^Logical Device Number (\d+)$/i
812           array = {
813             :id => devices[:arrays].count,
814             :controller => controller[:id],
815             :number => Regexp.last_match(1).to_i,
816             :disks => []
817           }
818
819           devices[:arrays] << array
820           controller[:arrays] << array[:id]
821
822           arrays << array
823         elsif line =~ /^      Device #(\d+)$/
824           disk = nil
825         elsif line =~ /^         Device is a Hard drive$/
826           disk = {
827             :id => devices[:disks].count,
828             :controller => controller[:id],
829             :arrays => []
830           }
831
832           devices[:disks] << disk
833           controller[:disks] << disk[:id]
834
835           disks << disk
836         elsif disk && line =~ /^         Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
837           disk[:channel_number] = Regexp.last_match(1)
838           disk[:device_number] = Regexp.last_match(2)
839         elsif disk && line =~ /^         State\s*:\s+(\S.*\S)\s*$/
840           case Regexp.last_match(1)
841           when "Online" then disk[:status] = "online"
842           when "Online (JBOD)" then disk[:status] = "online"
843           when "Hot Spare" then disk[:status] = "hotspare"
844           when "Ready" then disk[:status] = "unconfigured"
845           when "Global Hot-Spare" then disk[:status] = "hostspare"
846           when "Dedicated Hot-Spare" then disk[:status] = "hotspare"
847           else disk[:status] = "unknown"
848         end
849         elsif disk && line =~ /^         (\S.*\S)\s*:\s+(\S.*\S)\s*$/
850           case Regexp.last_match(1)
851           when "Reported Location" then disk[:location] = Regexp.last_match(2)
852           when "Vendor" then disk[:vendor] = Regexp.last_match(2)
853           when "Model" then disk[:model] = Regexp.last_match(2)
854           when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
855           when "Serial number" then disk[:serial_number] = Regexp.last_match(2)
856           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
857           when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
858           when "World-wide Name" then disk[:wwn] = Regexp.last_match(2)
859           when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
860           when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
861           end
862         elsif array && line =~ / Present \(.*((?:Connector|Enclosure):\d+,\s*(?:Device|Slot):\d+)\) /
863           array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(/,\s*/, ", ")
864         elsif array && line =~ /^   Status of Logical Device\s*:\s+(\S.*\S)\s*$/
865           case Regexp.last_match(1)
866           when "Optimal" then array[:status] = "optimal"
867           else array[:status] = "unknown"
868         end
869         elsif array && line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
870           case Regexp.last_match(1)
871           when "RAID level" then array[:raid_level] = Regexp.last_match(2)
872           when "RAID Level" then array[:raid_level] = Regexp.last_match(2)
873           when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
874           end
875         elsif line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
876           case Regexp.last_match(1)
877           when "Controller Model" then controller[:model] = Regexp.last_match(2)
878           when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
879           when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
880           when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
881           when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
882           end
883         elsif line =~ /^         Serial Number\s*:\s+(\S.*\S)\s*$/
884           controller[:serial_number] = Regexp.last_match(1)
885         end
886       end
887
888       host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
889         read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
890       end
891
892       arrays.each do |array|
893         array_number = array[:number]
894         device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
895
896         array[:device] = "/dev/#{File.basename(device)}"
897
898         array[:disks].map! do |location|
899           disk = disks.find { |disk| disk[:location] == location }
900
901           controller_number = controller[:number] - 1
902           device_number = disk[:device_number]
903
904           disk[:device] = "/dev/#{File.basename(device)}"
905           disk[:smart_device] = "aacraid,#{controller_number},0,#{device_number}"
906
907           disk[:arrays] << array[:id]
908           disk[:id]
909         end
910       end
911     end
912   end
913
914   def find_areca_disks(devices)
915     controller = {
916       :id => devices[:controllers].count,
917       :type => "areca",
918       :arrays => [],
919       :disks => []
920     }
921
922     devices[:controllers] << controller
923
924     IO.popen(%w(/opt/areca/x86_64/cli64 sys info)).each do |line|
925       next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
926
927       case Regexp.last_match(1)
928       when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
929       when "BOOT ROM Version" then controller[:bios_version] = Regexp.last_match(2)
930       when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
931       when "Controller Name" then controller[:model] = Regexp.last_match(2)
932       end
933     end
934
935     path = Dir.glob("/sys/bus/pci/devices/*/host*/scsi_host/host*/host_fw_model").find do |file|
936       read_sysctl_file(file) == controller[:model]
937     end
938
939     controller[:pci_slot] = File.basename(File.expand_path("#{path}/../../../.."))
940     controller[:device] = File.basename(Dir.glob(File.expand_path("#{path}/../../../target0:0:16/0:0:16:0/scsi_generic/*")).first)
941
942     arrays = []
943
944     IO.popen(%w(/opt/areca/x86_64/cli64 vsf info)).each do |line|
945       next unless line =~ /^\s+(\d+)\s+/
946       array = {
947         :id => devices[:arrays].count,
948         :number => Regexp.last_match(1),
949         :controller => controller[:id],
950         :disks => []
951       }
952
953       devices[:arrays] << array
954       controller[:arrays] << array[:id]
955
956       arrays << array
957     end
958
959     arrays.each do |array|
960       IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
961         if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
962           pci_slot = controller[:pci_slot]
963           channel = Regexp.last_match(1).to_i
964           id = Regexp.last_match(2).to_i
965           lun = Regexp.last_match(3).to_i
966
967           device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
968
969           array[:device] = "/dev/#{File.basename(device)}"
970         elsif line =~ /^Volume State\s+:\s+(.*\S)\s*$/
971           case Regexp.last_match(1)
972           when "Normal" then array[:status] = "optimal"
973           else array[:status] = "unknown"
974           end
975         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
976           case Regexp.last_match(1)
977           when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
978           when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
979           when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
980           when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
981           end
982         end
983       end
984     end
985
986     disks = []
987
988     IO.popen(%w(/opt/areca/x86_64/cli64 disk info)).each do |line|
989       next unless line =~ /^\s+(\d+)\s+.*\s+\d+\.\d+GB\s+(\S.*\S)\s*$/
990       next if Regexp.last_match(2) == "N.A."
991
992       disk = {
993         :id => devices[:disks].count,
994         :number => Regexp.last_match(1),
995         :controller => controller[:id],
996         :arrays => []
997       }
998
999       devices[:disks] << disk
1000       controller[:disks] << disk[:id]
1001
1002       if array = arrays.find { |array| array[:raid_set] == Regexp.last_match(2) }
1003         disk[:arrays] << array[:id]
1004         array[:disks] << disk[:id]
1005       end
1006
1007       disks << disk
1008     end
1009
1010     disks.each do |disk|
1011       IO.popen(["/opt/areca/x86_64/cli64", "disk", "info", "drv=#{disk[:number]}"]).each do |line|
1012         if line =~ /^IDE Channel\s+:\s+(\d+)\s*$/i
1013           disk[:smart_device] = "areca,#{Regexp.last_match(1)}"
1014         elsif line =~ /^Device Location\s+:\s+Enclosure#(\d+) Slot#?\s*0*(\d+)\s*$/i
1015           disk[:smart_device] = "areca,#{Regexp.last_match(2)}/#{Regexp.last_match(1)}"
1016         elsif line =~ /^Device State\s+:\s+(.*\S)\s*$/
1017           case Regexp.last_match(1)
1018           when "NORMAL" then disk[:status] = "online"
1019           else disk[:status] = "unknown"
1020           end
1021         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
1022           case Regexp.last_match(1)
1023           when "Model Name" then disk[:vendor], disk[:model] = Regexp.last_match(2).split
1024           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
1025           when "Disk Capacity" then disk[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
1026           end
1027         end
1028       end
1029     end
1030   end
1031
1032   def lvm_devices
1033     {
1034       :pvs => find_lvm_pvs,
1035       :vgs => find_lvm_vgs,
1036       :lvs => find_lvm_lvs
1037     }
1038   end
1039
1040   def find_lvm_pvs
1041     IO.popen(["pvdisplay", "-c"]).each_with_object({}) do |line, pvs|
1042       fields = line.strip.split(":")
1043
1044       pvs[fields[0]] = {
1045         :vg => fields[1],
1046         :pv_size => fields[2],
1047         :pv_status => fields[4],
1048         :pe_size => fields[7],
1049         :pe_total => fields[8],
1050         :pe_free => fields[9],
1051         :pe_allocated => fields[10],
1052         :pv_uuid => fields[11]
1053       }
1054     end
1055   end
1056
1057   def find_lvm_vgs
1058     IO.popen(["vgdisplay", "-c"]).each_with_object({}) do |line, vgs|
1059       fields = line.strip.split(":")
1060
1061       vgs[fields[0]] = {
1062         :vg_access => fields[1],
1063         :vg_status => fields[2],
1064         :lv_maximum => fields[4],
1065         :lv_count => fields[5],
1066         :lv_open => fields[6],
1067         :pv_maximum => fields[8],
1068         :pv_current => fields[9],
1069         :pv_actual => fields[10],
1070         :vg_size => fields[11],
1071         :pe_size => fields[12],
1072         :pe_total => fields[13],
1073         :pe_allocated => fields[14],
1074         :pe_free => fields[15],
1075         :vg_uuid => fields[16]
1076       }
1077     end
1078   end
1079
1080   def find_lvm_lvs
1081     IO.popen(["lvdisplay", "-c"]).each_with_object({}) do |line, lvs|
1082       fields = line.strip.split(":")
1083
1084       lvs[fields[0]] = {
1085         :vg => fields[1],
1086         :lv_access => fields[2],
1087         :lv_status => fields[3],
1088         :lv_open => fields[5],
1089         :lv_size => fields[6],
1090         :le_count => fields[7],
1091         :lv_minor => fields[11],
1092         :lv_major => fields[12]
1093       }
1094     end
1095   end
1096
1097   def psu_devices
1098     device = nil
1099
1100     IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
1101       if line =~ /^System Power Supply\s*$/
1102         device = {}
1103       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
1104         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
1105       elsif device && line =~ /^\s*$/
1106         devices << device
1107         device = nil
1108       end
1109     end
1110   end
1111
1112   def mc_device
1113     device = {}
1114
1115     IO.popen(["ipmitool", "mc", "info"]).each_with_object([]) do |line, devices|
1116       if line =~ /(Manufacturer [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1117         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1118       elsif line =~ /(Product [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1119         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1120       elsif line =~ /([A-Z ]+[A-Z])\s*:\s+(.*\S)\s*$/i
1121         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1122       end
1123     end
1124
1125     IO.popen(["ipmitool", "mc", "guid"]).each_with_object([]) do |line, devices|
1126       if line =~ /^System GUID\s*:\s+(\S+)\s*$/
1127         device[:system_guid] = Regexp.last_match(1)
1128       end
1129     end
1130
1131     device
1132   end
1133
1134   collect_data(:default) do
1135     hardware Mash.new
1136
1137     hardware[:pci] = pci_devices
1138     hardware[:network] = network_devices
1139     hardware[:memory] = memory_devices
1140     hardware[:disk] = disk_devices
1141     hardware[:lvm] = lvm_devices
1142     hardware[:psu] = psu_devices
1143     hardware[:mc] = mc_device
1144   end
1145 end