Convert megaraid disk sizes to 1000 based units
authorTom Hughes <tom@compton.nu>
Sun, 29 Nov 2015 20:29:44 +0000 (20:29 +0000)
committerTom Hughes <tom@compton.nu>
Sun, 29 Nov 2015 20:29:44 +0000 (20:29 +0000)
cookbooks/hardware/templates/default/ohai.rb.erb

index 20a2f77..a5c2d75 100644 (file)
@@ -11,6 +11,14 @@ Ohai.plugin(:Hardware) do
   rescue Errno::ENOENT, Errno::EINVAL
   end
 
+  def parse_disk_size(size)
+    if size =~ /^(\d+(?:\.\d+))?\s*TB/i
+      sprintf "%dTB", $1.to_f * 2**40 / 1000000000000
+    elsif size =~ /^(\d+(?:\.\d+))?\s*GB/i
+      sprintf "%dGB", $1.to_f * 2**30 / 1000000000
+    end
+  end
+
   def pci_devices
     device = nil
 
@@ -240,7 +248,7 @@ Ohai.plugin(:Hardware) do
         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
         when "WWN" then disk[:wwn] = Regexp.last_match(2)
         when "PD Type" then disk[:interface] = Regexp.last_match(2)
-        when "Raw Size" then disk[:size] = Regexp.last_match(2).sub(/\s*\[.*\]$/, "")
+        when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
         end
       elsif array && line =~ /^(\S.*\S)\s*:\s+(\S.*)$/
@@ -270,7 +278,7 @@ Ohai.plugin(:Hardware) do
         when "Device Id" then disk[:smart_devlce] = "megaraid,#{Regexp.last_match(2)}"
         when "WWN" then disk[:wwn] = Regexp.last_match(2)
         when "PD Type" then disk[:interface] = Regexp.last_match(2)
-        when "Raw Size" then disk[:size] = Regexp.last_match(2).sub(/\s*\[.*\]$/, "")
+        when "Raw Size" then disk[:size] = parse_disk_size(Regexp.last_match(2).sub(/\s*\[.*\]$/, ""))
         when "Inquiry Data" then disk[:vendor], disk[:model], disk[:serial] = Regexp.last_match(2).split
         end
       end