There's no easy defined way of getting memory information from the command
line.

Copying mainly the OpenBSD facts, but having to pull in memory free from
the vm_stat utility, and parsing the weird vm.swapusage sysctl value for
swap. Parsing "top -l 1 -n 0" seemed an option, but that took over a
second to run each time, so this appears cheaper.

Signed-off-by: Ben Hughes <b...@puppetlabs.com>
---
 lib/facter/memory.rb      |   37 +++++++++++++++++++++++++++++++++++++
 lib/facter/util/memory.rb |   31 +++++++++++++++++++++++++++++++
 2 files changed, 68 insertions(+), 0 deletions(-)

diff --git a/lib/facter/memory.rb b/lib/facter/memory.rb
index 5c85a78..01cab38 100644
--- a/lib/facter/memory.rb
+++ b/lib/facter/memory.rb
@@ -97,6 +97,43 @@ if Facter.value(:kernel) == "OpenBSD"
     end
 end
 
+if Facter.value(:kernel) == "Darwin"
+    swap = Facter::Util::Resolution.exec('sysctl vm.swapusage')
+    swapfree, swaptotal = 0, 0
+    unless swap.empty?
+        # Parse the line:
+        # vm.swapusage: total = 128.00M  used = 0.37M  free = 127.63M  
(encrypted)
+        if swap =~ /total\s=\s(\S+)\s+used\s=\s(\S+)\s+free\s=\s(\S+)\s/
+            swaptotal += $1.to_i
+            swapfree  += $3.to_i
+        end
+    end
+
+    Facter.add("SwapSize") do
+        confine :kernel => :Darwin
+        setcode do
+            Facter::Memory.scale_number(swaptotal.to_f,"MB")
+        end
+    end
+
+    Facter.add("SwapFree") do
+        confine :kernel => :Darwin
+        setcode do
+            Facter::Memory.scale_number(swapfree.to_f,"MB")
+        end
+    end
+
+    Facter::Memory.vmstat_darwin_find_free_memory()
+
+    Facter.add("MemoryTotal") do
+        confine :kernel => :Darwin
+        memtotal = Facter::Util::Resolution.exec("sysctl hw.memsize | cut 
-d':' -f2")
+        setcode do
+            Facter::Memory.scale_number(memtotal.to_f,"")
+        end
+    end
+end
+
 if Facter.value(:kernel) == "SunOS"
     swap = Facter::Util::Resolution.exec('/usr/sbin/swap -l')
     swapfree, swaptotal = 0, 0
diff --git a/lib/facter/util/memory.rb b/lib/facter/util/memory.rb
index 43abec6..029b117 100644
--- a/lib/facter/util/memory.rb
+++ b/lib/facter/util/memory.rb
@@ -62,5 +62,36 @@ module Facter::Memory
             end
         end
     end
+
+    # Darwin had to be different. It's generally opaque with how much RAM it is
+    # using, and this figure could be improved upon too I fear.
+    # Parses the output of "vm_stat", takes the pages free & pages speculative
+    # and multiples that by the page size (also given in output). Ties in with
+    # what activity monitor outputs for free memory.
+    def self.vmstat_darwin_find_free_memory()
+
+        memfree = 0
+        pagesize = 0
+        memspecfree = 0
+
+        vmstats = Facter::Util::Resolution.exec('vm_stat')
+        vmstats.each do |vmline|
+          case
+            when vmline =~ /page\ssize\sof\s(\d+)\sbytes/
+              pagesize = $1.to_i
+            when vmline =~ /^Pages\sfree:\s+(\d+)\./
+              memfree = $1.to_i
+            when vmline =~ /^Pages\sspeculative:\s+(\d+)\./
+              memspecfree = $1.to_i
+          end
+        end
+
+        freemem = ( memfree + memspecfree ) * pagesize
+        Facter.add("MemoryFree") do
+          setcode do
+            Facter::Memory.scale_number(freemem.to_f, "")
+          end
+        end
+    end
 end
 
-- 
1.7.4.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to puppet-dev@googlegroups.com.
To unsubscribe from this group, send email to 
puppet-dev+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to