diff options
author | Ben H <git@mumble.org.uk> | 2011-03-15 17:33:26 +1100 |
---|---|---|
committer | James Turnbull <james@lovedthanlost.net> | 2011-03-17 07:07:36 +1100 |
commit | d0f0f63f91a0eff72c1c528a6b5fb325bd9001d4 (patch) | |
tree | e60eb69f31394d21991acbb010601114e063b426 /lib | |
parent | fd4d26014b9c86d169713d9f4374cc4477f22616 (diff) | |
download | facter-d0f0f63f91a0eff72c1c528a6b5fb325bd9001d4.tar.gz facter-d0f0f63f91a0eff72c1c528a6b5fb325bd9001d4.tar.xz facter-d0f0f63f91a0eff72c1c528a6b5fb325bd9001d4.zip |
(#6327) Memory facts should be available on Mac Darwin
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/facter/memory.rb | 37 | ||||
-rw-r--r-- | 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 aa67702..9dd2e29 100644 --- a/lib/facter/memory.rb +++ b/lib/facter/memory.rb @@ -110,6 +110,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 |