]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Update Intel Mass Storage tool
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
1 require "pathname"
2
3 Ohai.plugin(:Hardware) do
4   provides "hardware"
5
6   def read_sysctl_link(file)
7     File.basename(File.readlink(file))
8   rescue Errno::ENOENT, Errno::ENOTDIR
9   end
10
11   def read_sysctl_file(file)
12     IO.read(file).strip
13   rescue Errno::ENOENT, Errno::ENOTDIR, Errno::EINVAL
14   end
15
16   def parse_memory_size(size)
17     if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
18       Regexp.last_match(1).to_f * 2**30
19     elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
20       Regexp.last_match(1).to_f * 2**20
21     elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
22       Regexp.last_match(1).to_f * 2**10
23     end
24   end
25
26   def format_disk_size(kb)
27     if kb == 0
28       ""
29     else
30       kblog10 = Math.log10(kb).floor
31
32       kb = kb.to_f * 2 / 10**kblog10
33       kb = kb.round.to_f / 2
34
35       if kblog10 >= 9
36         format "%gTB", kb * 10**(kblog10 - 9)
37       elsif kblog10 >= 6
38         format "%dGB", kb * 10**(kblog10 - 6)
39       else
40         format "%dMB", kb * 10**(kblog10 - 3)
41       end
42     end
43   end
44
45   def memory_to_disk_size(size)
46     format_disk_size(parse_memory_size(size))
47   end
48
49   def find_sas_device(address)
50     file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
51       read_sysctl_file(file) == "0x#{address}"
52     end
53
54     if file
55       dir = File.dirname(file)
56       device = Dir.glob("#{dir}/block/*").first ||
57                Dir.glob("#{dir}/scsi_generic/*").first
58
59       "/dev/#{File.basename(device)}"
60     end
61   end
62
63   def pci_devices
64     devices = {}
65     device = nil
66
67     IO.popen(["lspci", "-Dkvmm"]).each do |line|
68       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
69         device = {
70           :slot => Regexp.last_match(1),
71           :domain => Regexp.last_match(2),
72           :bus => Regexp.last_match(3),
73           :device => Regexp.last_match(4),
74           :function => Regexp.last_match(5)
75         }
76       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
77         case Regexp.last_match(1)
78         when "Class" then device[:class_name] = Regexp.last_match(2)
79         when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
80         when "Device" then device[:device_name] = Regexp.last_match(2)
81         when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
82         when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
83         when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
84         when "Rev" then device[:revision] = Regexp.last_match(2)
85         when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
86         when "Driver" then device[:driver] = Regexp.last_match(2)
87         when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
88         end
89       elsif device && line =~ /^\s*$/
90         devices[device[:slot]] = device
91         device = nil
92       end
93     end
94
95     IO.popen(["lspci", "-Dkvmmn"]).each do |line|
96       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
97         device = devices[Regexp.last_match(1)]
98       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
99         case Regexp.last_match(1)
100         when "Class" then device[:class_id] = Regexp.last_match(2)
101         when "Vendor" then device[:vendor_id] = Regexp.last_match(2)
102         when "Device" then device[:device_id] = Regexp.last_match(2)
103         when "SVendor" then device[:subsystem_vendor_id] = Regexp.last_match(2)
104         when "SDevice" then device[:subsystem_device_id] = Regexp.last_match(2)
105         end
106       elsif device && line =~ /^\s*$/
107         device = nil
108       end
109     end
110
111     devices
112   end
113
114   def network_devices
115     Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
116       name = File.basename(device)
117
118       devices[name] = {
119         :device => read_sysctl_link("#{device}/device"),
120         :duplex => read_sysctl_file("#{device}/duplex"),
121         :speed => read_sysctl_file("#{device}/speed")
122       }.delete_if { |_, v| v.nil? }
123     end
124   end
125
126   def memory_devices
127     device = nil
128
129     IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
130       if line =~ /^Memory Device\s*$/
131         device = {}
132       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
133         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
134       elsif device && line =~ /^\s*$/
135         devices << device
136         device = nil
137       end
138     end
139   end
140
141   def disk_devices
142     disk = Mash.new
143
144     disk[:controllers] = []
145     disk[:arrays] = []
146     disk[:disks] = []
147
148     find_direct_disks(disk)
149     find_nvme_disks(disk)
150
151     find_hp_disks(disk) if File.exist?("/usr/sbin/ssacli")
152     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
153     find_mpt1_disks(disk) if File.exist?("/usr/sbin/lsiutil")
154     find_mpt2_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
155     find_adaptec_disks(disk) if File.exist?("/usr/sbin/arcconf")
156     find_areca_disks(disk) if File.exist?("/opt/areca/x86_64/cli64")
157
158     find_md_arrays(disk)
159
160     disk[:disks].each do |disk|
161       if disk[:vendor] =~ /^(BTWA|CVPR|PHDV)/ && disk[:model] == "INTEL"
162         disk[:model] = disk[:serial_number]
163         disk[:serial_number] = disk[:vendor]
164         disk[:vendor] = "INTEL"
165       end
166
167       if disk[:vendor].nil? && disk[:model] =~ /^ATA\s+(.*)$/
168         disk[:vendor] = "ATA"
169         disk[:model] = Regexp.last_match(1)
170       end
171
172       if disk[:vendor].nil? || disk[:vendor] == "ATA"
173         if disk[:model] =~ /^(\S+)\s+(.*)$/
174           disk[:vendor] = Regexp.last_match(1)
175           disk[:model] = Regexp.last_match(2)
176         elsif disk[:model] =~ /^ST/
177           disk[:vendor] = "SEAGATE"
178         elsif disk[:model] =~ /^C300-(.*)$/
179           disk[:vendor] = "CRUCIAL"
180           disk[:model] = Regexp.last_match(1)
181         end
182       end
183
184       disk[:model].sub!(/-.*$/, "") if disk[:model]
185     end
186
187     disk
188   end
189
190   def find_direct_disks(devices)
191     Dir.glob("/sys/class/scsi_host/host*") do |host|
192       driver = read_sysctl_file("#{host}/proc_name")
193
194       if %w(ahci mptsas sata_mv sata_nv).include?(driver)
195         bus = host.sub("/sys/class/scsi_host/host", "")
196
197         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
198           next unless File.exist?("#{device}/scsi_disk")
199
200           block = Dir.glob("#{device}/block/*").first
201           size = read_sysctl_file("#{block}/size").to_f / 2
202
203           devices[:disks] << {
204             :id => devices[:disks].count,
205             :device => "/dev/#{File.basename(block)}",
206             :vendor => read_sysctl_file("#{device}/vendor"),
207             :model => read_sysctl_file("#{device}/model"),
208             :firmware_version => read_sysctl_file("#{device}/rev"),
209             :size => format_disk_size(size),
210             :arrays => []
211           }
212         end
213       end
214     end
215   end
216
217   def find_nvme_disks(devices)
218     Dir.glob("/sys/class/nvme/nvme*") do |device|
219       controller = {
220         :id => devices[:controllers].count,
221         :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
222         :arrays => [],
223         :disks => []
224       }
225
226       devices[:controllers] << controller
227
228       IO.popen(["lspci", "-Dkvmm", "-s", controller[:pci_slot]]).each do |line|
229         if line =~ /^SVendor:\s+(\S.*\S)\s*$/
230           controller[:vendor] = Regexp.last_match(1)
231         elsif line =~ /^SDevice:\s+(\S.*\S)\s*$/
232           controller[:model] = Regexp.last_match(1)
233         end
234       end
235
236       Dir.glob("#{device}/nvme*").each do |block|
237         size = read_sysctl_file("#{block}/size").to_f / 2
238
239         disk = {
240           :id => devices[:disks].count,
241           :controller => controller[:id],
242           :device => "/dev/#{File.basename(block)}",
243           :vendor => controller[:vendor],
244           :model => controller[:model],
245           :size => format_disk_size(size),
246           :arrays => []
247         }
248
249         devices[:disks] << disk
250         controller[:disks] << disk[:id]
251       end
252     end
253   end
254
255   def find_md_arrays(devices)
256     array = nil
257
258     File.new("/proc/mdstat", "r").each do |line|
259       if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*)+)$/
260         array = {
261           :id => devices[:arrays].count,
262           :device => "/dev/#{Regexp.last_match(1)}",
263           :raid_level => Regexp.last_match(2),
264           :disks => []
265         }
266
267         Regexp.last_match(3).scan(/ (sd[a-z]+|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
268           if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
269             disk[:arrays] << array[:id]
270             array[:disks] << disk[:id]
271           end
272         end
273
274         devices[:arrays] << array
275       elsif array && line =~ /^\s+(\d+) blocks/
276         array[:size] = format_disk_size(Regexp.last_match(1).to_i)
277       end
278     end
279   end
280
281   def find_hp_disks(devices)
282     controllers = []
283     disks = []
284
285     controller = nil
286     array = nil
287     disk = nil
288
289     IO.popen(%w(ssacli controller all show config detail)).each do |line|
290       if line =~ /^Smart (?:Array|HBA) (\S+) /
291         controller = {
292           :id => devices[:controllers].count,
293           :model => Regexp.last_match(1),
294           :arrays => [],
295           :disks => []
296         }
297
298         devices[:controllers] << controller
299
300         controllers << controller
301
302         array = nil
303         disk = nil
304       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
305         case Regexp.last_match(1)
306         when "Slot" then controller[:slot] = Regexp.last_match(2)
307         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
308         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
309         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
310         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
311         end
312       elsif controller && line =~ /^      Logical Drive: (\d+)$/
313         array = {
314           :id => devices[:arrays].count,
315           :controller => controller[:id],
316           :number => Regexp.last_match(1),
317           :disks => []
318         }
319
320         devices[:arrays] << array
321         controller[:arrays] << array[:id]
322
323         disk = nil
324       elsif array && line =~ /^      physicaldrive (\S+)$/
325         disk = {
326           :id => devices[:disks].count,
327           :controller => controller[:id],
328           :arrays => [array[:id]],
329           :location => Regexp.last_match(1)
330         }
331
332         devices[:disks] << disk
333         controller[:disks] << disk[:id]
334         array[:disks] << disk[:id]
335       elsif disk && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
336         case Regexp.last_match(1)
337         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
338         when "Size" then disk[:size] = Regexp.last_match(2)
339         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
340         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
341         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
342         when "Model" then disk[:model] = Regexp.last_match(2)
343         end
344       elsif array && line =~ /^         (\S[^:]+):\s+(.*\S)\s*$/
345         case Regexp.last_match(1)
346         when "Size" then array[:size] = Regexp.last_match(2)
347         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
348         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
349         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
350         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
351         end
352       end
353     end
354
355     controllers.each do |controller|
356       slot = controller[:slot]
357
358       IO.popen(%W(ssacli controller slot=#{slot} pd all show status)).each do |line|
359         if line =~ /^   physicaldrive (\S+) /
360           disks << Regexp.last_match(1)
361         end
362       end
363
364       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
365         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
366       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:3:0/*:3:0:0/scsi_generic/sg*").first
367         controller[:device] = "/dev/#{File.basename(device)}"
368       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:1:0/*:1:0:*/scsi_generic/sg*").first
369         controller[:device] = "/dev/#{File.basename(device)}"
370       end
371     end
372
373     devices[:disks].each do |disk|
374      disk[:smart_device] = "cciss,#{disks.find_index(disk[:location])}"
375     end
376   end
377
378   def find_megaraid_disks(devices)
379     controllers = []
380     arrays = []
381
382     controller = nil
383     array = nil
384     disk = nil
385
386     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
387       if line =~ /^PCI information for Controller (\d+)$/
388         controller = {
389           :id => devices[:controllers].count,
390           :arrays => [],
391           :disks => []
392         }
393
394         devices[:controllers] << controller
395
396         controllers << controller
397       elsif line =~ /^Bus Number\s+:\s+(\d+)$/
398         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
399       elsif line =~ /^Device Number\s+:\s+(\d+)$/
400         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
401       elsif line =~ /^Function Number\s+:\s+(\d+)$/
402         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
403       end
404     end
405
406     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
407       if line =~ /^Adapter #(\d+)$/
408         controller = controllers[Regexp.last_match(1).to_i]
409       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
410         case Regexp.last_match(1)
411         when "Product Name" then controller[:model] = Regexp.last_match(2)
412         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
413         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
414         end
415       end
416     end
417
418     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
419       if line =~ /^Adapter #(\d+)$/
420         controller = controllers[Regexp.last_match(1).to_i]
421       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
422         pci_slot = controller[:pci_slot]
423         target = Regexp.last_match(2)
424         device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
425
426         array = {
427           :id => devices[:arrays].count,
428           :controller => controller[:id],
429           :number => Regexp.last_match(1),
430           :device => "/dev/#{File.basename(device)}",
431           :disks => []
432         }
433
434         devices[:arrays] << array
435         controller[:arrays] << array[:id]
436
437         arrays << array
438
439         disk = nil
440       elsif array && line =~ /^PD: (\d+) Information$/
441         disk = {
442           :id => devices[:disks].count,
443           :controller => controller[:id],
444           :arrays => [array[:id]]
445         }
446
447         devices[:disks] << disk
448         controller[:disks] << disk[:id]
449         array[:disks] << disk[:id]
450       elsif disk && line =~ /^Firmware state:\s+(.*\S)\s*$/
451         Regexp.last_match(1).split(/,\s*/).each do |state|
452           case state
453           when "Unconfigured(bad)" then disk[:status] = "unconfigured"
454           when "Online" then disk[:status] = "online"
455           when "Hotspare" then disk[:status] = "hotspare"
456           when "Failed" then disk[:status] = "failed"
457           when "Spun Up" then disk[:state] = "spun_up"
458           when "Spun down" then disk[:state] = "spun_down"
459           end
460         end
461       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
462         case Regexp.last_match(1)
463         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
464         when "WWN" then disk[:wwn] = Regexp.last_match(2)
465         when "PD Type" then disk[:interface] = Regexp.last_match(2)
466         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
467         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
468         end
469       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
470         case Regexp.last_match(1)
471         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
472         when "Size" then array[:size] = Regexp.last_match(2)
473         end
474       end
475     end
476
477     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
478       if line =~ /^Adapter #(\d+)$/
479         controller = controllers[Regexp.last_match(1).to_i]
480       elsif controller && line =~ /^Enclosure Device ID: \d+$/
481         disk = {
482           :controller => controller[:id]
483         }
484       elsif disk && line =~ /^WWN:\s+(\S+)$/
485         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
486           disk[:id] = devices[:disks].count
487           disk[:wwn] = Regexp.last_match(1)
488
489           devices[:disks] << disk
490         end
491       elsif disk && line =~ /^Firmware state:\s+(.*\S)\s*$/
492         Regexp.last_match(1).split(/,\s*/).each do |state|
493           case state
494           when "Unconfigured(bad)" then disk[:status] = "unconfigured"
495           when "Online" then disk[:status] = "online"
496           when "Hotspare" then disk[:status] = "hotspare"
497           when "Failed" then disk[:status] = "failed"
498           when "Spun Up" then disk[:state] = "spun_up"
499           when "Spun down" then disk[:state] = "spun_down"
500           end
501         end
502       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
503         case Regexp.last_match(1)
504         when "Device Id" then disk[:smart_device] = "megaraid,#{Regexp.last_match(2)}"
505         when "PD Type" then disk[:interface] = Regexp.last_match(2)
506         when "Raw Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
507         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial_number] = Regexp.last_match(2).split
508         end
509       end
510     end
511
512     controllers.each do |controller|
513       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
514         controller[:device] = "/dev/#{File.basename(device)}"
515       end
516     end
517   end
518
519   def find_mpt1_disks(devices)
520     controllers = []
521     disks = []
522
523     controller = nil
524
525     IO.popen(%w(lsiutil -s)).each do |line|
526       if line =~ /^\/proc\/mpt\/ioc(\d+)\s+LSI Logic\s+(\S+)\s+/
527         controller = {
528           :id => devices[:controllers].count,
529           :model => Regexp.last_match(1),
530           :arrays => [],
531           :disks => []
532         }
533
534         controllers << controller
535         devices[:controllers] << controller
536       elsif line =~ /^\s+(\d+)\s+(\d+)\s+PhysDisk (\d+)\s+(\S+)\s+(\S+)\s+\d+\s+(\S+)\s+/
537         disks[Regexp.last_match(3).to_i] = {
538           :id => devices[:disks].count,
539           :controller => controller[:id],
540           :vendor => Regexp.last_match(4),
541           :model => Regexp.last_match(5),
542           :sas_address => Regexp.last_match(6),
543           :arrays => []
544         }
545
546         controller[:disks] << devices[:disks].count
547         devices[:disks] << disks[Regexp.last_match(3).to_i]
548       end
549     end
550
551     controllers.each_with_index do |controller, index|
552       port = index + 1
553       array = nil
554
555       IO.popen(["lsiutil", "-p", port.to_s, "-a", "69,0"]).each do |line|
556         if line =~ /^ (\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+RAID/
557           seg = Regexp.last_match(1).to_i
558           bus = Regexp.last_match(2).to_i
559           dev = Regexp.last_match(3).to_i
560           fun = Regexp.last_match(4).to_i
561
562           controller[:pci_slot] = sprintf("%04x:%02x:%02x.%01x", seg, bus, dev, fun)
563         end
564       end
565
566       IO.popen(["lsiutil", "-p", port.to_s, "-a", "21,1,0,0"]).each do |line|
567         if line =~ /^Volume (\d+) is/
568           array = {
569             :id => devices[:arrays].count,
570             :controller => controller[:id],
571             :number => Regexp.last_match(1),
572             :disks => []
573           }
574
575           devices[:arrays] << array
576           controller[:arrays] << array[:id]
577         elsif line =~ /^  Member \d+ is PhysDisk (\d+) /
578           array[:disks] << disks[Regexp.last_match(1).to_i][:id]
579           disks[Regexp.last_match(1).to_i][:arrays] << array[:id]
580         end
581       end
582     end
583
584     disks.each do |disk|
585       slot = controllers[disk[:controller]][:pci_slot]
586       sas_address = "0x#{disk[:sas_address]}"
587
588       Dir.glob("/sys/bus/pci/devices/#{slot}/host*/port-*:*/end_device-*:*/sas_device/end_device-*:*").each do |sas_device|
589         if read_sysctl_file("#{sas_device}/sas_address") == sas_address
590           if device = Dir.glob("#{sas_device}/device/target*:0:*/*:0:*:0/scsi_generic/sg*").first
591             disk[:device] = "/dev/#{File.basename(device)}"            
592           end
593         end
594       end
595     end
596   end
597
598   def find_mpt2_disks(devices)
599     controllers = []
600
601     IO.popen(%w(sas2ircu list)).each do |line|
602       next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
603       controllers[Regexp.last_match(1).to_i] = {
604         :id => devices[:controllers].count,
605         :model => Regexp.last_match(2),
606         :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
607         :arrays => [],
608         :disks => []
609       }
610
611       devices[:controllers] << controllers[Regexp.last_match(1).to_i]
612     end
613
614     controllers.each_with_index do |controller, index|
615       arrays = []
616       disks = []
617
618       array = nil
619       disk = nil
620
621       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
622         if line =~ /^IR volume (\d+)$/
623           array = {
624             :id => devices[:arrays].count,
625             :controller => controller[:id],
626             :number => Regexp.last_match(1),
627             :disks => []
628           }
629
630           devices[:arrays] << array
631           controller[:arrays] << array[:id]
632
633           arrays << array
634         elsif line =~ /^Device is a Hard disk$/
635           disk = {
636             :id => devices[:disks].count,
637             :controller => controller[:id],
638             :arrays => []
639           }
640
641           devices[:disks] << disk
642           controller[:disks] << disk[:id]
643
644           disks << disk
645         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
646           case Regexp.last_match(1)
647           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
648           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
649           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
650           when "Size (in MB)/(in sectors)" then disk[:size] = memory_to_disk_size("#{Regexp.last_match(2).split('/').first} MB")
651           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
652           when "Model Number" then disk[:model] = Regexp.last_match(2)
653           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
654           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
655           when "Protocol" then disk[:interface] = Regexp.last_match(2)
656           end
657         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
658           array[:disks] << Regexp.last_match(1)
659         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
660           case Regexp.last_match(1)
661           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
662           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
663           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
664           end
665         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
666           case Regexp.last_match(1)
667           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
668           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
669           end
670         end
671       end
672
673       arrays.each do |array|
674         array[:disks].map! do |location|
675           disk = disks.find { |disk| disk[:location] == location }
676
677           disk[:arrays] << array[:id]
678           disk[:id]
679         end
680       end
681     end
682   end
683
684   def find_adaptec_disks(devices)
685     controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers Found: (\d+)$/i).first.first.to_i
686
687     1.upto(controller_count).each do |controller_number|
688       controller = {
689         :id => devices[:controllers].count,
690         :number => controller_number,
691         :arrays => [],
692         :disks => []
693       }
694
695       devices[:controllers] << controller
696
697       arrays = []
698       disks = []
699
700       array = nil
701       disk = nil
702
703       IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
704         if line =~ /^Logical Device Number (\d+)$/i
705           array = {
706             :id => devices[:arrays].count,
707             :controller => controller[:id],
708             :number => Regexp.last_match(1).to_i,
709             :disks => []
710           }
711
712           devices[:arrays] << array
713           controller[:arrays] << array[:id]
714
715           arrays << array
716         elsif line =~ /^      Device #(\d+)$/
717           disk = nil
718         elsif line =~ /^         Device is a Hard drive$/
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 =~ /^         Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
730           disk[:channel_number] = Regexp.last_match(1)
731           disk[:device_number] = Regexp.last_match(2)
732         elsif disk && line =~ /^         (\S.*\S)\s*:\s+(\S.*\S)\s*$/
733           case Regexp.last_match(1)
734           when "Reported Location" then disk[:location] = Regexp.last_match(2)
735           when "Vendor" then disk[:vendor] = Regexp.last_match(2)
736           when "Model" then disk[:model] = Regexp.last_match(2)
737           when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
738           when "Serial number" then disk[:serial_number] = Regexp.last_match(2)
739           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
740           when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
741           when "World-wide Name" then disk[:wwn] = Regexp.last_match(2)
742           when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
743           when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
744           end
745         elsif array && line =~ / Present \(.*((?:Connector|Enclosure):\d+,\s*(?:Device|Slot):\d+)\) /
746           array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(/,\s*/, ", ")
747         elsif array && line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
748           case Regexp.last_match(1)
749           when "RAID level" then array[:raid_level] = Regexp.last_match(2)
750           when "RAID Level" then array[:raid_level] = Regexp.last_match(2)
751           when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
752           end
753         elsif line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
754           case Regexp.last_match(1)
755           when "Controller Model" then controller[:model] = Regexp.last_match(2)
756           when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
757           when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
758           when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
759           when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
760           end
761         elsif line =~ /^         Serial Number\s*:\s+(\S.*\S)\s*$/
762           controller[:serial_number] = Regexp.last_match(1)
763         end
764       end
765
766       host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
767         read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
768       end
769
770       arrays.each do |array|
771         array_number = array[:number]
772         device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
773
774         array[:device] = "/dev/#{File.basename(device)}"
775
776         array[:disks].map! do |location|
777           disk = disks.find { |disk| disk[:location] == location }
778
779           controller_number = controller[:number] - 1
780           device_number = disk[:device_number]
781
782           disk[:device] = "/dev/#{File.basename(device)}"
783           disk[:smart_device] = "aacraid,#{controller_number},0,#{device_number}"
784
785           disk[:arrays] << array[:id]
786           disk[:id]
787         end
788       end
789     end
790   end
791
792   def find_areca_disks(devices)
793     controller = {
794       :id => devices[:controllers].count,
795       :arrays => [],
796       :disks => []
797     }
798
799     devices[:controllers] << controller
800
801     IO.popen(%w(/opt/areca/x86_64/cli64 sys info)).each do |line|
802       next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
803
804       case Regexp.last_match(1)
805       when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
806       when "BOOT ROM Version" then controller[:bios_version] = Regexp.last_match(2)
807       when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
808       when "Controller Name" then controller[:model] = Regexp.last_match(2)
809       end
810     end
811
812     path = Dir.glob("/sys/bus/pci/devices/*/host*/scsi_host/host*/host_fw_model").find do |file|
813       read_sysctl_file(file) == controller[:model]
814     end
815
816     controller[:pci_slot] = File.basename(File.expand_path("#{path}/../../../.."))
817     controller[:device] = File.basename(Dir.glob(File.expand_path("#{path}/../../../target0:0:16/0:0:16:0/scsi_generic/*")).first)
818
819     arrays = []
820
821     IO.popen(%w(/opt/areca/x86_64/cli64 vsf info)).each do |line|
822       next unless line =~ /^\s+(\d+)\s+/
823       array = {
824         :id => devices[:arrays].count,
825         :number => Regexp.last_match(1),
826         :controller => controller[:id],
827         :disks => []
828       }
829
830       devices[:arrays] << array
831       controller[:arrays] << array[:id]
832
833       arrays << array
834     end
835
836     arrays.each do |array|
837       IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
838         if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
839           pci_slot = controller[:pci_slot]
840           channel = Regexp.last_match(1).to_i
841           id = Regexp.last_match(2).to_i
842           lun = Regexp.last_match(3).to_i
843
844           device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
845
846           array[:device] = "/dev/#{File.basename(device)}"
847         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
848           case Regexp.last_match(1)
849           when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
850           when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
851           when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
852           when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
853           end
854         end
855       end
856     end
857
858     disks = []
859
860     IO.popen(%w(/opt/areca/x86_64/cli64 disk info)).each do |line|
861       next unless line =~ /^\s+(\d+)\s+.*\s+\d+\.\d+GB\s+(\S.*\S)\s*$/
862       next if Regexp.last_match(2) == "N.A."
863
864       disk = {
865         :id => devices[:disks].count,
866         :number => Regexp.last_match(1),
867         :controller => controller[:id],
868         :arrays => []
869       }
870
871       devices[:disks] << disk
872       controller[:disks] << disk[:id]
873
874       if array = arrays.find { |array| array[:raid_set] == Regexp.last_match(2) }
875         disk[:arrays] << array[:id]
876         array[:disks] << disk[:id]
877       end
878
879       disks << disk
880     end
881
882     disks.each do |disk|
883       IO.popen(["/opt/areca/x86_64/cli64", "disk", "info", "drv=#{disk[:number]}"]).each do |line|
884         if line =~ /^IDE Channel\s+:\s+(\d+)\s*$/i
885           disk[:smart_device] = "areca,#{Regexp.last_match(1)}"
886         elsif line =~ /^Device Location\s+:\s+Enclosure#(\d+) Slot#?\s*0*(\d+)\s*$/i
887           disk[:smart_device] = "areca,#{Regexp.last_match(2)}/#{Regexp.last_match(1)}"
888         elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
889           case Regexp.last_match(1)
890           when "Model Name" then disk[:vendor], disk[:model] = Regexp.last_match(2).split
891           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
892           when "Disk Capacity" then disk[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
893           end
894         end
895       end
896     end
897   end
898
899   def lvm_devices
900     {
901       :pvs => find_lvm_pvs,
902       :vgs => find_lvm_vgs,
903       :lvs => find_lvm_lvs
904     }
905   end
906
907   def find_lvm_pvs
908     IO.popen(["pvdisplay", "-c"]).each_with_object({}) do |line, pvs|
909       fields = line.strip.split(":")
910
911       pvs[fields[0]] = {
912         :vg => fields[1],
913         :pv_size => fields[2],
914         :pv_status => fields[4],
915         :pe_size => fields[7],
916         :pe_total => fields[8],
917         :pe_free => fields[9],
918         :pe_allocated => fields[10],
919         :pv_uuid => fields[11]
920       }
921     end
922   end
923
924   def find_lvm_vgs
925     IO.popen(["vgdisplay", "-c"]).each_with_object({}) do |line, vgs|
926       fields = line.strip.split(":")
927
928       vgs[fields[0]] = {
929         :vg_access => fields[1],
930         :vg_status => fields[2],
931         :lv_maximum => fields[4],
932         :lv_count => fields[5],
933         :lv_open => fields[6],
934         :pv_maximum => fields[8],
935         :pv_current => fields[9],
936         :pv_actual => fields[10],
937         :vg_size => fields[11],
938         :pe_size => fields[12],
939         :pe_total => fields[13],
940         :pe_allocated => fields[14],
941         :pe_free => fields[15],
942         :vg_uuid => fields[16]
943       }
944     end
945   end
946
947   def find_lvm_lvs
948     IO.popen(["lvdisplay", "-c"]).each_with_object({}) do |line, lvs|
949       fields = line.strip.split(":")
950
951       lvs[fields[0]] = {
952         :vg => fields[1],
953         :lv_access => fields[2],
954         :lv_status => fields[3],
955         :lv_open => fields[5],
956         :lv_size => fields[6],
957         :le_count => fields[7],
958         :lv_minor => fields[11],
959         :lv_major => fields[12]
960       }
961     end
962   end
963
964   def psu_devices
965     device = nil
966
967     IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
968       if line =~ /^System Power Supply\s*$/
969         device = {}
970       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
971         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
972       elsif device && line =~ /^\s*$/
973         devices << device
974         device = nil
975       end
976     end
977   end
978
979   def mc_device
980     device = {}
981
982     IO.popen(["ipmitool", "mc", "info"]).each_with_object([]) do |line, devices|
983       if line =~ /(Manufacturer [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
984         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
985       elsif line =~ /(Product [A-Z ]+[A-Z])\s*:\s+(.*\S)\s+\(.*\)\s*$/i
986         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
987       elsif line =~ /([A-Z ]+[A-Z])\s*:\s+(.*\S)\s*$/i
988         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2)
989       end
990     end
991
992     IO.popen(["ipmitool", "mc", "guid"]).each_with_object([]) do |line, devices|
993       if line =~ /^System GUID\s*:\s+(\S+)\s*$/
994         device[:system_guid] = Regexp.last_match(1)
995       end
996     end
997
998     device
999   end
1000
1001   collect_data(:default) do
1002     hardware Mash.new
1003
1004     hardware[:pci] = pci_devices
1005     hardware[:network] = network_devices
1006     hardware[:memory] = memory_devices
1007     hardware[:disk] = disk_devices
1008     hardware[:lvm] = lvm_devices
1009     hardware[:psu] = psu_devices
1010     hardware[:mc] = mc_device
1011   end
1012 end