]> git.openstreetmap.org Git - chef.git/blob - cookbooks/hardware/templates/default/ohai.rb.erb
Gather information on directly connected disks
[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       sprintf "%dTB", Regexp.last_match(1).to_f * 2**40 / 1_000_000_000_000
17     elsif size =~ /^(\d+(?:\.\d+))?\s*GB/i
18       sprintf "%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     find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
90     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
91     # aacraid
92     # areca (/opt/areca/x86_64/cli64)
93
94     disk
95   end
96
97   def find_direct_disks(devices)
98     Dir.glob("/sys/class/scsi_host/host*") do |host|
99       driver = read_sysctl_file("#{host}/proc_name")
100
101       if driver == "ahci" || driver == "mptsas" ||
102          driver == "mpt2sas" || driver == "mpt3sas"
103         bus = host.sub("/sys/class/scsi_host/host", "")
104
105         Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
106           next unless File.exist?("#{device}/scsi_disk")
107
108           block = Dir.glob("#{device}/block/*").first
109           vendor = read_sysctl_file("#{device}/vendor")
110           model = read_sysctl_file("#{device}/model")
111           size = read_sysctl_file("#{block}/size").to_i * 512
112
113           if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
114             vendor = Regexp.last_match(1)
115             model = Regexp.last_match(2)
116           end
117
118           if size > 1_000_000_000_000
119             size = sprintf "%d TB", size / 1_000_000_000_000
120           elsif size > 1000000000
121             size = sprintf "%d GB", size / 1000000000
122           end
123
124           devices[:disks] << {
125             :id => devices[:disks].count,
126             :device => "/dev/#{File.basename(block)}",
127             :vendor => vendor,
128             :model => model,
129             :firmware_version => read_sysctl_file("#{device}/rev"),
130             :size => size
131           }
132         end
133       end
134     end
135   end
136
137   def find_hp_disks(devices)
138     controllers = []
139     disks = []
140
141     controller = nil
142     array = nil
143     disk = nil
144
145     IO.popen(%w(hpssacli controller all show config detail)).each do |line|
146       if line =~ /^Smart Array (\S+) /
147         controller = {
148           :id => devices[:controllers].count,
149           :model => Regexp.last_match(1),
150           :arrays => [],
151           :disks => []
152         }
153
154         devices[:controllers] << controller
155
156         controllers << controller
157
158         array = nil
159         disk = nil
160       elsif controller && line =~ /^   (\S.*):\s+(.*)$/
161         case Regexp.last_match(1)
162         when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
163         when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
164         when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
165         when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
166         end
167       elsif controller && line =~ /^      Logical Drive: (\d+)$/
168         array = {
169           :id => devices[:arrays].count,
170           :controller => controller[:id],
171           :number => Regexp.last_match(1),
172           :disks => []
173         }
174
175         devices[:arrays] << array
176         controller[:arrays] << array[:id]
177
178         disk = nil
179       elsif controller && line =~ /^      physicaldrive (\S+) /
180         disks << Regexp.last_match(1)
181       elsif array && line =~ /^      physicaldrive (\S+)$/
182         disk = {
183           :id => devices[:disks].count,
184           :controller => controller[:id],
185           :array => array[:id],
186           :location => Regexp.last_match(1),
187           :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
188         }
189
190         devices[:disks] << disk
191         controller[:disks] << disk[:id]
192         array[:disks] << disk[:id]
193       elsif disk && line =~ /^         (\S[^:]+):\s+(.*)$/
194         case Regexp.last_match(1)
195         when "Interface Type" then disk[:interface] = Regexp.last_match(2)
196         when "Size" then disk[:size] = Regexp.last_match(2)
197         when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
198         when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
199         when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
200         when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
201         end
202       elsif array && line =~ /^         (\S[^:]+):\s+(.*)$/
203         case Regexp.last_match(1)
204         when "Size" then array[:size] = Regexp.last_match(2)
205         when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
206         when "Disk Name" then array[:device] = Regexp.last_match(2).strip
207         when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
208         when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
209         end
210       end
211     end
212
213     controllers.each do |controller|
214       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
215         controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
216       elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
217         controller[:device] = "/dev/#{File.basename(device)}"
218       end
219     end
220   end
221
222   def find_megaraid_disks(devices)
223     controllers = []
224     arrays = []
225
226     controller = nil
227     array = nil
228     disk = nil
229
230     IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
231       if line =~ /^PCI information for Controller (\d+)$/
232         controller = {
233           :id => devices[:controllers].count,
234           :arrays => [],
235           :disks => []
236         }
237
238         devices[:controllers] << controller
239
240         controllers << controller
241       elsif line =~ /^Bus Number\s+:\s+(\d+)$/
242         controller[:pci_slot] = sprintf "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
243       elsif line =~ /^Device Number\s+:\s+(\d+)$/
244         controller[:pci_slot] = sprintf "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
245       elsif line =~ /^Function Number\s+:\s+(\d+)$/
246         controller[:pci_slot] = sprintf "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
247       end
248     end
249
250     IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
251       if line =~ /^Adapter #(\d+)$/
252         controller = controllers[Regexp.last_match(1).to_i]
253       elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
254         case Regexp.last_match(1)
255         when "Product Name" then controller[:model] = Regexp.last_match(2)
256         when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
257         when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
258         end
259       end
260     end
261
262     IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
263       if line =~ /^Adapter #(\d+)$/
264         controller = controllers[Regexp.last_match(1).to_i]
265       elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
266         array = {
267           :id => devices[:arrays].count,
268           :controller => controller[:id],
269           :number => Regexp.last_match(1),
270           :disks => []
271         }
272
273         devices[:arrays] << array
274         controller[:arrays] << array[:id]
275
276         arrays << array
277
278         disk = nil
279       elsif array && line =~ /^PD: (\d+) Information$/
280         disk = {
281           :id => devices[:disks].count,
282           :controller => controller[:id],
283           :array => array[:id]
284         }
285
286         devices[:disks] << disk
287         controller[:disks] << disk[:id]
288         array[:disks] << disk[:id]
289       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
290         case Regexp.last_match(1)
291         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
292         when "WWN" then disk[:wwn] = Regexp.last_match(2)
293         when "PD Type" then disk[:interface] = Regexp.last_match(2)
294         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
295         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
296         end
297       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
298         case Regexp.last_match(1)
299         when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
300         when "Size" then array[:size] = Regexp.last_match(2)
301         end
302       end
303     end
304
305     IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
306       if line =~ /^Adapter #(\d+)$/
307         controller = controllers[Regexp.last_match(1).to_i]
308       elsif controller && line =~ /^Enclosure Device ID: \d+$/
309         disk = {
310           :controller => controller[:id]
311         }
312       elsif disk && line =~ /^WWN:\s+(\S+)$/
313         unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
314           disk[:id] = devices[:disks].count
315           disk[:wwn] = Regexp.last_match(1)
316
317           devices[:disks] << disk
318         end
319       elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
320         case Regexp.last_match(1)
321         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
322         when "WWN" then disk[:wwn] = Regexp.last_match(2)
323         when "PD Type" then disk[:interface] = Regexp.last_match(2)
324         when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
325         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
326         end
327       end
328     end
329
330     controllers.each do |controller|
331       if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
332         controller[:device] = "/dev/#{File.basename(device)}"
333       end
334     end
335   end
336
337   collect_data(:default) do
338     hardware Mash.new
339
340     hardware[:pci] = pci_devices
341     hardware[:network] = network_devices
342     hardware[:memory] = memory_devices
343     hardware[:disk] = disk_devices
344   end
345 end