Module: Facter::Memory

### Home Classes Methods

### In Files

- lib/facter/util/memory.rb

### Methods

- ::mem_free
- ::mem_free_info
- ::mem_size
- ::mem_size_info
- ::meminfo_number
- ::parse_swap
- ::parse_swap_line
- ::scale_mem_free_value
- ::scale_mem_size_value
- ::scale_number
- ::scale_swap_value
- ::swap_free
- ::swap_info
- ::swap_size
- ::vmstat_darwin_find_free_memory
- ::vmstat_find_free_memory

### Files

- LICENSE
- Rakefile
- THANKS

### Class/Module Index

Quicksearch

- Facter
- Facter::Application
- Facter::Manufacturer
- Facter::Memory
- Facter::NetMask
- Facter::Util
- Facter::Util::CFPropertyList
- Facter::Util::CFPropertyList::CFArray
- Facter::Util::CFPropertyList::CFBoolean
- Facter::Util::CFPropertyList::CFData
- Facter::Util::CFPropertyList::CFDate
- Facter::Util::CFPropertyList::CFDictionary
- Facter::Util::CFPropertyList::CFInteger
- Facter::Util::CFPropertyList::CFReal
- Facter::Util::CFPropertyList::CFString
- Facter::Util::CFPropertyList::CFType
- Facter::Util::CFPropertyList::XML
- Facter::Util::Collection
- Facter::Util::CompositeLoader
- Facter::Util::Config
- Facter::Util::Confine
- Facter::Util::DirectoryLoader
- Facter::Util::DirectoryLoader::NoSuchDirectoryError
- Facter::Util::EC2
- Facter::Util::Fact
- Facter::Util::FileRead
- Facter::Util::IP
- Facter::Util::IP::Windows
- Facter::Util::IP::Windows::Bindings4
- Facter::Util::IP::Windows::Bindings6
- Facter::Util::Loader
- Facter::Util::Macaddress
- Facter::Util::Macaddress::Darwin
- Facter::Util::Macaddress::Windows
- Facter::Util::Macosx
- Facter::Util::MonkeyPatches
- Facter::Util::MonkeyPatches::Lines
- Facter::Util::NothingLoader
- Facter::Util::Parser
- Facter::Util::Parser::Base
- Facter::Util::Parser::JsonParser
- Facter::Util::Parser::KeyValuePairOutputFormat
- Facter::Util::Parser::NothingParser
- Facter::Util::Parser::PowershellParser
- Facter::Util::Parser::ScriptParser
- Facter::Util::Parser::TextParser
- Facter::Util::Parser::YamlParser
- Facter::Util::Processor
- Facter::Util::Registry
- Facter::Util::Resolution
- Facter::Util::SolarisZones
- Facter::Util::Uptime
- Facter::Util::Values
- Facter::Util::Virtual
- Facter::Util::Vlans
- Facter::Util::WMI
- Facter::Util::Xendomains
- Plist
- Plist::Emit
- Plist::Listener
- Plist::PArray
- Plist::PData
- Plist::PDate
- Plist::PDict
- Plist::PFalse
- Plist::PInteger
- Plist::PKey
- Plist::PList
- Plist::PReal
- Plist::PString
- Plist::PTag
- Plist::PTrue
- Plist::StreamParser
- CFFormatError
- CFPlistError
- CFTypeError
- IO
- Object
- String

No matching classes.

# Facter::Memory

memory.rb Support module for memory related facts

### Public Class Methods

