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