]> git.openstreetmap.org Git - chef.git/blobdiff - cookbooks/hardware/templates/default/ohai.rb.erb
Gather information on NVME disks
[chef.git] / cookbooks / hardware / templates / default / ohai.rb.erb
index 74c23f731653bc755cb271e129052d48c469dc4d..5854de82dac04c0cc6b50d4692e30baf0d161588 100644 (file)
@@ -1,3 +1,5 @@
+require "pathname"
+
 Ohai.plugin(:Hardware) do
   provides "hardware"
 
@@ -125,11 +127,12 @@ Ohai.plugin(:Hardware) do
     disk[:disks] = []
 
     find_direct_disks(disk)
+    find_nvme_disks(disk)
 
     find_hp_disks(disk) if File.exist?("/usr/sbin/hpssacli")
     find_megaraid_disks(disk) if File.exist?("/usr/sbin/megacli")
     find_mpt_disks(disk) if File.exist?("/usr/sbin/sas2ircu")
-    # aacraid
+    find_adaptec_disks(disk) if File.exist?("/usr/sbin/arcconf")
     find_areca_disks(disk) if File.exist?("/opt/areca/x86_64/cli64")
 
     find_md_arrays(disk)
@@ -150,7 +153,7 @@ Ohai.plugin(:Hardware) do
           block = Dir.glob("#{device}/block/*").first
           vendor = read_sysctl_file("#{device}/vendor")
           model = read_sysctl_file("#{device}/model")
-          size = read_sysctl_file("#{block}/size").to_i * 512
+          size = read_sysctl_file("#{block}/size").to_f / 2
 
           if vendor == "ATA" && model =~ /^(\S+)\s+(.*)$/
             vendor = Regexp.last_match(1)
@@ -171,11 +174,49 @@ Ohai.plugin(:Hardware) do
     end
   end
 
+  def find_nvme_disks(devices)
+    Dir.glob("/sys/class/misc/nvme*") do |device|
+      controller = {
+        :id => devices[:controllers].count,
+        :pci_slot => File.basename(Pathname.new("#{device}/device").realpath),
+        :arrays => [],
+        :disks => []
+      }
+
+      devices[:controllers] << controller
+
+      IO.popen(["lspci", "-Dkvmm", "-s", controller[:pci_slot]]).each do |line|
+        if line =~ /^SVendor:\s+(\S.*\S)\s*$/
+          controller[:vendor] = Regexp.last_match(1)
+        elsif line =~ /^SDevice:\s+(\S.*\S)\s*$/
+          controller[:model] = Regexp.last_match(1)
+        end
+      end
+
+      Dir.glob("#{device}/device/block/*").each do |block|
+        size = read_sysctl_file("#{block}/size").to_f / 2
+
+        disk = {
+          :id => devices[:disks].count,
+          :controller => controller[:id],
+          :device => "/dev/#{File.basename(block)}",
+          :vendor => controller[:vendor],
+          :model => controller[:model],
+          :size => format_disk_size(size),
+          :arrays => []
+        }
+
+        devices[:disks] << disk
+        controller[:disks] << disk[:id]
+      end
+    end
+  end
+
   def find_md_arrays(devices)
     array = nil
 
     File.new("/proc/mdstat", "r").each do |line|
