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