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
17 IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
18 if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
20 :slot => Regexp.last_match(1),
21 :domain => Regexp.last_match(2),
22 :bus => Regexp.last_match(3),
23 :device => Regexp.last_match(4),
24 :function => Regexp.last_match(5)
26 elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
27 case Regexp.last_match(1)
28 when "Class" then device[:class_name] = Regexp.last_match(2)
29 when "Vendor" then device[:vendor_name] = Regexp.last_match(2)
30 when "Device" then device[:device_name] = Regexp.last_match(2)
31 when "SVendor" then device[:subsystem_vendor_name] = Regexp.last_match(2)
32 when "SDevice" then device[:subsystem_device_name] = Regexp.last_match(2)
33 when "PhySlot" then device[:physical_slot] = Regexp.last_match(2)
34 when "Rev" then device[:revision] = Regexp.last_match(2)
35 when "ProgIf" then device[:programming_interface] = Regexp.last_match(2)
36 when "Driver" then device[:driver] = Regexp.last_match(2)
37 when "Module" then device[:modules] = Array(device[:modules]) << Regexp.last_match(2)
39 elsif device && line =~ /^\s*$/
40 devices[device[:slot]] = device
47 Dir.glob("/sys/class/net/*").each_with_object(Mash.new) do |device, devices|
48 name = File.basename(device)
51 :device => read_sysctl_link("#{device}/device"),
52 :duplex => read_sysctl_file("#{device}/duplex"),
53 :speed => read_sysctl_file("#{device}/speed")
54 }.delete_if { |_, v| v.nil? }
61 IO.popen(["dmidecode", "-t", "memory"]).each_with_object([]) do |line, devices|
62 if line =~ /^Memory Device\s*$/
64 elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
65 device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
66 elsif device && line =~ /^\s*$/
76 disk[:controllers] = []
80 find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
81 find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
86 def find_hp_disks(devices)
94 IO.popen(%w(hpssacli controller all show config detail)).each do |line|
95 if line =~ /^Smart Array (\S+) /
97 :id => devices[:controllers].count,
98 :model => Regexp.last_match(1),
103 devices[:controllers] << controller
105 controllers << controller
109 elsif controller && line =~ /^ (\S.*):\s+(.*)$/
110 case Regexp.last_match(1)
111 when "Serial Number" then controller[:serial_number] = Regexp.last_match(2)
112 when "Hardware Revision" then controller[:hardware_version] = Regexp.last_match(2)
113 when "Firmware Version" then controller[:firmware_version] = Regexp.last_match(2)
114 when "PCI Address (Domain:Bus:Device.Function)" then controller[:pci_slot] = Regexp.last_match(2)
116 elsif controller && line =~ /^ Logical Drive: (\d+)$/
118 :id => devices[:arrays].count,
119 :controller => controller[:id],
120 :number => Regexp.last_match(1),
124 devices[:arrays] << array
125 controller[:arrays] << array[:id]
128 elsif controller && line =~ /^ physicaldrive (\S+) /
129 disks << Regexp.last_match(1)
130 elsif array && line =~ /^ physicaldrive (\S+)$/
132 :id => devices[:disks].count,
133 :controller => controller[:id],
134 :array => array[:id],
135 :location => Regexp.last_match(1),
136 :smart_device => "cciss,#{disks.find_index(Regexp.last_match(1))}"
139 devices[:disks] << disk
140 controller[:disks] << disk[:id]
141 array[:disks] << disk[:id]
142 elsif disk && line =~ /^ (\S[^:]+):\s+(.*)$/
143 case Regexp.last_match(1)
144 when "Interface Type" then disk[:interface] = Regexp.last_match(2)
145 when "Size" then disk[:size] = Regexp.last_match(2)
146 when "Rotational Speed" then disk[:rpm] = Regexp.last_match(2)
147 when "Firmware Revision" then disk[:firmware_version] = Regexp.last_match(2)
148 when "Serial Number" then disk[:serial_number] = Regexp.last_match(2)
149 when "Model" then disk[:vendor], disk[:model] = Regexp.last_match(2).squeeze(" ").strip.sub(/^ATA /, "").split
151 elsif array && line =~ /^ (\S[^:]+):\s+(.*)$/
152 case Regexp.last_match(1)
153 when "Size" then array[:size] = Regexp.last_match(2)
154 when "Fault Tolerance" then array[:raid_level] = Regexp.last_match(2)
155 when "Disk Name" then array[:device] = Regexp.last_match(2).strip
156 when "Mount Points" then array[:mount_point] = Regexp.last_match(2).split.first
157 when "Unique Identifier" then array[:wwn] = Regexp.last_match(2)
162 controllers.each do |controller|
163 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/cciss*").first
164 controller[:device] = File.basename(device).sub(/^cciss(\d+)$/, "/dev/cciss/c\\1d0")
165 elsif device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target0:0:0/0:0:0:0/scsi_generic/sg*").first
166 controller[:device] = "/dev/#{File.basename(device)}"
171 def find_megaraid_disks(devices)
179 IO.popen(%w(megacli -AdpGetPciInfo -aAll -NoLog)).each do |line|
180 if line =~ /^PCI information for Controller (\d+)$/
182 :id => devices[:controllers].count,
187 devices[:controllers] << controller
189 controllers << controller
190 elsif line =~ /^Bus Number\s+:\s+(\d+)$/
191 controller[:pci_slot] = sprintf "0000:%02x", Integer("0x#{Regexp.last_match(1)}")
192 elsif line =~ /^Device Number\s+:\s+(\d+)$/
193 controller[:pci_slot] = sprintf "%s:%02x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
194 elsif line =~ /^Function Number\s+:\s+(\d+)$/
195 controller[:pci_slot] = sprintf "%s.%01x", controller[:pci_slot], Integer("0x#{Regexp.last_match(1)}")
199 IO.popen(%w(megacli -AdpAllInfo -aAll -NoLog)).each do |line|
200 if line =~ /^Adapter #(\d+)$/
201 controller = controllers[Regexp.last_match(1).to_i]
202 elsif line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
203 case Regexp.last_match(1)
204 when "Product Name" then controller[:model] = Regexp.last_match(2)
205 when "Serial No" then controller[:serial_number] = Regexp.last_match(2)
206 when "FW Package Build" then controller[:firmware_version] = Regexp.last_match(2)
211 IO.popen(%w(megacli -LdPdInfo -aAll -NoLog)).each do |line|
212 if line =~ /^Adapter #(\d+)$/
213 controller = controllers[Regexp.last_match(1).to_i]
214 elsif controller && line =~ /^Virtual Drive: (\d+) \(Target Id: (\d+)\)$/
216 :id => devices[:arrays].count,
217 :controller => controller[:id],
218 :number => Regexp.last_match(1),
222 devices[:arrays] << array
223 controller[:arrays] << array[:id]
228 elsif array && line =~ /^PD: (\d+) Information$/
230 :id => devices[:disks].count,
231 :controller => controller[:id],
235 devices[:disks] << disk
236 controller[:disks] << disk[:id]
237 array[:disks] << disk[:id]
238 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
239 case Regexp.last_match(1)
240 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
241 when "WWN" then disk[:wwn] = Regexp.last_match(2)
242 when "PD Type" then disk[:interface] = Regexp.last_match(2)
243 when "Raw Size" then disk[:size] = Regexp.last_match(2).sub(/\s*\[.*\]$/, "")
244 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
246 elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
247 case Regexp.last_match(1)
248 when "RAID Level" then array[:raid_level] = Regexp.last_match(2).scan(/Primary-(\d+)/).first.first
249 when "Size" then array[:size] = Regexp.last_match(2)
254 IO.popen(%w(megacli -PDList -aAll -NoLog)).each do |line|
255 if line =~ /^Adapter #(\d+)$/
256 controller = controllers[Regexp.last_match(1).to_i]
257 elsif controller && line =~ /^Enclosure Device ID: \d+$/
259 :controller => controller[:id]
261 elsif disk && line =~ /^WWN:\s+(\S+)$/
262 unless devices[:disks].find { |d| d[:wwn] == Regexp.last_match(1) }
263 disk[:id] = devices[:disks].count
264 disk[:wwn] = Regexp.last_match(1)
266 devices[:disks] << disk
268 elsif disk && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
269 case Regexp.last_match(1)
270 when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
271 when "WWN" then disk[:wwn] = Regexp.last_match(2)
272 when "PD Type" then disk[:interface] = Regexp.last_match(2)
273 when "Raw Size" then disk[:size] = Regexp.last_match(2).sub(/\s*\[.*\]$/, "")
274 when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
279 controllers.each do |controller|
280 if device = Dir.glob("/sys/bus/pci/devices/#{controller[:pci_slot]}/host*/target*:2:0/*/scsi_generic/sg*").first
281 controller[:device] = "/dev/#{File.basename(device)}"
286 collect_data(:default) do
289 hardware[:pci] = pci_devices
290 hardware[:network] = network_devices
291 hardware[:memory] = memory_devices
292 hardware[:disk] = disk_devices