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