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