mem_free(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 70
    def self.mem_free(kernel = Facter.value(:kernel))
      output = mem_free_info(kernel)
      scale_mem_free_value output, kernel
    end

mem_free_info(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 75
    def self.mem_free_info(kernel = Facter.value(:kernel))
      case kernel
      when /OpenBSD/, /SunOS/, /Dragonfly/
        vmstat_find_free_memory()
      when /FreeBSD/
        vmstat_find_free_memory(["-H"])
      when /Darwin/
        vmstat_darwin_find_free_memory()
      end
    end

mem_size(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 97
    def self.mem_size(kernel = Facter.value(:kernel))
      output = mem_size_info(kernel)
      scale_mem_size_value output, kernel
    end

mem_size_info(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 102
    def self.mem_size_info(kernel = Facter.value(:kernel))
      case kernel
      when /OpenBSD/
        Facter::Util::Resolution.exec("sysctl hw.physmem | cut -d'=' -f2")
      when /FreeBSD/
        Facter::Util::Resolution.exec("sysctl -n hw.physmem")
      when /Darwin/
        Facter::Util::Resolution.exec("sysctl -n hw.memsize")
      when /Dragonfly/
        Facter::Util::Resolution.exec("sysctl -n hw.physmem")
      end
    end

meminfo_number(tag) click to toggle source

    # File lib/facter/util/memory.rb, line 6
    def self.meminfo_number(tag)
      memsize = ""
      size = [0]
      File.readlines("/proc/meminfo").each do |l|
        size = $1.to_f if l =~ /^#{tag}:\s+(\d+)\s+\S+/
        if tag == "MemFree" &&
            l =~ /^(?:Buffers|Cached):\s+(\d+)\s+\S+/
          size += $1.to_f
        end
      end
      size / 1024.0
    end

parse_swap(output, kernel = Facter.value(:kernel), size_or_free = :size) click to toggle source

    # File lib/facter/util/memory.rb, line 149
    def self.parse_swap (output, kernel = Facter.value(:kernel), size_or_free = :size)
      value_in_mb = 0.0
      value = 0
      is_size = size_or_free == :size
      unless output.nil?
        output.each_line do |line|
          value += parse_swap_line(line, kernel, is_size)
        end
      end
      value_in_mb = scale_swap_value(value, kernel)
    end

parse_swap_line(line, kernel, is_size) click to toggle source

There is a lot of duplication here because of concern over being able to add new platforms in a reasonable manner. For all of these platforms the first regex corresponds to the swap size value and the second corresponds to the swap free value, but this may not always be the case. In Ruby 1.9.3 it is possible to give these names, but sadly 1.8.7 does not support this.

    # File lib/facter/util/memory.rb, line 167
    def self.parse_swap_line(line, kernel, is_size)
      case kernel
      when /AIX/
        if line =~ /^\/\S+\s.*\s+(\S+)MB\s+(\S+)MB/
          (is_size) ? $1.to_i : $2.to_i
        else
          0
        end
      when /OpenBSD/
        if line =~ /^total: (\d+)k bytes allocated = \d+k used, (\d+)k available$/
          (is_size) ? $1.to_i : $2.to_i
        else
          0
        end
      when /FreeBSD/
        if line =~ /\S+\s+(\d+)\s+\d+\s+(\d+)\s+\d+%$/
          (is_size) ? $1.to_i : $2.to_i
        else
          0
        end
      when /Darwin/
        if line =~ /total\s=\s(\S+)M\s+used\s=\s\S+M\s+free\s=\s(\S+)M\s/
          (is_size) ? $1.to_i : $2.to_i
        else
          0
        end
      when /SunOS/
        if line =~ /^\/\S+\s.*\s+(\d+)\s+(\d+)$/
          (is_size) ? $1.to_i : $2.to_i
        else
          0
        end
      end
    end

scale_mem_free_value(value, kernel) click to toggle source

    # File lib/facter/util/memory.rb, line 86
     def self.scale_mem_free_value (value, kernel)
       case kernel
       when /OpenBSD/, /FreeBSD/, /SunOS/, /Dragonfly/
         value.to_f / 1024.0
       when /Darwin/
         value.to_f / 1024.0 / 1024.0
       else
         value.to_f
       end
    end

scale_mem_size_value(value, kernel) click to toggle source

    # File lib/facter/util/memory.rb, line 115
    def self.scale_mem_size_value(value, kernel)
      case kernel
      when /OpenBSD/, /FreeBSD/, /Darwin/, /Dragonfly/
        value.to_f / 1024.0 / 1024.0
      else
        value.to_f
      end
    end

scale_number(size, multiplier) click to toggle source

    # File lib/facter/util/memory.rb, line 19
    def self.scale_number(size, multiplier)
      suffixes = ['', 'kB', 'MB', 'GB', 'TB']

      s = suffixes.shift
      while s != multiplier
        s = suffixes.shift
      end

      while size > 1024.0
        size /= 1024.0
        s = suffixes.shift
      end

      "%.2f %s" % [size, s]
    end

scale_swap_value(value, kernel) click to toggle source

    # File lib/facter/util/memory.rb, line 202
    def self.scale_swap_value(value, kernel)
      case kernel
      when /OpenBSD/, /FreeBSD/
        value.to_f / 1024.0
      when /SunOS/
        value.to_f / 2 / 1024.0
      else
        value.to_f
      end
    end

swap_free(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 129
    def self.swap_free(kernel = Facter.value(:kernel))
      output = swap_info(kernel)
      parse_swap output, kernel, :free if output
    end

swap_info(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 134
    def self.swap_info(kernel = Facter.value(:kernel))
      case kernel
      when /AIX/
        (Facter.value(:id) == "root") ? Facter::Util::Resolution.exec('swap -l') : nil
      when /OpenBSD/
        Facter::Util::Resolution.exec('swapctl -s')
      when /FreeBSD/
        Facter::Util::Resolution.exec('swapinfo -k')
      when /Darwin/
        Facter::Util::Resolution.exec('sysctl vm.swapusage')
      when /SunOS/
        Facter::Util::Resolution.exec('/usr/sbin/swap -l')
      end
    end

swap_size(kernel = Facter.value(:kernel)) click to toggle source

    # File lib/facter/util/memory.rb, line 124
    def self.swap_size(kernel = Facter.value(:kernel))
      output = swap_info(kernel)
      parse_swap output, kernel, :size if output
    end

vmstat_darwin_find_free_memory() click to toggle source

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.

    # File lib/facter/util/memory.rb, line 49
    def self.vmstat_darwin_find_free_memory()

      memfree = 0
      pagesize = 0
      memspecfree = 0

      vmstats = Facter::Util::Resolution.exec('vm_stat')
      vmstats.each_line 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
    end

vmstat_find_free_memory(args = []) click to toggle source

    # File lib/facter/util/memory.rb, line 35
    def self.vmstat_find_free_memory(args = [])
      cmd = 'vmstat'
      cmd += (' ' + args.join(' ')) unless args.empty?
      row = Facter::Util::Resolution.exec(cmd).split("\n")[-1]
      if row =~ /^\s*\d+\s*\d+\s*\d+\s*\d+\s*(\d+)/
        memfree = $1
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.