Sha256: 7f101b4a8bfa3a36500ad8eb32e31cbfa4dd73a3c24ccb9b1371ead2a86c527f

Contents?: true

Size: 1.88 KB

Versions: 21

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

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

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

        def read_performance_information
          require "#{ROOT_DIR}/lib/resolvers/windows/ffi/memory_ffi"

          state_ptr = FFI::MemoryPointer.new(PerformanceInformation.size)
          if MemoryFFI::GetPerformanceInfo(state_ptr, state_ptr.size) == FFI::WIN32FALSE
            @log.debug 'Resolving memory facts failed'
            return
          end

          PerformanceInformation.new(state_ptr)
        end

        def calculate_memory
          state = read_performance_information
          return unless state

          total_bytes = state[:PhysicalTotal] * state[:PageSize]
          available_bytes = state[:PhysicalAvailable] * state[:PageSize]
          if total_bytes.zero? || available_bytes.zero?
            @log.debug 'Available or Total bytes are zero could not proceed further'
            return
          end

          { total_bytes: total_bytes, available_bytes: available_bytes, used_bytes: total_bytes - available_bytes }
        end

        def validate_info(fact_name)
          result = calculate_memory
          return unless result

          build_facts_list(result)
          @fact_list[fact_name]
        end

        def build_facts_list(result)
          @fact_list[:total_bytes] = result[:total_bytes]
          @fact_list[:available_bytes] = result[:available_bytes]
          @fact_list[:used_bytes] = result[:used_bytes]
          @fact_list[:capacity] = format('%<capacity>.2f',
                                         capacity: (result[:used_bytes] / result[:total_bytes].to_f * 100)) + '%'
        end
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
facter-4.0.29 lib/resolvers/windows/memory_resolver.rb
facter-4.0.28 lib/resolvers/windows/memory_resolver.rb
facter-4.0.26 lib/resolvers/windows/memory_resolver.rb
facter-4.0.25 lib/resolvers/windows/memory_resolver.rb
facter-4.0.24 lib/resolvers/windows/memory_resolver.rb
facter-4.0.23 lib/resolvers/windows/memory_resolver.rb
facter-4.0.22 lib/resolvers/windows/memory_resolver.rb
facter-4.0.21 lib/resolvers/windows/memory_resolver.rb
facter-4.0.20 lib/resolvers/windows/memory_resolver.rb
facter-4.0.19 lib/resolvers/windows/memory_resolver.rb
facter-4.0.18 lib/resolvers/windows/memory_resolver.rb
facter-4.0.17 lib/resolvers/windows/memory_resolver.rb
facter-4.0.16 lib/resolvers/windows/memory_resolver.rb
facter-4.0.15 lib/resolvers/windows/memory_resolver.rb
facter-4.0.14 lib/resolvers/windows/memory_resolver.rb
facter-4.0.13 lib/resolvers/windows/memory_resolver.rb
facter-4.0.12 lib/resolvers/windows/memory_resolver.rb
facter-4.0.11 lib/resolvers/windows/memory_resolver.rb
facter-4.0.10.pre lib/resolvers/windows/memory_resolver.rb
facter-4.0.9.pre lib/resolvers/windows/memory_resolver.rb