]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Fix rubocopy warnings
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
1 Ohai.plugin(:Hardware) do
2   provides "hardware"
3
4   def read_sysctl_link(file)
5     File.basename(File.readlink(file))
6   rescue Errno::ENOENT
7   end
8
9   def read_sysctl_file(file)
10     IO.read(file).strip
11   rescue Errno::ENOENT, Errno::EINVAL
12   end
13
14   def parse_disk_size(size)
15     if size =~ /^(\d+(?:\.\d+)?)\s*TB/i
16       format "%dTB", Regexp.last_match(1).to_f * 2**40 / 1_000_000_000_000
17     elsif size =~ /^(\d+(?:\.\d+)?)\s*GB/i
18       format "%dGB", Regexp.last_match(1).to_f * 2**30 / 1000000000
19     elsif size =~ /^(\d+(?:\.\d+)?)\s*MB/i
20       format "%dGB", Regexp.last_match(1).to_f * 2**20 / 1000000000
21     end
22   end
23
24   def find_sas_device(address)
25     file = Dir.glob("/sys/class/scsi_generic/sg*/device/sas_address").find do |file|
26       read_sysctl_file(file) == "0x#{address}"
27     end
28
29     if file
30       dir = File.dirname(file)
31       device = Dir.glob("#{dir}/block/*").first ||
32                Dir.glob("#{dir}/scsi_generic/*").first
33
34       "/dev/#{File.basename(device)}"
35     end
36   end
37
38   def pci_devices
39     device = nil
40
41     IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
42       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
43         device = {
44           :slot => Regexp.last_match(1),
45           :domain => Regexp.last_match(2),
46           :bus => Regexp.last_match(3),
47           :device => Regexp.last_match(4),
48           :function => Regexp.last_match(5)
49         }
50       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
51         case Regexp.last_match(1)
52         when "Class" then device[:class_name] = Regexp.last_match(2)
53         when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
54         when "Device" then device[:device_name] = Regexp.last_match(2)
55         when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
56         when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
57         when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
58         when "Rev" then device[:revision] = Regexp.last_match(2)
59         when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
60         when "Driver" then device[:driver] = Regexp.last_match(2)
61         when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
62         end
63       elsif device && line =~ /^\s*$/
64         devices[device[:slot]] = device
65         device = nil
66       end
67     end
68   end
69
70   def network_devices
71     Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
72       name = File.basename(device)
73
74       devices[name] = {
75         :device => read_sysctl_link("#{device}/device"),
76         :duplex => read_sysctl_file("#{device}/duplex"),
77         :speed => read_sysctl_file("#{device}/speed")
78       }.delete_if { |_, v| v.nil? }
79     end
80   end
81
82   def memory_devices
83     device = nil
84
85     IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
86       if line =~ /^Memory Device\s*$/
87         device = {}
88       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
89         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
90       elsif device && line =~ /^\s*$/
91         devices << device
92         device = nil
93       end
94     end
95   end
96
97   def disk_devices
98     disk = Mash.new
99
100     disk[:controllers] = []
101     disk[:arrays] = []
102     disk[:disks] = []
103
104     find_direct_disks(disk)
105
106     find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
107     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
108     find_mpt_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
109     # aacraid
110     # areca (/opt/areca/x86_64/cli64)
111
112     find_md_arrays(disk)
113
114     disk
115   end
116
117   def find_direct_disks(devices)
118     Dir.glob("/sys/class/scsi_host/host*") do |host|
119       driver = read_sysctl_file("#{host}/proc_name")
120
121       if driver == "ahci" || driver == "mptsas"
122         bus = host.sub("/sys/class/scsi_host/host", "")
123
124         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
125           next unless File.exist?("#{device}/scsi_disk")
126
127           block = Dir.glob("#{device}/block/*").first
128           vendor = read_sysctl_file("#{device}/vendor")
129           model = read_sysctl_file("#{device}/model")
130           size = read_sysctl_file("#{block}/size").to_i * 512
131
132           if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
133             vendor = Regexp.last_match(1)
134             model = Regexp.last_match(2)
135           end
136
137           if size > 1_000_000_000_000
138             size = format "%d TB", size / 1_000_000_000_000
139           elsif size > 1000000000
140             size = format "%d GB", size / 1000000000
141           end
142
143           devices[:disks] << {
144             :id => devices[:disks].count,
145             :device => "/dev/#{File.basename(block)}",
146             :vendor => vendor,
147             :model => model,
148             :firmware_version => read_sysctl_file("#{device}/rev"),
149             :size => size,
150             :arrays => []
151           }
152         end
153       end
154     end
155   end
156
157   def find_md_arrays(devices)
158     File.new("/proc/mdstat", "r").each do |line|
159       next unless line =~ /^(md\d+) : active raid(\d+)((?: [a-z]+\d+\[\d+\](?:\([A-Z]\))*)+)$/
160
161       array = {
162         :id => devices[:arrays].count,
163         :device => "/dev/#{Regexp.last_match(1)}",
164         :raid_level => Regexp.last_match(2),
165         :disks => []
166       }
167
168       Regexp.last_match(3).scan(/ ([a-z]+)\d+\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
169         if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
170           disk[:arrays] << array[:id]
171           array[:disks] << disk[:id]
172         end
173       end
174
175       devices[:arrays] << array
176     end
177   end
178
179   def find_hp_disks(devices)
180     controllers = []
181     disks = []
182
183     controller = nil
184     array = nil
185     disk = nil
186
187     IO.popen(%w(hpssacli controller all show config detail)).each do |line|
188       if line =~ /^Smart Array (\S+) /
189         controller = {
190           :id => devices[:controllers].count,
191           :model => Regexp.last_match(1),
192           :arrays => [],
193           :disks => []
194         }
195
196         devices[:controllers] << controller
197
198         controllers << controller
199
200         array = nil
201         disk = nil
202       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
203         case Regexp.last_match(1)
204         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
205         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
206         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
207         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
208         end
209       elsif controller && line =~ /^      Logical Drive: (\d+)$/
210         array = {
211           :id => devices[:arrays].count,
212           :controller => controller[:id],
213           :number => Regexp.last_match(1),
214           :disks => []
215         }
216
217         devices[:arrays] << array
218         controller[:arrays] << array[:id]
219
220         disk = nil
221       elsif controller && line =~ /^      physicaldrive (\S+) /
222         disks << Regexp.last_match(1)
223       elsif array && line =~ /^      physicaldrive (\S+)$/
224         disk = {
225           :id => devices[:disks].count,
226           :controller => controller[:id],
227           :arrays => [array[:id]],
228           :location => Regexp.last_match(1),
229           :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
230         }
231
232         devices[:disks] << disk
233         controller[:disks] << disk[:id]
234         array[:disks] << disk[:id]
235       elsif disk && line =~ /^         (\S[^:]+):\s+(.*)$/
236         case Regexp.last_match(1)
237         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
238         when "Size" then disk[:size] = Regexp.last_match(2)
239         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
240         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
241         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
242         when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
243         end
244       elsif array && line =~ /^         (\S[^:]+):\s+(.*)$/
245         case Regexp.last_match(1)
246         when "Size" then array[:size] = Regexp.last_match(2)
247         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
248         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
249         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
250         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
251         end
252       end
253     end
254
255     controllers.each do |controller|
256       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
257         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
258       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
259         controller[:device] = "/dev/#{File.basename(device)}"
260       end
261     end
262   end
263
264   def find_megaraid_disks(devices)
265     controllers = []
266     arrays = []
267
268     controller = nil
269     array = nil
270     disk = nil
271
272     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
273       if line =~ /^PCI information for Controller (\d+)$/
274         controller = {
275           :id => devices[:controllers].count,
276           :arrays => [],
277           :disks => []
278         }
279
280         devices[:controllers] << controller
281
282         controllers << controller
283       elsif line =~ /^Bus Number\s+:\s+(\d+)$/
284         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
285       elsif line =~ /^Device Number\s+:\s+(\d+)$/
286         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
287       elsif line =~ /^Function Number\s+:\s+(\d+)$/
288         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
289       end
290     end
291
292     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
293       if line =~ /^Adapter #(\d+)$/
294         controller = controllers[Regexp.last_match(1).to_i]
295       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
296         case Regexp.last_match(1)
297         when "Product Name" then controller[:model] = Regexp.last_match(2)
298         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
299         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
300         end
301       end
302     end
303
304     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
305       if line =~ /^Adapter #(\d+)$/
306         controller = controllers[Regexp.last_match(1).to_i]
307       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
308         pci_slot = controller[:pci_slot]
309         target = Regexp.last_match(2)
310         device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:2:#{target}/*:2:#{target}:0/block/*").first
311
312         array = {
313           :id => devices[:arrays].count,
314           :controller => controller[:id],
315           :number => Regexp.last_match(1),
316           :device => "/dev/#{File.basename(device)}",
317           :disks => []
318         }
319
320         devices[:arrays] << array
321         controller[:arrays] << array[:id]
322
323         arrays << array
324
325         disk = nil
326       elsif array && line =~ /^PD: (\d+) Information$/
327         disk = {
328           :id => devices[:disks].count,
329           :controller => controller[:id],
330           :arrays => [array[:id]]
331         }
332
333         devices[:disks] << disk
334         controller[:disks] << disk[:id]
335         array[:disks] << disk[:id]
336       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
337         case Regexp.last_match(1)
338         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
339         when "WWN" then disk[:wwn] = Regexp.last_match(2)
340         when "PD Type" then disk[:interface] = Regexp.last_match(2)
341         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
342         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
343         end
344       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
345         case Regexp.last_match(1)
346         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
347         when "Size" then array[:size] = Regexp.last_match(2)
348         end
349       end
350     end
351
352     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
353       if line =~ /^Adapter #(\d+)$/
354         controller = controllers[Regexp.last_match(1).to_i]
355       elsif controller && line =~ /^Enclosure Device ID: \d+$/
356         disk = {
357           :controller => controller[:id]
358         }
359       elsif disk && line =~ /^WWN:\s+(\S+)$/
360         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
361           disk[:id] = devices[:disks].count
362           disk[:wwn] = Regexp.last_match(1)
363
364           devices[:disks] << disk
365         end
366       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
367         case Regexp.last_match(1)
368         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
369         when "WWN" then disk[:wwn] = Regexp.last_match(2)
370         when "PD Type" then disk[:interface] = Regexp.last_match(2)
371         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
372         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
373         end
374       end
375     end
376
377     controllers.each do |controller|
378       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
379         controller[:device] = "/dev/#{File.basename(device)}"
380       end
381     end
382   end
383
384   def find_mpt_disks(devices)
385     controllers = []
386
387     IO.popen(%w(sas2ircu list)).each do |line|
388       next unless line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
389       controllers[Regexp.last_match(1).to_i] = {
390         :id => devices[:controllers].count,
391         :model => Regexp.last_match(2),
392         :pci_slot => Regexp.last_match(3).sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
393         :arrays => [],
394         :disks => []
395       }
396
397       devices[:controllers] << controllers[Regexp.last_match(1).to_i]
398     end
399
400     controllers.each_with_index do |controller, index|
401       arrays = []
402       disks = []
403
404       array = nil
405       disk = nil
406
407       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
408         if line =~ /^IR volume (\d+)$/
409           array = {
410             :id => devices[:arrays].count,
411             :controller => controller[:id],
412             :number => Regexp.last_match(1),
413             :disks => []
414           }
415
416           devices[:arrays] << array
417           controller[:arrays] << array[:id]
418
419           arrays << array
420
421           disk = nil
422         elsif array && line =~ /^Device is a Hard disk$/
423           disk = {
424             :id => devices[:disks].count,
425             :controller => controller[:id],
426             :arrays => []
427           }
428
429           devices[:disks] << disk
430           controller[:disks] << disk[:id]
431
432           disks << disk
433         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
434           case Regexp.last_match(1)
435           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
436           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
437           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
438           when "Size (in MB)/(in sectors)" then disk[:size] = parse_disk_size("#{Regexp.last_match(2).split('/').first} MB")
439           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
440           when "Model Number" then disk[:model] = Regexp.last_match(2)
441           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
442           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
443           when "Protocol" then disk[:interface] = Regexp.last_match(2)
444           end
445         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
446           array[:disks] << Regexp.last_match(1)
447         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
448           case Regexp.last_match(1)
449           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
450           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
451           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
452           end
453         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
454           case Regexp.last_match(1)
455           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
456           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
457           end
458         end
459       end
460
461       arrays.each do |array|
462         array[:disks].map! do |location|
463           disk = disks.find { |disk| disk[:location] == location }
464
465           disk[:arrays] << array[:id]
466           disk[:id]
467         end
468       end
469     end
470   end
471
472   collect_data(:default) do
473     hardware Mash.new
474
475     hardware[:pci] = pci_devices
476     hardware[:network] = network_devices
477     hardware[:memory] = memory_devices
478     hardware[:disk] = disk_devices
479   end
480 end