]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
e78e0c6bc5ad1bc83c2a5c7c31ced24c7c03d196
[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         array = {
309           :id => devices[:arrays].count,
310           :controller => controller[:id],
311           :number => Regexp.last_match(1),
312           :disks => []
313         }
314
315         devices[:arrays] << array
316         controller[:arrays] << array[:id]
317
318         arrays << array
319
320         disk = nil
321       elsif array && line =~ /^PD: (\d+) Information$/
322         disk = {
323           :id => devices[:disks].count,
324           :controller => controller[:id],
325           :arrays => [array[:id]]
326         }
327
328         devices[:disks] << disk
329         controller[:disks] << disk[:id]
330         array[:disks] << disk[:id]
331       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
332         case Regexp.last_match(1)
333         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
334         when "WWN" then disk[:wwn] = Regexp.last_match(2)
335         when "PD Type" then disk[:interface] = Regexp.last_match(2)
336         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
337         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
338         end
339       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
340         case Regexp.last_match(1)
341         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
342         when "Size" then array[:size] = Regexp.last_match(2)
343         end
344       end
345     end
346
347     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
348       if line =~ /^Adapter #(\d+)$/
349         controller = controllers[Regexp.last_match(1).to_i]
350       elsif controller && line =~ /^Enclosure Device ID: \d+$/
351         disk = {
352           :controller => controller[:id]
353         }
354       elsif disk && line =~ /^WWN:\s+(\S+)$/
355         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
356           disk[:id] = devices[:disks].count
357           disk[:wwn] = Regexp.last_match(1)
358
359           devices[:disks] << disk
360         end
361       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
362         case Regexp.last_match(1)
363         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
364         when "WWN" then disk[:wwn] = Regexp.last_match(2)
365         when "PD Type" then disk[:interface] = Regexp.last_match(2)
366         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
367         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
368         end
369       end
370     end
371
372     controllers.each do |controller|
373       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
374         controller[:device] = "/dev/#{File.basename(device)}"
375       end
376     end
377   end
378
379   def find_mpt_disks(devices)
380     controllers = []
381
382     IO.popen(%w(sas2ircu list)).each do |line|
383       if line =~ /^\s+(\d+)\s+(\S+)\s+\h+h\s+\h+h\s+(\S+)\s+\h+h\s+\h+h\s*$/
384         controllers[$1.to_i] = {
385           :id => devices[:controllers].count,
386           :model => $2,
387           :pci_slot => $3.sub(/^(\h{2})h:(\h{2})h:(\h{2})h:0(\h)h$/, "00\\1:\\2:\\3.\\4"),
388           :arrays => [],
389           :disks => []
390         }
391
392         devices[:controllers] << controllers[$1.to_i]
393       end
394     end
395
396     controllers.each_with_index do |controller, index|
397       arrays = []
398       disks = []
399
400       array = nil
401       disk = nil
402
403       IO.popen(["sas2ircu", index.to_s, "display"]).each do |line|
404         if line =~ /^IR volume (\d+)$/
405           array = {
406             :id => devices[:arrays].count,
407             :controller => controller[:id],
408             :number => Regexp.last_match(1),
409             :disks => []
410           }
411
412           devices[:arrays] << array
413           controller[:arrays] << array[:id]
414
415           arrays << array
416
417           disk = nil
418         elsif array && line =~ /^Device is a Hard disk$/
419           disk = {
420             :id => devices[:disks].count,
421             :controller => controller[:id],
422             :arrays => []
423           }
424
425           devices[:disks] << disk
426           controller[:disks] << disk[:id]
427
428           disks << disk
429         elsif disk && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
430           case Regexp.last_match(1)
431           when "Enclosure #" then disk[:location] = Regexp.last_match(2)
432           when "Slot #" then disk[:location] = "#{disk[:location]}:#{Regexp.last_match(2)}"
433           when "SAS Address" then disk[:device] = find_sas_device(Regexp.last_match(2).tr("-", ""))
434           when "Size (in MB)/(in sectors)" then disk[:size] = parse_disk_size("#{Regexp.last_match(2).split("/").first} MB")
435           when "Manufacturer" then disk[:vendor] = Regexp.last_match(2)
436           when "Model Number" then disk[:model] = Regexp.last_match(2)
437           when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
438           when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
439           when "Protocol" then disk[:interface] = Regexp.last_match(2)
440           end
441         elsif array && line =~ /^  PHY\[\d+\] Enclosure#\/Slot#\s+:\s+(\d+:\d+)\s*$/
442           array[:disks] << $1
443         elsif array && line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
444           case Regexp.last_match(1)
445           when "Volume wwid" then array[:device] = find_sas_device(Regexp.last_match(2))
446           when "RAID level" then array[:raid_level] = Regexp.last_match(2).sub(/^RAID/, "")
447           when "Size (in MB)" then array[:size] = "#{Regexp.last_match(2)} MB"
448           end
449         elsif line =~ /^  (\S.*\S)\s+:\s+(.*\S)\s*$/
450           case Regexp.last_match(1)
451           when "BIOS version" then controller[:bios_version] = Regexp.last_match(2)
452           when "Firmware version" then controller[:firmware_version] = Regexp.last_match(2)
453           end
454         end
455       end
456
457       arrays.each do |array|
458         array[:disks].map! do |location|
459           disk = disks.find { |disk| disk[:location] == location }
460
461           disk[:arrays] << array[:id]
462           disk[:id]
463         end
464       end
465     end
466   end
467
468   disk_devices
469
470   collect_data(:default) do
471     hardware Mash.new
472
473     hardware[:pci] = pci_devices
474     hardware[:network] = network_devices
475     hardware[:memory] = memory_devices
476     hardware[:disk] = disk_devices
477   end
478 end