]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Recognise some extra disk statuses on HP controllers
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
1 require "pathname"
2
3 Ohai.plugin(:Hardware) do
4   provides "hardware"
5
6   def read_sysctl_link(file)
7     File.basename(File.readlink(file))
8   rescue Errno::ENOENT, Errno::ENOTDIR
9   end
10
11   def read_sysctl_file(file)
12     IO.read(file).strip
13   rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EINVAL
14   end
15
16   def parse_memory_size(size)
17     if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
18       Regexp.last_match(1).to_f * 2**30
19     elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
20       Regexp.last_match(1).to_f * 2**20
21     elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
22       Regexp.last_match(1).to_f * 2**10
23     end
24   end
25
26   def format_disk_size(kb)
27     if kb == 0
28       ""
29     else
30       kblog10 = Math.log10(kb).floor
31
32       kb = kb.to_f * 2 / 10**kblog10
33       kb = kb.round.to_f / 2
34
35       if kblog10 >= 9
36         format "%gTB", kb * 10**(kblog10 - 9)
37       elsif kblog10 >= 6
38         format "%dGB", kb * 10**(kblog10 - 6)
39       else
40         format "%dMB", kb * 10**(kblog10 - 3)
41       end
42     end
43   end
44
45   def memory_to_disk_size(size)
46     format_disk_size(parse_memory_size(size))
47   end
48
49   def find_sas_device(address)
50     file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
51       read_sysctl_file(file) == "0x#{address}"
52     end
53
54     if file
55       dir = File.dirname(file)
56       device = Dir.glob("#{dir}/block/*").first ||
57                Dir.glob("#{dir}/scsi_generic/*").first
58
59       "/dev/#{File.basename(device)}"
60     end
61   end
62
63   def pci_devices
64     devices = {}
65     device = nil
66
67     IO.popen(["lspci", "-Dkvmm"]).each do |line|
68       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
69         device = {
70           :slot => Regexp.last_match(1),
71           :domain => Regexp.last_match(2),
72           :bus => Regexp.last_match(3),
73           :device => Regexp.last_match(4),
74           :function => Regexp.last_match(5)
75         }
76       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
77         case Regexp.last_match(1)
78         when "Class" then device[:class_name] = Regexp.last_match(2)
79         when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
80         when "Device" then device[:device_name] = Regexp.last_match(2)
81         when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
82         when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
83         when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
84         when "Rev" then device[:revision] = Regexp.last_match(2)
85         when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
86         when "Driver" then device[:driver] = Regexp.last_match(2)
87         when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
88         end
89       elsif device && line =~ /^\s*$/
90         devices[device[:slot]] = device
91         device = nil
92       end
93     end
94
95     IO.popen(["lspci", "-Dkvmmn"]).each do |line|
96       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
97         device = devices[Regexp.last_match(1)]
98       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
99         case Regexp.last_match(1)
100         when "Class" then device[:class_id] = Regexp.last_match(2)
101         when "Vendor" then device[:vendor_id] = Regexp.last_match(2)
102         when "Device" then device[:device_id] = Regexp.last_match(2)
103         when "SVendor" then device[:subsystem_vendor_id] = Regexp.last_match(2)
104         when "SDevice" then device[:subsystem_device_id] = Regexp.last_match(2)
105         end
106       elsif device && line =~ /^\s*$/
107         device = nil
108       end
109     end
110
111     devices
112   end
113
114   def network_devices
115     Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
116       name = File.basename(device)
117
118       devices[name] = {
119         :device => read_sysctl_link("#{device}/device"),
120         :duplex => read_sysctl_file("#{device}/duplex"),
121         :speed => read_sysctl_file("#{device}/speed")
122       }.delete_if { |_, v| v.nil? }
123     end
124   end
125
126   def memory_devices
127     device = nil
128
129     IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
130       if line =~ /^Memory Device\s*$/
131         device = {}
132       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
133         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
134       elsif device && line =~ /^\s*$/
135         devices << device
136         device = nil
137       end
138     end
139   end
140
141   def disk_devices
142     disk = Mash.new
143
144     disk[:controllers] = []
145     disk[:arrays] = []
146     disk[:disks] = []
147
148     find_direct_disks(disk)
149     find_nvme_disks(disk)
150
151     find_hp_disks(disk) if File.exist?("/usr/sbin/ssacli")
152     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
153     find_mpt1_disks(disk) if File.exist?("/usr/sbin/lsiutil")
154     find_mpt2_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
155     find_adaptec_disks(disk) if File.exist?("/usr/sbin/arcconf")
156     find_areca_disks(disk) if File.exist?("/opt/areca/x86_64/cli64")
157
158     find_md_arrays(disk)
159
160     disk[:disks].each do |disk|
161       if disk[:vendor] =~ /^(BTWA|CVPR|PHDV)/ && disk[:model] == "INTEL"
162         disk[:model] = disk[:serial_number]
163         disk[:serial_number] = disk[:vendor]
164         disk[:vendor] = "INTEL"
165       end
166
167       if disk[:vendor].nil? && disk[:model] =~ /^ATA\s+(.*)$/
168         disk[:vendor] = "ATA"
169         disk[:model] = Regexp.last_match(1)
170       end
171
172       if disk[:vendor].nil? || disk[:vendor] == "ATA"
173         if disk[:model] =~ /^(\S+)\s+(.*)$/
174           disk[:vendor] = Regexp.last_match(1)
175           disk[:model] = Regexp.last_match(2)
176         elsif disk[:model] =~ /^ST/
177           disk[:vendor] = "SEAGATE"
178         elsif disk[:model] =~ /^C300-(.*)$/
179           disk[:vendor] = "CRUCIAL"
180           disk[:model] = Regexp.last_match(1)
181         end
182       end
183
184       disk[:model].sub!(/-.*$/, "") if disk[:model]
185     end
186
187     disk
188   end
189
190   def find_direct_disks(devices)
191     Dir.glob("/sys/class/scsi_host/host*") do |host|
192       driver = read_sysctl_file("#{host}/proc_name")
193
194       if %w(ahci mptsas sata_mv sata_nv).include?(driver)
195         bus = host.sub("/sys/class/scsi_host/host", "")
196
197         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
198           next unless File.exist?("#{device}/scsi_disk")
199
200           block = Dir.glob("#{device}/block/*").first
201           size = read_sysctl_file("#{block}/size").to_f / 2
202
203           devices[:disks] << {
204             :id => devices[:disks].count,
205             :device => "/dev/#{File.basename(block)}",
206             :vendor => read_sysctl_file("#{device}/vendor"),
207             :model => read_sysctl_file("#{device}/model"),
208             :firmware_version => read_sysctl_file("#{device}/rev"),
209             :size => format_disk_size(size),
210             :arrays => []
211           }
212         end
213       end
214     end
215   end
216
217   def find_nvme_disks(devices)
218     Dir.glob("/sys/class/nvme/nvme*") do |device|
219       controller = {
220         :id => devices[:controllers].count,
221         :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
222         :arrays => [],
223         :disks => []
224       }
225
226       devices[:controllers] << controller
227
228       IO.popen(["lspci", "-Dkvmm", "-s", controller[:pci_slot]]).each do |line|
229         if line =~ /^SVendor:\s+(\S.*\S)\s*$/
230           controller[:vendor] = Regexp.last_match(1)
231         elsif line =~ /^SDevice:\s+(\S.*\S)\s*$/
232           controller[:model] = Regexp.last_match(1)
233         end
234       end
235
236       Dir.glob("#{device}/nvme*").each do |block|
237         size = read_sysctl_file("#{block}/size").to_f / 2
238
239         disk = {
240           :id => devices[:disks].count,
241           :controller => controller[:id],
242           :device => "/dev/#{File.basename(block)}",
243           :vendor => controller[:vendor],
244           :model => controller[:model],
245           :size => format_disk_size(size),
246           :arrays => []
247         }
248
249         devices[:disks] << disk
250         controller[:disks] << disk[:id]
251       end
252     end
253   end
254
255   def find_md_arrays(devices)
256     controller = {
257       :id => devices[:controllers].count,
258       :type => "md",
259       :arrays => [],
260       :disks => []
261     }
262
263     devices[:controllers] << controller
264
265     array = nil
266
267     File.new("/proc/mdstat", "r").each do |line|
268       if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*)+)$/
269         array = {
270           :id => devices[:arrays].count,
271           :device => "/dev/#{Regexp.last_match(1)}",
272           :status => "optimal",
273           :raid_level => Regexp.last_match(2),
274           :disks => []
275         }
276
277         Regexp.last_match(3).split(" ").each do |member|
278           if member =~ /^(sd[a-z]+|nvme\d+n\d+).*/
279             device = Regexp.last_match(1)
280
281             if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
282               if member =~ /\(F\)/
283                 disk[:status] = "failed"
284               elsif member =~ /\(S\)/
285                 disk[:status] = "hotspare"
286               else
287                 disk[:status] = "online"
288               end
289
290               disk[:arrays] << array[:id]
291               array[:disks] << disk[:id]
292             end
293           end
294         end
295
296         devices[:arrays] << array
297         controller[:arrays] << array[:id]
298       elsif array && line =~ /^\s+(\d+) blocks.*(?:\[([U_]+)\])?/
299         array[:size] = format_disk_size(Regexp.last_match(1).to_i)
300         array[:status] = "degraded" if Regexp.last_match(2) =~ /_/
301       elsif array && line =~ /^\s+\[.*\]\s+(\S+)\s+=/
302         case Regexp.last_match(1)
303         when "recovery" then array[:status] = "rebuilding"
304         when "resync" then array[:status] = "rebuilding"
305         when "checking" then array[:status] = "checking"
306         end
307       end
308     end
309   end
310
311   def find_hp_disks(devices)
312     controllers = []
313     disks = []
314
315     controller = nil
316     array = nil
317     disk = nil
318
319     IO.popen(%w(ssacli controller all show config detail)).each do |line|
320       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] == "Predictive Failure"
421         disk[:status] = "failed"
422       elsif disk[:status] == "OK" && disk[:drive_type] == "Data Drive"
423         disk[:status] = "online"
424       elsif disk[:status] == "OK" && disk[:drive_type] == "Spare Drive"
425         disk[:status] = "hotspare"
426       elsif disk[:status] == "OK" && disk[:drive_type] == "Unassigned Drive"
427         disk[:status] = "unconfigured"
428       else
429         disk[:status] = "unknown"
430       end
431
432       disk.delete(:drive_type)
433     end
434   end
435
436   def find_megaraid_disks(devices)
437     controllers = []
438     arrays = []
439
440     controller = nil
441     array = nil
442     disk = nil
443
444     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
445       if line =~ /^PCI information for Controller (\d+)$/
446         controller = {
447           :id => devices[:controllers].count,
448           :type => "megaraid",
449           :arrays => [],
450           :disks => []
451         }
452
453         devices[:controllers] << controller
454
455         controllers << controller
456       elsif line =~ /^Bus Number\s+:\s+([0-9a-f]+)$/i
457         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
458       elsif line =~ /^Device Number\s+:\s+([0-9a-f]+)$/i
459         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
460       elsif line =~ /^Function Number\s+:\s+([0-9a-f]+)$/i
461         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
462       end
463     end
464
465     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
466       if line =~ /^Adapter #(\d+)$/
467         controller = controllers[Regexp.last_match(1).to_i]
468       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
469         case Regexp.last_match(1)
470         when "Product Name" then controller[:model] = Regexp.last_match(2)
471         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
472         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
473         end
474       end
475     end
476
477     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
478       if line =~ /^Adapter #(\d+)$/
479         controller = controllers[Regexp.last_match(1).to_i]
480       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
481         pci_slot = controller[:pci_slot]
482         target = Regexp.last_match(2)
483         device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
484
485         array = {
486           :id => devices[:arrays].count,
487           :controller => controller[:id],
488           :number => Regexp.last_match(1),
489           :device => "/dev/#{File.basename(device)}",
490           :disks => []
491         }
492
493         devices[:arrays] << array
494         controller[:arrays] << array[:id]
495
496         arrays << array
497
498         disk = nil
499       elsif array && line =~ /^PD: (\d+) Information$/
500         disk = {
501           :id => devices[:disks].count,
502           :controller => controller[:id],
503           :arrays => [array[:id]]
504         }
505
506         devices[:disks] << disk
507         controller[:disks] << disk[:id]
508         array[:disks] << disk[:id]
509       elsif disk && line =~ /^Firmware state:\s+(.*\S),\s*(.*\S)\s*$/
510         case Regexp.last_match(1)
511         when "Unconfigured(good)" then disk[:status] = "unconfigured"
512         when "Unconfigured(bad)" then disk[:status] = "unconfigured"
513         when "Hotspare" then disk[:status] = "hotspare"
514         when "Offline" then disk[:status] = "offline"
515         when "Online" then disk[:status] = "online"
516         when "Rebuild" then disk[:status] = "rebuilding"
517         when "Failed" then disk[:status] = "failed"
518         when "Copyback" then disk[:status] = "rebuilding"
519         else disk[:status] = "unknown"
520         end
521         case Regexp.last_match(2)
522         when "Spun Up" then disk[:state] = "spun_up"
523         when "Spun down" then disk[:state] = "spun_down"
524         else disk[:state] = "unknown"
525         end
526       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
527         case Regexp.last_match(1)
528         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
529         when "WWN" then disk[:wwn] = Regexp.last_match(2)
530         when "PD Type" then disk[:interface] = Regexp.last_match(2)
531         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
532         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
533         end
534       elsif array && line =~ /^State\s*:\s+(.*\S)\s*$/
535         case Regexp.last_match(1)
536         when "Partially Degraded" then array[:status] = "degraded"
537         when "Degraded" then array[:status] = "degraded"
538         when "Optimal" then array[:status] = "optimal"
539         when "Consistency Check" then array[:status] = "checking"
540         when "Background Initialization" then array[:status] = "initialising"
541         when "Initialization" then array[:status] = "initialising"
542         when "Reconstruction" then array[:status] = "rebuilding"
543         else array[:status] = "unknown"
544         end
545       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
546         case Regexp.last_match(1)
547         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
548         when "Size" then array[:size] = Regexp.last_match(2)
549         end
550       end
551     end
552
553     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
554       if line =~ /^Adapter #(\d+)$/
555         controller = controllers[Regexp.last_match(1).to_i]
556       elsif controller && line =~ /^Enclosure Device ID: \d+$/
557         disk = {
558           :controller => controller[:id]
559         }
560       elsif disk && line =~ /^WWN:\s+(\S+)$/
561         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
562           disk[:id] = devices[:disks].count
563           disk[:wwn] = Regexp.last_match(1)
564
565           devices[:disks] << disk
566         end
567       elsif disk && line =~ /^Firmware state:\s+(.*\S),\s*(.*\S)\s*$/
568         case Regexp.last_match(1)
569         when "Unconfigured(good)" then disk[:status] = "unconfigured"
570         when "Unconfigured(bad)" then disk[:status] = "unconfigured"
571         when "Hotspare" then disk[:status] = "hotspare"
572         when "Offline" then disk[:status] = "offline"
573         when "Online" then disk[:status] = "online"
574         when "Rebuild" then disk[:status] = "rebuilding"
575         when "Failed" then disk[:status] = "failed"
576         when "Copyback" then disk[:status] = "rebuilding"
577         else disk[:status] = "unknown"
578         end
579         case Regexp.last_match(2)
580         when "Spun Up" then disk[:state] = "spun_up"
581         when "Spun down" then disk[:state] = "spun_down"
582         else disk[:state] = "unknown"
583         end
584       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
585         case Regexp.last_match(1)
586         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
587         when "PD Type" then disk[:interface] = Regexp.last_match(2)
588         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
589         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
590         end
591       end
592     end
593
594     controllers.each do |controller|
595       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
596         controller[:device] = "/dev/#{File.basename(device)}"
597       end
598     end
599   end
600
601   def find_mpt1_disks(devices)
602     controllers = []
603     disks = []
604
605     controller = nil
606
607     IO.popen(%w(lsiutil -s)).each do |line|
608       if line =~ /^\/proc\/mpt\/ioc(\d+)\s+LSI Logic\s+(\S+)\s+/
609         controller = {
610           :id => devices[:controllers].count,
611           :type => "mpt1",
612           :model => Regexp.last_match(1),
613           :arrays => [],
614           :disks => []
615         }
616
617         controllers << controller
618         devices[:controllers] << controller
619       elsif line =~ /^\s+(\d+)\s+(\d+)\s+PhysDisk (\d+)\s+(\S+)\s+(\S+)\s+\d+\s+(\S+)\s+/
620         disks[Regexp.last_match(3).to_i] = {
621           :id => devices[:disks].count,
622           :controller => controller[:id],
623           :vendor => Regexp.last_match(4),
624           :model => Regexp.last_match(5),
625           :sas_address => Regexp.last_match(6),
626           :arrays => []
627         }
628
629         controller[:disks] << devices[:disks].count
630         devices[:disks] << disks[Regexp.last_match(3).to_i]
631       end
632     end
633
634     controllers.each_with_index do |controller, index|
635       port = index + 1
636       array = nil
637
638       IO.popen(["lsiutil", "-p", port.to_s, "-a", "69,0"]).each do |line|
639         if line =~ /^ (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+RAID/
640           seg = Regexp.last_match(1).to_i
641           bus = Regexp.last_match(2).to_i
642           dev = Regexp.last_match(3).to_i
643           fun = Regexp.last_match(4).to_i
644
645           controller[:pci_slot] = sprintf("%04x:%02x:%02x.%01x", seg, bus, dev, fun)
646         end
647       end
648
649       IO.popen(["lsiutil", "-p", port.to_s, "-a", "21,1,0,0"]).each do |line|
650         if line =~ /^Volume (\d+) is/
651           array = {
652             :id => devices[:arrays].count,
653             :controller => controller[:id],
654             :number => Regexp.last_match(1),
655             :disks => []
656           }
657
658           devices[:arrays] << array
659           controller[:arrays] << array[:id]
660         elsif line =~ /^  Member \d+ is PhysDisk (\d+) /
661           array[:disks] << disks[Regexp.last_match(1).to_i][:id]
662           disks[Regexp.last_match(1).to_i][:arrays] << array[:id]
663         end
664       end
665     end
666
667     disks.each do |disk|
668       slot = controllers[disk[:controller]][:pci_slot]
669       sas_address = "0x#{disk[:sas_address]}"
670
671       Dir.glob("/sys/bus/pci/devices/#{slot}/host*/port-*:*/end_device-*:*/sas_device/end_device-*:*").each do |sas_device|
672         if read_sysctl_file("#{sas_device}/sas_address") == sas_address
673           if device = Dir.glob("#{sas_device}/device/target*:0:*/*:0:*:0/scsi_generic/sg*").first
674             disk[:device] = "/dev/#{File.basename(device)}"            
675           end
676         end
677       end
678     end
679   end
680
681   def find_mpt2_disks(devices)
682     controllers = []
683
684     IO.popen(%w(sas2ircu list)).each do |line|
685       next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
686       controllers[Regexp.last_match(1).to_i] = {
687         :id => devices[:controllers].count,
688         :type => "mpt2",
689         :model => Regexp.last_match(2),
690         :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
691         :arrays => [],
692         :disks => []
693       }
694
695       devices[:controllers] << controllers[Regexp.last_match(1).to_i]
696     end
697
698     controllers.each_with_index do |controller, index|
699       arrays = []
700       disks = []
701
702       array = nil
703       disk = nil
704
705       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
706         if line =~ /^IR volume (\d+)$/
707           array = {
708             :id => devices[:arrays].count,
709             :controller => controller[:id],
710             :number => Regexp.last_match(1),
711             :disks => []
712           }
713
714           devices[:arrays] << array
715           controller[:arrays] << array[:id]
716
717           arrays << array
718         elsif line =~ /^Device is a Hard disk$/
719           disk = {
720             :id => devices[:disks].count,
721             :controller => controller[:id],
722             :arrays => []
723           }
724
725           devices[:disks] << disk
726           controller[:disks] << disk[:id]
727
728           disks << disk
729         elsif disk && line =~ /^  State\s+:\s+(.*\S)\s*$/
730           Regexp.last_match(1).split(/,\s*/).each do |state|
731             case state
732             when "Online (ONL)" then disk[:status] = "online"
733             when "Hot Spare (HSP)" then disk[:status] = "hotspare"
734             when "Ready (RDY)" then disk[:status] = "unconfigured"
735             when "Available (AVL)" then disk[:status] = "unconfigured"
736             when "Failed (FLD)" then disk[:status] = "failed"
737             when "Missing (MIS)" then disk[:status] = "missing"
738             when "Standby (SBY)" then disk[:status] = "unconfigured"
739             when "Out of Sync (OSY)" then disk[:status] = "degraded"
740             when "Degraded (DGD)" then disk[:status] = "degraded"
741             when "Rebuilding (RBLD)" then disk[:status] = "rebuilding"
742             when "Optimal (OPT)" then disk[:status] = "online"
743             else disk[:status] = "unknown"
744             end
745           end
746         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
747           case Regexp.last_match(1)
748           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
749           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
750           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
751           when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
752           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
753           when "Model Number" then disk[:model] = Regexp.last_match(2)
754           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
755           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
756           when "Protocol" then disk[:interface] = Regexp.last_match(2)
757           end
758         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
759           array[:disks] << Regexp.last_match(1)
760         elsif array && line =~ /^  Status of volume\s+:\s+(.*\S)\s*$/
761           Regexp.last_match(1).split(/,\s*/).each do |state|
762             case state
763             when "Okay (OKY)" then array[:status] = "optimal"
764             when "Degraded (DGD)" then array[:status] = "degraded"
765             when "Failed (FLD)" then array[:status] = "failed"
766             when "Missing (MIS)" then array[:status] = "missing"
767             when "Initializing (INIT)" then array[:status] = "initialising"
768             when "Online (ONL)" then array[:status] = "optimal"
769             else array[:status] = "unknown"
770             end
771           end
772         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
773           case Regexp.last_match(1)
774           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
775           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
776           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
777           end
778         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
779           case Regexp.last_match(1)
780           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
781           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
782           end
783         end
784       end
785
786       arrays.each do |array|
787         array[:disks].map! do |location|
788           disk = disks.find { |disk| disk[:location] == location }
789
790           disk[:arrays] << array[:id]
791           disk[:id]
792         end
793       end
794     end
795   end
796
797   def find_adaptec_disks(devices)
798     controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers Found: (\d+)$/i).first.first.to_i
799
800     1.upto(controller_count).each do |controller_number|
801       controller = {
802         :id => devices[:controllers].count,
803         :number => controller_number,
804         :type => "adaptec",
805         :arrays => [],
806         :disks => []
807       }
808
809       devices[:controllers] << controller
810
811       arrays = []
812       disks = []
813
814       array = nil
815       disk = nil
816
817       IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
818         if line =~ /^Logical Device Number (\d+)$/i
819           array = {
820             :id => devices[:arrays].count,
821             :controller => controller[:id],
822             :number => Regexp.last_match(1).to_i,
823             :disks => []
824           }
825
826           devices[:arrays] << array
827           controller[:arrays] << array[:id]
828
829           arrays << array
830         elsif line =~ /^      Device #(\d+)$/
831           disk = nil
832         elsif line =~ /^         Device is a Hard drive$/
833           disk = {
834             :id => devices[:disks].count,
835             :controller => controller[:id],
836             :arrays => []
837           }
838
839           devices[:disks] << disk
840           controller[:disks] << disk[:id]
841
842           disks << disk
843         elsif disk && line =~ /^         Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
844           disk[:channel_number] = Regexp.last_match(1)
845           disk[:device_number] = Regexp.last_match(2)
846         elsif disk && line =~ /^         State\s*:\s+(\S.*\S)\s*$/
847           case Regexp.last_match(1)
848           when "Online" then disk[:status] = "online"
849           when "Online (JBOD)" then disk[:status] = "online"
850           when "Hot Spare" then disk[:status] = "hotspare"
851           when "Ready" then disk[:status] = "unconfigured"
852           when "Global Hot-Spare" then disk[:status] = "hostspare"
853           when "Dedicated Hot-Spare" then disk[:status] = "hotspare"
854           else disk[:status] = "unknown"
855         end
856         elsif disk && line =~ /^         (\S.*\S)\s*:\s+(\S.*\S)\s*$/
857           case Regexp.last_match(1)
858           when "Reported Location" then disk[:location] = Regexp.last_match(2)
859           when "Vendor" then disk[:vendor] = Regexp.last_match(2)
860           when "Model" then disk[:model] = Regexp.last_match(2)
861           when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
862           when "Serial number" then disk[:serial_number] = Regexp.last_match(2)
863           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
864           when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
865           when "World-wide Name" then disk[:wwn] = Regexp.last_match(2)
866           when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
867           when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
868           end
869         elsif array && line =~ / Present \(.*((?:Connector|Enclosure):\d+,\s*(?:Device|Slot):\d+)\) /
870           array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(/,\s*/, ", ")
871         elsif array && line =~ /^   Status of Logical Device\s*:\s+(\S.*\S)\s*$/
872           case Regexp.last_match(1)
873           when "Optimal" then array[:status] = "optimal"
874           else array[:status] = "unknown"
875         end
876         elsif array && line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
877           case Regexp.last_match(1)
878           when "RAID level" then array[:raid_level] = Regexp.last_match(2)
879           when "RAID Level" then array[:raid_level] = Regexp.last_match(2)
880           when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
881           end
882         elsif line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
883           case Regexp.last_match(1)
884           when "Controller Model" then controller[:model] = Regexp.last_match(2)
885           when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
886           when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
887           when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
888           when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
889           end
890         elsif line =~ /^         Serial Number\s*:\s+(\S.*\S)\s*$/
891           controller[:serial_number] = Regexp.last_match(1)
892         end
893       end
894
895       host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
896         read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
897       end
898
899       arrays.each do |array|
900         array_number = array[:number]
901         device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
902
903         array[:device] = "/dev/#{File.basename(device)}"
904
905         array[:disks].map! do |location|
906           disk = disks.find { |disk| disk[:location] == location }
907
908           controller_number = controller[:number] - 1
909           device_number = disk[:device_number]
910
911           disk[:device] = "/dev/#{File.basename(device)}"
912           disk[:smart_device] = "aacraid,#{controller_number},0,#{device_number}"
913
914           disk[:arrays] << array[:id]
915           disk[:id]
916         end
917       end
918     end
919   end
920
921   def find_areca_disks(devices)
922     controller = {
923       :id => devices[:controllers].count,
924       :type => "areca",
925       :arrays => [],
926       :disks => []
927     }
928
929     devices[:controllers] << controller
930
931     IO.popen(%w(/opt/areca/x86_64/cli64 sys info)).each do |line|
932       next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
933
934       case Regexp.last_match(1)
935       when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
936       when "BOOT ROM Version" then controller[:bios_version] = Regexp.last_match(2)
937       when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
938       when "Controller Name" then controller[:model] = Regexp.last_match(2)
939       end
940     end
941
942     path = Dir.glob("/sys/bus/pci/devices/*/host*/scsi_host/host*/host_fw_model").find do |file|
943       read_sysctl_file(file) == controller[:model]
944     end
945
946     controller[:pci_slot] = File.basename(File.expand_path("#{path}/../../../.."))
947     controller[:device] = File.basename(Dir.glob(File.expand_path("#{path}/../../../target0:0:16/0:0:16:0/scsi_generic/*")).first)
948
949     arrays = []
950
951     IO.popen(%w(/opt/areca/x86_64/cli64 vsf info)).each do |line|
952       next unless line =~ /^\s+(\d+)\s+/
953       array = {
954         :id => devices[:arrays].count,
955         :number => Regexp.last_match(1),
956         :controller => controller[:id],
957         :disks => []
958       }
959
960       devices[:arrays] << array
961       controller[:arrays] << array[:id]
962
963       arrays << array
964     end
965
966     arrays.each do |array|
967       IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
968         if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
969           pci_slot = controller[:pci_slot]
970           channel = Regexp.last_match(1).to_i
971           id = Regexp.last_match(2).to_i
972           lun = Regexp.last_match(3).to_i
973
974           device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
975
976           array[:device] = "/dev/#{File.basename(device)}"
977         elsif line =~ /^Volume State\s+:\s+(.*\S)\s*$/
978           case Regexp.last_match(1)
979           when "Normal" then array[:status] = "optimal"
980           else array[:status] = "unknown"
981           end
982         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
983           case Regexp.last_match(1)
984           when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
985           when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
986           when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
987           when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
988           end
989         end
990       end
991     end
992
993     disks = []
994
995     IO.popen(%w(/opt/areca/x86_64/cli64 disk info)).each do |line|
996       next unless line =~ /^\s+(\d+)\s+.*\s+\d+\.\d+GB\s+(\S.*\S)\s*$/
997       next if Regexp.last_match(2) == "N.A."
998
999       disk = {
1000         :id => devices[:disks].count,
1001         :number => Regexp.last_match(1),
1002         :controller => controller[:id],
1003         :arrays => []
1004       }
1005
1006       devices[:disks] << disk
1007       controller[:disks] << disk[:id]
1008
1009       if array = arrays.find { |array| array[:raid_set] == Regexp.last_match(2) }
1010         disk[:arrays] << array[:id]
1011         array[:disks] << disk[:id]
1012       end
1013
1014       disks << disk
1015     end
1016
1017     disks.each do |disk|
1018       IO.popen(["/opt/areca/x86_64/cli64", "disk", "info", "drv=#{disk[:number]}"]).each do |line|
1019         if line =~ /^IDE Channel\s+:\s+(\d+)\s*$/i
1020           disk[:smart_device] = "areca,#{Regexp.last_match(1)}"
1021         elsif line =~ /^Device Location\s+:\s+Enclosure#(\d+) Slot#?\s*0*(\d+)\s*$/i
1022           disk[:smart_device] = "areca,#{Regexp.last_match(2)}/#{Regexp.last_match(1)}"
1023         elsif line =~ /^Device State\s+:\s+(.*\S)\s*$/
1024           case Regexp.last_match(1)
1025           when "NORMAL" then disk[:status] = "online"
1026           else disk[:status] = "unknown"
1027           end
1028         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
1029           case Regexp.last_match(1)
1030           when "Model Name" then disk[:vendor], disk[:model] = Regexp.last_match(2).split
1031           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
1032           when "Disk Capacity" then disk[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
1033           end
1034         end
1035       end
1036     end
1037   end
1038
1039   def lvm_devices
1040     {
1041       :pvs => find_lvm_pvs,
1042       :vgs => find_lvm_vgs,
1043       :lvs => find_lvm_lvs
1044     }
1045   end
1046
1047   def find_lvm_pvs
1048     IO.popen(["pvdisplay", "-c"]).each_with_object({}) do |line, pvs|
1049       fields = line.strip.split(":")
1050
1051       pvs[fields[0]] = {
1052         :vg => fields[1],
1053         :pv_size => fields[2],
1054         :pv_status => fields[4],
1055         :pe_size => fields[7],
1056         :pe_total => fields[8],
1057         :pe_free => fields[9],
1058         :pe_allocated => fields[10],
1059         :pv_uuid => fields[11]
1060       }
1061     end
1062   end
1063
1064   def find_lvm_vgs
1065     IO.popen(["vgdisplay", "-c"]).each_with_object({}) do |line, vgs|
1066       fields = line.strip.split(":")
1067
1068       vgs[fields[0]] = {
1069         :vg_access => fields[1],
1070         :vg_status => fields[2],
1071         :lv_maximum => fields[4],
1072         :lv_count => fields[5],
1073         :lv_open => fields[6],
1074         :pv_maximum => fields[8],
1075         :pv_current => fields[9],
1076         :pv_actual => fields[10],
1077         :vg_size => fields[11],
1078         :pe_size => fields[12],
1079         :pe_total => fields[13],
1080         :pe_allocated => fields[14],
1081         :pe_free => fields[15],
1082         :vg_uuid => fields[16]
1083       }
1084     end
1085   end
1086
1087   def find_lvm_lvs
1088     IO.popen(["lvdisplay", "-c"]).each_with_object({}) do |line, lvs|
1089       fields = line.strip.split(":")
1090
1091       lvs[fields[0]] = {
1092         :vg => fields[1],
1093         :lv_access => fields[2],
1094         :lv_status => fields[3],
1095         :lv_open => fields[5],
1096         :lv_size => fields[6],
1097         :le_count => fields[7],
1098         :lv_minor => fields[11],
1099         :lv_major => fields[12]
1100       }
1101     end
1102   end
1103
1104   def psu_devices
1105     device = nil
1106
1107     IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
1108       if line =~ /^System Power Supply\s*$/
1109         device = {}
1110       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
1111         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
1112       elsif device && line =~ /^\s*$/
1113         devices << device
1114         device = nil
1115       end
1116     end
1117   end
1118
1119   def mc_device
1120     device = {}
1121
1122     IO.popen(["ipmitool", "mc", "info"]).each_with_object([]) do |line, devices|
1123       if line =~ /(Manufacturer [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1124         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1125       elsif line =~ /(Product [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
1126         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1127       elsif line =~ /([A-Z ]+[A-Z])\s*:\s+(.*\S)\s*$/i
1128         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
1129       end
1130     end
1131
1132     IO.popen(["ipmitool", "mc", "guid"]).each_with_object([]) do |line, devices|
1133       if line =~ /^System GUID\s*:\s+(\S+)\s*$/
1134         device[:system_guid] = Regexp.last_match(1)
1135       end
1136     end
1137
1138     device
1139   end
1140
1141   collect_data(:default) do
1142     hardware Mash.new
1143
1144     hardware[:pci] = pci_devices
1145     hardware[:network] = network_devices
1146     hardware[:memory] = memory_devices
1147     hardware[:disk] = disk_devices
1148     hardware[:lvm] = lvm_devices
1149     hardware[:psu] = psu_devices
1150     hardware[:mc] = mc_device
1151   end
1152 end