Sha256: 7eb873e3681baac4074f97b6258b6198c958632d865433f0e5e6c4903fd1a929

Contents?: true

Size: 1.94 KB

Versions: 19

Compression:

Stored size: 1.94 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Linux
      class Memory < BaseResolver
        @semaphore = Mutex.new
        @fact_list ||= {}
        @log = Facter::Log.new(self)
        class << self
          private

          def post_resolve(fact_name)
            @fact_list.fetch(fact_name) { read_meminfo_file(fact_name) }
          end

          def read_meminfo_file(fact_name)
            meminfo_output = Util::FileHelper.safe_read('/proc/meminfo', nil)
            return unless meminfo_output

            read_system(meminfo_output)
            read_swap(meminfo_output)

            @fact_list[fact_name]
          end

          def read_system(output)
            @fact_list[:total] = kilobytes_to_bytes(output.match(/MemTotal:\s+(\d+)\s/)[1])
            @fact_list[:memfree] = kilobytes_to_bytes(output.match(/MemFree:\s+(\d+)\s/)[1])
            @fact_list[:used_bytes] = compute_used(@fact_list[:total], @fact_list[:memfree])
            @fact_list[:capacity] = compute_capacity(@fact_list[:used_bytes], @fact_list[:total])
          end

          def read_swap(output)
            total = output.match(/SwapTotal:\s+(\d+)\s/)[1]
            return if total.to_i.zero?

            @fact_list[:swap_total] = kilobytes_to_bytes(total)
            @fact_list[:swap_free] = kilobytes_to_bytes(output.match(/SwapFree:\s+(\d+)\s/)[1])
            @fact_list[:swap_used_bytes] = compute_used(@fact_list[:swap_total], @fact_list[:swap_free])
            @fact_list[:swap_capacity] = compute_capacity(@fact_list[:swap_used_bytes], @fact_list[:swap_total])
          end

          def kilobytes_to_bytes(quantity)
            quantity.to_i * 1024
          end

          def compute_capacity(used, total)
            format('%<computed_capacity>.2f', computed_capacity: (used / total.to_f * 100)) + '%'
          end

          def compute_used(total, free)
            total - free
          end
        end
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
facter-4.0.35 lib/facter/resolvers/memory_resolver.rb
facter-4.0.34 lib/facter/resolvers/memory_resolver.rb
facter-4.0.33 lib/facter/resolvers/memory_resolver.rb
facter-4.0.32 lib/facter/resolvers/memory_resolver.rb
facter-4.0.31 lib/facter/resolvers/memory_resolver.rb
facter-4.0.30 lib/facter/resolvers/memory_resolver.rb
facter-4.0.29 lib/resolvers/memory_resolver.rb
facter-4.0.28 lib/resolvers/memory_resolver.rb
facter-4.0.26 lib/resolvers/memory_resolver.rb
facter-4.0.25 lib/resolvers/memory_resolver.rb
facter-4.0.24 lib/resolvers/memory_resolver.rb
facter-4.0.23 lib/resolvers/memory_resolver.rb
facter-4.0.22 lib/resolvers/memory_resolver.rb
facter-4.0.21 lib/resolvers/memory_resolver.rb
facter-4.0.20 lib/resolvers/memory_resolver.rb
facter-4.0.19 lib/resolvers/memory_resolver.rb
facter-4.0.18 lib/resolvers/memory_resolver.rb
facter-4.0.17 lib/resolvers/memory_resolver.rb
facter-4.0.16 lib/resolvers/memory_resolver.rb