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 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
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)
89 find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
90 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
92 # areca (/opt/areca/x86_64/cli64)
97 def find_direct_disks(devices)
98 Dir.glob("/sys/class/scsi_host/host*") do |host|
99 driver = read_sysctl_file("#{host}/proc_name")
101 if driver == "ahci" || driver == "mptsas" ||
102 driver == "mpt2sas" || driver == "mpt3sas"
103 bus = host.sub("/sys/class/scsi_host/host", "")
105 Dir.glob("/sys/bus/scsi/devices/#{bus}:0:*").each do |device|
106 next unless File.exist?("#{device}/scsi_disk")
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
113 if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
114 vendor = Regexp.last_match(1)
115 model = Regexp.last_match(2)
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
125 :id => devices[:disks].count,
126 :device => "/dev/#{File.basename(block)}",
129 :firmware_version => read_sysctl_file("#{device}/rev"),
137 def find_hp_disks(devices)
145 IO.popen(%w(hpssacli controller all show config detail)).each do |line|
146 if line =~ /^Smart Array (\S+) /
148 :id => devices[:controllers].count,
149 :model => Regexp.last_match(1),
154 devices[:controllers] << controller
156 controllers << controller
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)
167 elsif controller && line =~ /^ Logical Drive: (\d+)$/
169 :id => devices[:arrays].count,
170 :controller => controller[:id],
171 :number => Regexp.last_match(1),
175 devices[:arrays] << array
176 controller[:arrays] << array[:id]
179 elsif controller && line =~ /^ physicaldrive (\S+) /
180 disks << Regexp.last_match(1)
181 elsif array && line =~ /^ physicaldrive (\S+)$/
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))}"
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
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)
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)}"
222 def find_megaraid_disks(devices)
230 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
231 if line =~ /^PCI information for Controller (\d+)$/
233 :id => devices[:controllers].count,
238 devices[:controllers] << controller
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)}")
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)
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+)\)$/
267 :id => devices[:arrays].count,
268 :controller => controller[:id],
269 :number => Regexp.last_match(1),
273 devices[:arrays] << array
274 controller[:arrays] << array[:id]
279 elsif array && line =~ /^PD: (\d+) Information$/
281 :id => devices[:disks].count,
282 :controller => controller[:id],
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
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)
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+$/
310 :controller => controller[:id]
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)
317 devices[:disks] << disk
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
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)}"
337 collect_data(:default) do
340 hardware[:pci] = pci_devices
341 hardware[:network] = network_devices
342 hardware[:memory] = memory_devices
343 hardware[:disk] = disk_devices