-      if line =~ /^(md\d+) : active raid(\d+)((?: [a-z]+\d+\[\d+\](?:\([A-Z]\))*)+)$/
+      if line =~ /^(md\d+) : active raid(\d+)((?: (?:sd[a-z]|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*)+)$/
         array = {
           :id => devices[:arrays].count,
           :device => "/dev/#{Regexp.last_match(1)}",
@@ -183,7 +224,7 @@ Ohai.plugin(:Hardware) do
           :disks => []
         }
 
-        Regexp.last_match(3).scan(/ ([a-z]+)\d+\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
+        Regexp.last_match(3).scan(/ (sd[a-z]+|nvme\d+n\d+)\d*\[\d+\](?:\([A-Z]\))*/).flatten.each do |device|
           if disk = devices[:disks].find { |d| d[:device] == "/dev/#{device}" }
             disk[:arrays] << array[:id]
             array[:disks] << disk[:id]
@@ -191,7 +232,7 @@ Ohai.plugin(:Hardware) do
         end
 
         devices[:arrays] << array
-      elsif line =~ /^\s+(\d+) blocks/
+      elsif array && line =~ /^\s+(\d+) blocks/
         array[:size] = format_disk_size(Regexp.last_match(1).to_i)
       end
     end
@@ -488,6 +529,108 @@ Ohai.plugin(:Hardware) do
     end
   end
 
+  def find_adaptec_disks(devices)
+    controller_count = IO.popen(%w(arcconf getconfig 0)).first.scan(/^Controllers found: (\d+)$/).first.first.to_i
+
+    1.upto(controller_count).each do |controller_number|
+      controller = {
+        :id => devices[:controllers].count,
+        :number => controller_number,
+        :arrays => [],
+        :disks => []
+      }
+
+      devices[:controllers] << controller
+
+      arrays = []
+      disks = []
+
+      array = nil
+      disk = nil
+
+      IO.popen(["arcconf", "getconfig", controller_number.to_s]).each do |line|
+        if line =~ /^Logical device number (\d+)$/
+          array = {
+            :id => devices[:arrays].count,
+            :controller => controller[:id],
+            :number => Regexp.last_match(1).to_i,
+            :disks => []
+          }
+
+          devices[:arrays] << array
+          controller[:arrays] << array[:id]
+
+          arrays << array
+        elsif line =~ /^      Device #(\d+)$/
+          disk = nil
+        elsif line =~ /^         Device is a Hard drive$/
+          disk = {
+            :id => devices[:disks].count,
+            :controller => controller[:id],
+            :arrays => []
+          }
+
+          devices[:disks] << disk
+          controller[:disks] << disk[:id]
+
+          disks << disk
+        elsif disk && line =~ /^         Reported Channel,Device\(T:L\)\s*:\s+(\d+),(\d+)\(\d+:0\)\s*$/
+          disk[:channel_number] = Regexp.last_match(1)
+          disk[:device_number] = Regexp.last_match(1)
+        elsif disk && line =~ /^         (\S.*\S)\s*:\s+(\S.*\S)\s*$/
+          case Regexp.last_match(1)
+          when "Reported Location" then disk[:location] = Regexp.last_match(2)
+          when "Vendor" then disk[:vendor] = Regexp.last_match(2)
+          when "Model" then disk[:model] = Regexp.last_match(2)
+          when "Firmware" then disk[:firmware_version] = Regexp.last_match(2)
+          when "Serial" then disk[:serial_number] = Regexp.last_match(2)
+          when "World-wide name" then disk[:wwn] = Regexp.last_match(2)
+          when "Total Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
+          when "Size" then disk[:size] = memory_to_disk_size(Regexp.last_match(2))
+          end
+        elsif array && line =~ / Present \(Controller:\d+,((?:Connector|Enclosure):\d+,(?:Device|Slot):\d+)\) /
+          array[:disks] << Regexp.last_match(1).tr(":", " ").gsub(",", ", ")
+        elsif array && line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
+          case Regexp.last_match(1)
+          when "RAID level" then array[:raid_level] = Regexp.last_match(2)
+          when "Size" then array[:size] = memory_to_disk_size(Regexp.last_match(2))
+          end
+        elsif line =~ /^   (\S.*\S)\s*:\s+(\S.*\S)\s*$/
+          case Regexp.last_match(1)
+          when "Controller Model" then controller[:model] = Regexp.last_match(2)
+          when "Controller Serial Number" then controller[:serial_number] = Regexp.last_match(2)
+          when "Controller World Wide Name" then controller[:wwn] = Regexp.last_match(2)
+          when "BIOS" then controller[:bios_version] = Regexp.last_match(2)
+          when "Firmware" then controller[:firmware_version] = Regexp.last_match(2)
+          end
+        end
+      end
+
+      host = Dir.glob("/sys/class/scsi_host/host*").find do |host|
+        read_sysctl_file("#{host}/serial_number") == controller[:serial_number]
+      end
+
+      arrays.each do |array|
+        array_number = array[:number]
+        device = Dir.glob("#{host}/device/target*:0:#{array_number}/*:0:#{array_number}:0/block/*").first
+
+        array[:device] = "/dev/#{File.basename(device)}"
+
+        array[:disks].map! do |location|
+          disk = disks.find { |disk| disk[:location] == location }
+
+          device_number = disk[:device_number]
+          device = Dir.glob("#{host}/device/target*:1:#{device_number}/*:1:#{device_number}:0/scsi_generic/*").first
+
+          disk[:device] = "/dev/#{File.basename(device)}"
+
+          disk[:arrays] << array[:id]
+          disk[:id]
+        end
+      end
+    end
+  end
+
   def find_areca_disks(devices)
     controller = {
       :id => devices[:controllers].count,
@@ -534,13 +677,22 @@ Ohai.plugin(:Hardware) do
 
     arrays.each do |array|
       IO.popen(["/opt/areca/x86_64/cli64", "vsf", "info", "vol=#{array[:number]}"]).each do |line|
-        next unless line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
+        if line =~ /^SCSI Ch\/Id\/Lun\s+:\s+(\d+)\/(\d+)\/(\d+)\s*$/
+          pci_slot = controller[:pci_slot]
+          channel = Regexp.last_match(1).to_i
+          id = Regexp.last_match(2).to_i
+          lun = Regexp.last_match(3).to_i
 
-        case Regexp.last_match(1)
-        when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
-        when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
-        when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
-        when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
+          device = Dir.glob("/sys/bus/pci/devices/#{pci_slot}/host*/target*:0:0/0:#{channel}:#{id}:#{lun}/block/*").first
+
+          array[:device] = "/dev/#{File.basename(device)}"
+        elsif line =~ /^(\S.*\S)\s+:\s+(.*\S)\s*$/
+          case Regexp.last_match(1)
+          when "Volume Set Name" then array[:volume_set] = Regexp.last_match(2)
+          when "Raid Set Name" then array[:raid_set] = Regexp.last_match(2)
+          when "Volume Capacity" then array[:size] = format_disk_size(Regexp.last_match(2).to_f * 1000 * 1000)
+          when "Raid Level" then array[:raid_level] = Regexp.last_match(2).sub(/^Raid/, "")
+          end
         end
       end
     end