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