]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
1f0a90fb6c4a9c14fa023f41682326364b1f3326
[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     end
20   end
21
22   def pci_devices
23     device = nil
24
25     IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
26       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
27         device = {
28           :slot => Regexp.last_match(1),
29           :domain => Regexp.last_match(2),
30           :bus => Regexp.last_match(3),
31           :device => Regexp.last_match(4),
32           :function => Regexp.last_match(5)
33         }
34       elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
35         case Regexp.last_match(1)
36         when "Class" then device[:class_name] = Regexp.last_match(2)
37         when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
38         when "Device" then device[:device_name] = Regexp.last_match(2)
39         when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
40         when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
41         when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
42         when "Rev" then device[:revision] = Regexp.last_match(2)
43         when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
44         when "Driver" then device[:driver] = Regexp.last_match(2)
45         when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
46         end
47       elsif device && line =~ /^\s*$/
48         devices[device[:slot]] = device
49         device = nil
50       end
51     end
52   end
53
54   def network_devices
55     Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
56       name = File.basename(device)
57
58       devices[name] = {
59         :device => read_sysctl_link("#{device}/device"),
60         :duplex => read_sysctl_file("#{device}/duplex"),
61         :speed => read_sysctl_file("#{device}/speed")
62       }.delete_if { |_, v| v.nil? }
63     end
64   end
65
66   def memory_devices
67     device = nil
68
69     IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
70       if line =~ /^Memory Device\s*$/
71         device = {}
72       elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
73         device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
74       elsif device && line =~ /^\s*$/
75         devices << device
76         device = nil
77       end
78     end
79   end
80
81   def disk_devices
82     disk = Mash.new
83
84     disk[:controllers] = []
85     disk[:arrays] = []
86     disk[:disks] = []
87
88     find_direct_disks(disk)
89
90     find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
91     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
92     # aacraid
93     # areca (/opt/areca/x86_64/cli64)
94
95     find_md_arrays(disk)
96
97     disk
98   end
99
100   def find_direct_disks(devices)
101     Dir.glob("/sys/class/scsi_host/host*") do |host|
102       driver = read_sysctl_file("#{host}/proc_name")
103
104       if driver == "ahci" || driver == "mptsas" ||
105          driver == "mpt2sas" || driver == "mpt3sas"
106         bus = host.sub("/sys/class/scsi_host/host", "")
107
108         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
109           next unless File.exist?("#{device}/scsi_disk")
110
111           block = Dir.glob("#{device}/block/*").first
112           vendor = read_sysctl_file("#{device}/vendor")
113           model = read_sysctl_file("#{device}/model")
114           size = read_sysctl_file("#{block}/size").to_i * 512
115
116           if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
117             vendor = Regexp.last_match(1)
118             model = Regexp.last_match(2)
119           end
120
121           if size > 1_000_000_000_000
122             size = format "%d TB", size / 1_000_000_000_000
123           elsif size > 1000000000
124             size = format "%d GB", size / 1000000000
125           end
126
127           devices[:disks] << {
128             :id => devices[:disks].count,
129             :device => "/dev/#{File.basename(block)}",
130             :vendor => vendor,
131             :model => model,
132             :firmware_version => read_sysctl_file("#{device}/rev"),
133             :size => size,
134             :arrays => []
135           }
136         end
137       end
138     end
139   end
140
141   def find_md_arrays(devices)
142     File.new("/proc/mdstat", "r").each do |line|
143       next unless line =~ /^(md\d+) : active raid(\d+)((?: [a-z]+\d+\[\d+\](?:\([A-Z]\))*)+)$/
144
145       array = {
146         :id => devices[:arrays].count,
147         :device => "/dev/#{Regexp.last_match(1)}",
148         :raid_level => Regexp.last_match(2),
149         :disks => []
150       }
151
152       Regexp.last_match(3).scan(/ ([a-z]+)\d+\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
153         if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
154           disk[:arrays] << array[:id]
155           array[:disks] << disk[:id]
156         end
157       end
158
159       devices[:arrays] << array
160     end
161   end
162
163   def find_hp_disks(devices)
164     controllers = []
165     disks = []
166
167     controller = nil
168     array = nil
169     disk = nil
170
171     IO.popen(%w(hpssacli controller all show config detail)).each do |line|
172       if line =~ /^Smart Array (\S+) /
173         controller = {
174           :id => devices[:controllers].count,
175           :model => Regexp.last_match(1),
176           :arrays => [],
177           :disks => []
178         }
179
180         devices[:controllers] << controller
181
182         controllers << controller
183
184         array = nil
185         disk = nil
186       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
187         case Regexp.last_match(1)
188         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
189         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
190         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
191         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
192         end
193       elsif controller && line =~ /^      Logical Drive: (\d+)$/
194         array = {
195           :id => devices[:arrays].count,
196           :controller => controller[:id],
197           :number => Regexp.last_match(1),
198           :disks => []
199         }
200
201         devices[:arrays] << array
202         controller[:arrays] << array[:id]
203
204         disk = nil
205       elsif controller && line =~ /^      physicaldrive (\S+) /
206         disks << Regexp.last_match(1)
207       elsif array && line =~ /^      physicaldrive (\S+)$/
208         disk = {
209           :id => devices[:disks].count,
210           :controller => controller[:id],
211           :arrays => [array[:id]],
212           :location => Regexp.last_match(1),
213           :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
214         }
215
216         devices[:disks] << disk
217         controller[:disks] << disk[:id]
218         array[:disks] << disk[:id]
219       elsif disk && line =~ /^         (\S[^:]+):\s+(.*)$/
220         case Regexp.last_match(1)
221         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
222         when "Size" then disk[:size] = Regexp.last_match(2)
223         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
224         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
225         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
226         when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
227         end
228       elsif array && line =~ /^         (\S[^:]+):\s+(.*)$/
229         case Regexp.last_match(1)
230         when "Size" then array[:size] = Regexp.last_match(2)
231         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
232         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
233         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
234         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
235         end
236       end
237     end
238
239     controllers.each do |controller|
240       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
241         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
242       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
243         controller[:device] = "/dev/#{File.basename(device)}"
244       end
245     end
246   end
247
248   def find_megaraid_disks(devices)
249     controllers = []
250     arrays = []
251
252     controller = nil
253     array = nil
254     disk = nil
255
256     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
257       if line =~ /^PCI information for Controller (\d+)$/
258         controller = {
259           :id => devices[:controllers].count,
260           :arrays => [],
261           :disks => []
262         }
263
264         devices[:controllers] << controller
265
266         controllers << controller
267       elsif line =~ /^Bus Number\s+:\s+(\d+)$/
268         controller[:pci_slot] = format "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
269       elsif line =~ /^Device Number\s+:\s+(\d+)$/
270         controller[:pci_slot] = format "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
271       elsif line =~ /^Function Number\s+:\s+(\d+)$/
272         controller[:pci_slot] = format "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
273       end
274     end
275
276     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
277       if line =~ /^Adapter #(\d+)$/
278         controller = controllers[Regexp.last_match(1).to_i]
279       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
280         case Regexp.last_match(1)
281         when "Product Name" then controller[:model] = Regexp.last_match(2)
282         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
283         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
284         end
285       end
286     end
287
288     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
289       if line =~ /^Adapter #(\d+)$/
290         controller = controllers[Regexp.last_match(1).to_i]
291       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
292         array = {
293           :id => devices[:arrays].count,
294           :controller => controller[:id],
295           :number => Regexp.last_match(1),
296           :disks => []
297         }
298
299         devices[:arrays] << array
300         controller[:arrays] << array[:id]
301
302         arrays << array
303
304         disk = nil
305       elsif array && line =~ /^PD: (\d+) Information$/
306         disk = {
307           :id => devices[:disks].count,
308           :controller => controller[:id],
309           :arrays => [array[:id]]
310         }
311
312         devices[:disks] << disk
313         controller[:disks] << disk[:id]
314         array[:disks] << disk[:id]
315       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
316         case Regexp.last_match(1)
317         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
318         when "WWN" then disk[:wwn] = Regexp.last_match(2)
319         when "PD Type" then disk[:interface] = Regexp.last_match(2)
320         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
321         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
322         end
323       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
324         case Regexp.last_match(1)
325         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
326         when "Size" then array[:size] = Regexp.last_match(2)
327         end
328       end
329     end
330
331     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
332       if line =~ /^Adapter #(\d+)$/
333         controller = controllers[Regexp.last_match(1).to_i]
334       elsif controller && line =~ /^Enclosure Device ID: \d+$/
335         disk = {
336           :controller => controller[:id]
337         }
338       elsif disk && line =~ /^WWN:\s+(\S+)$/
339         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
340           disk[:id] = devices[:disks].count
341           disk[:wwn] = Regexp.last_match(1)
342
343           devices[:disks] << disk
344         end
345       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
346         case Regexp.last_match(1)
347         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
348         when "WWN" then disk[:wwn] = Regexp.last_match(2)
349         when "PD Type" then disk[:interface] = Regexp.last_match(2)
350         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
351         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
352         end
353       end
354     end
355
356     controllers.each do |controller|
357       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
358         controller[:device] = "/dev/#{File.basename(device)}"
359       end
360     end
361   end
362
363   collect_data(:default) do
364     hardware Mash.new
365
366     hardware[:pci] = pci_devices
367     hardware[:network] = network_devices
368     hardware[:memory] = memory_devices
369     hardware[:disk] = disk_devices
370   end
371 end