]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/hardware/templates/default/ohai.rb.erb
Gather raw IDs for PCI devices
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
index 87560b8ddd678939b5088ecfc46239d2fe64d1c2..e8e01845ecb5c6b2ef21b984063b17e21df16f9a 100644 (file)
@@ -61,9 +61,10 @@ Ohai.plugin(:Hardware) do
   end
 
   def pci_devices
+    devices = {}
     device = nil
 
-    IO.popen(["lspci", "-Dkvmm"]).each_with_object(Mash.new) do |line, devices|
+    IO.popen(["lspci", "-Dkvmm"]).each do |line|
       if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
         device = {
           :slot => Regexp.last_match(1),
@@ -90,6 +91,24 @@ Ohai.plugin(:Hardware) do
         device = nil
       end
     end
+
+    IO.popen(["lspci", "-Dkvmmn"]).each do |line|
+      if line =~ /^Slot:\s+((\h{4}):(\h{2}):(\h{2}).(\h))\s*$/
+        device = devices[Regexp.last_match(1)]
+      elsif device && line =~ /^([A-Z]+):\s+(.*)\s*$/i
+        case Regexp.last_match(1)
+        when "Class" then device[:class_id] = Regexp.last_match(2)
+        when "Vendor" then device[:vendor_id] = Regexp.last_match(2)
+        when "Device" then device[:device_id] = Regexp.last_match(2)
+        when "SVendor" then device[:subsystem_vendor_id] = Regexp.last_match(2)
+        when "SDevice" then device[:subsystem_device_id] = Regexp.last_match(2)
+        end
+      elsif device && line =~ /^\s*$/
+        device = nil
+      end
+    end
+
+    devices
   end
 
   def network_devices
@@ -758,6 +777,21 @@ Ohai.plugin(:Hardware) do
     end
   end
 
+  def psu_devices
+    device = nil
+
+    IO.popen(["dmidecode", "-t", "39"]).each_with_object([]) do |line, devices|
+      if line =~ /^System Power Supply\s*$/
+        device = {}
+      elsif device && line =~ /^\s+([A-Z ]+):\s+(.*)\s*$/i
+        device[Regexp.last_match(1).tr(" ", "_").downcase.to_sym] = Regexp.last_match(2).strip
+      elsif device && line =~ /^\s*$/
+        devices << device
+        device = nil
+      end
+    end
+  end
+
   collect_data(:default) do
     hardware Mash.new
 
@@ -765,5 +799,6 @@ Ohai.plugin(:Hardware) do
     hardware[:network] = network_devices
     hardware[:memory] = memory_devices
     hardware[:disk] = disk_devices
+    hardware[:psu] = psu_devices
   end
 end