Standard units provided are B and MB.
Signed-off-by: Dominic Cleal <dcl...@redhat.com>
---
Local-branch: tickets/master/2066
lib/facter/memory.rb | 70 ++++++++++++++------------------------------
lib/facter/util/memory.rb | 57 ++++++++++++++++++++++++++++++++++++
2 files changed, 79 insertions(+), 48 deletions(-)
diff --git a/lib/facter/memory.rb b/lib/facter/memory.rb
index 06640e6..992ed40 100644
--- a/lib/facter/memory.rb
+++ b/lib/facter/memory.rb
@@ -7,16 +7,14 @@
#
require 'facter/util/memory'
-{ :MemorySize => "MemTotal",
- :MemoryFree => "MemFree",
- :SwapSize => "SwapTotal",
- :SwapFree => "SwapFree"
-}.each do |fact, name|
- Facter.add(fact) do
- confine :kernel => :linux
- setcode do
- Facter::Memory.meminfo_number(name)
- end
+if Facter.value(:kernel) == "Linux"
+ { :MemorySize => "MemTotal",
+ :MemoryFree => "MemFree",
+ :SwapSize => "SwapTotal",
+ :SwapFree => "SwapFree"
+ }.each do |fact, name|
+ meminfo = Facter::Memory.meminfo_number(name)
+ Facter::Memory::add_memfacts(fact, meminfo, :linux)
end
end
@@ -30,19 +28,11 @@ if Facter.value(:kernel) == "AIX" and Facter.value(:id) == "root"
end
end
- Facter.add("SwapSize") do
- confine :kernel => :aix
- setcode do
- Facter::Memory.scale_number(swaptotal.to_f,"MB")
- end
- end
+ meminfo = Facter::Memory.scale_number(swaptotal.to_f,"MB")
+ Facter::Memory::add_memfacts("SwapSize", meminfo, :aix)
- Facter.add("SwapFree") do
- confine :kernel => :aix
- setcode do
- Facter::Memory.scale_number(swapfree.to_f,"MB")
- end
- end
+ meminfo = Facter::Memory.scale_number(swapfree.to_f,"MB")
+ Facter::Memory::add_memfacts("SwapFree", meminfo, :aix)
end
if Facter.value(:kernel) == "OpenBSD"
@@ -55,33 +45,17 @@ if Facter.value(:kernel) == "OpenBSD"
end
end
- Facter.add("SwapSize") do
- confine :kernel => :openbsd
- setcode do
- Facter::Memory.scale_number(swaptotal.to_f,"kB")
- end
- end
+ meminfo = Facter::Memory.scale_number(swaptotal.to_f,"kB")
+ Facter::Memory::add_memfacts("SwapSize", meminfo, :openbsd)
- Facter.add("SwapFree") do
- confine :kernel => :openbsd
- setcode do
- Facter::Memory.scale_number(swapfree.to_f,"kB")
- end
- end
+ meminfo = Facter::Memory.scale_number(swapfree.to_f,"kB")
+ Facter::Memory::add_memfacts("SwapFree", meminfo, :openbsd)
- Facter.add("MemoryFree") do
- confine :kernel => :openbsd
- memfree = Facter::Util::Resolution.exec("vmstat | tail -n 1 | awk '{ print $5 }'")
- setcode do
- Facter::Memory.scale_number(memfree.to_f,"kB")
- end
- end
+ memfree = Facter::Util::Resolution.exec("vmstat | tail -n 1 | awk '{ print $5 }'")
+ meminfo = Facter::Memory.scale_number(memfree.to_f,"kB")
+ Facter::Memory::add_memfacts("MemoryFree", meminfo, :openbsd)
- Facter.add("MemoryTotal") do
- confine :kernel => :openbsd
- memtotal = Facter::Util::Resolution.exec("sysctl hw.physmem | cut -d'=' -f2")
- setcode do
- Facter::Memory.scale_number(memtotal.to_f,"")
- end
- end
+ memtotal = Facter::Util::Resolution.exec("sysctl hw.physmem | cut -d'=' -f2")
+ meminfo = Facter::Memory.scale_number(memfree.to_f,"")
+ Facter::Memory::add_memfacts("MemoryTotal", meminfo, :openbsd)
end
diff --git a/lib/facter/util/memory.rb b/lib/facter/util/memory.rb
index 2004491..672c452 100644
--- a/lib/facter/util/memory.rb
+++ b/lib/facter/util/memory.rb
@@ -16,6 +16,28 @@
module Facter::Memory
require 'thread'
+ # Stores all units from meminfo as fact_SUFFIX
+ def self.add_memfacts(fact, meminfo, kernel = nil)
+ best = meminfo[:best]
+ Facter.add(fact) do
+ confine :kernel => kernel if kernel
+ setcode do
+ best
+ end
+ end
+
+ # Load in values converted to other units
+ meminfo.each do |memunit,memsize|
+ next if memunit == :best
+ Facter.add("%s_%s" % [fact, memunit]) do
+ confine :kernel => kernel if kernel
+ setcode do
+ memsize
+ end
+ end
+ end
+ end
+
def self.meminfo_number(tag)
memsize = ""
Thread::exclusive do
@@ -35,7 +57,17 @@ module Facter::Memory
memsize
end
+ # Converts an input number into a range of standard units and the most
+ # appropriate unit (:best). Returns a hash similar to:
+ # { :best => "123 MB", "kB" => 125952, "MB" => 123 }
def self.scale_number(size, multiplier)
+ units = scale_number_standards(size, multiplier)
+ units[:best] = scale_number_best(size, multiplier)
+ units
+ end
+
+ # Converts an input number into the most appropriate unit
+ def self.scale_number_best(size, multiplier)
suffixes = ['', 'kB', 'MB', 'GB', 'TB']
s = suffixes.shift
@@ -50,5 +82,30 @@ module Facter::Memory
return "%.2f %s" % [size, s]
end
+
+ # Converts one input number into a range of standard units
+ def self.scale_number_standards(size, multiplier, units = ['B', 'MB'])
+ suffixes = ['B', 'kB', 'MB', 'GB', 'TB']
+ standards = {}
+
+ units.each do |u|
+ idx = suffixes.index(multiplier) # our input
+ uidx = suffixes.index(u) # our target
+ convsize = size
+
+ while uidx < idx
+ convsize *= 1024.0
+ idx -= 1
+ end
+
+ while uidx > idx
+ convsize /= 1024.0
+ idx += 1
+ end
+
+ standards[u] = "%.2f" % convsize
+ end
+ standards
+ end
end
--
1.7.3.2
Please note that this conflicts with the patch I submitted yesterday for
#1423 (Solaris memory facts), but I thought it'd be good to get it out
there for review anyway. I'm happy to update and resubmit this if/when
the other is merged or vice versa.
--
Dominic Cleal
Red Hat Consulting
m: +44 (0)7818 512168
I'd rather not make this change right now as I think we want a
consistent approach with in Facter to handle this. Lets use this patch
as a way to spark discussion but apply the 1423 patch right now.
Paul
Sure, hopefully it serves to show the result I'm looking for as a user.
Are you thinking to include this in the 2.0 redesign?
Yeah the idea is to have the internal representation be something sane
and be able to render to human readable units in the display/export
layer.
Paul