1 Ohai.plugin(:Hardware) do
4 def read_sysctl_link(file)
5 File.basename(File.readlink(file))
9 def read_sysctl_file(file)
11 rescue Errno::ENOENT, Errno::EINVAL
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
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*$/
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)
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)
47 elsif device && line =~ /^\s*$/
48 devices[device[:slot]] = device
55 Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
56 name = File.basename(device)
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? }
69 IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
70 if line =~ /^Memory Device\s*$/
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*$/
84 disk[:controllers] = []
88 find_direct_disks(disk)
90 find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
91 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
93 # areca (/opt/areca/x86_64/cli64)
100 def find_direct_disks(devices)
101 Dir.glob("/sys/class/scsi_host/host*") do |host|
102 driver = read_sysctl_file("#{host}/proc_name")
104 if driver == "ahci" || driver == "mptsas" ||
105 driver == "mpt2sas" || driver == "mpt3sas"
106 bus = host.sub("/sys/class/scsi_host/host", "")
108 Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
109 next unless File.exist?("#{device}/scsi_disk")
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
116 if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
117 vendor = Regexp.last_match(1)
118 model = Regexp.last_match(2)
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
128 :id => devices[:disks].count,
129 :device => "/dev/#{File.basename(block)}",
132 :firmware_version => read_sysctl_file("#{device}/rev"),
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]\))*)+)$/
146 :id => devices[:arrays].count,
147 :device => "/dev/#{Regexp.last_match(1)}",
148 :raid_level => Regexp.last_match(2),
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]
159 devices[:arrays] << array
163 def find_hp_disks(devices)
171 IO.popen(%w(hpssacli controller all show config detail)).each do |line|
172 if line =~ /^Smart Array (\S+) /
174 :id => devices[:controllers].count,
175 :model => Regexp.last_match(1),
180 devices[:controllers] << controller
182 controllers << controller
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)
193 elsif controller && line =~ /^ Logical Drive: (\d+)$/
195 :id => devices[:arrays].count,
196 :controller => controller[:id],
197 :number => Regexp.last_match(1),
201 devices[:arrays] << array
202 controller[:arrays] << array[:id]
205 elsif controller && line =~ /^ physicaldrive (\S+) /
206 disks << Regexp.last_match(1)
207 elsif array && line =~ /^ physicaldrive (\S+)$/
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))}"
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
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)
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)}"
248 def find_megaraid_disks(devices)
256 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
257 if line =~ /^PCI information for Controller (\d+)$/
259 :id => devices[:controllers].count,
264 devices[:controllers] << controller
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)}")
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)
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+)\)$/
293 :id => devices[:arrays].count,
294 :controller => controller[:id],
295 :number => Regexp.last_match(1),
299 devices[:arrays] << array
300 controller[:arrays] << array[:id]
305 elsif array && line =~ /^PD: (\d+) Information$/
307 :id => devices[:disks].count,
308 :controller => controller[:id],
309 :arrays => [array[:id]]
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
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)
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+$/
336 :controller => controller[:id]
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)
343 devices[:disks] << disk
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
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)}"
363 collect_data(:default) do
366 hardware[:pci] = pci_devices
367 hardware[:network] = network_devices
368 hardware[:memory] = memory_devices
369 hardware[:disk] = disk_devices