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