Sha256: db0597ee052b8da34ffd66227e45800f7629f04263356a4bc2788ac877362410

Contents?: true

Size: 1.32 KB

Versions: 23

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

module Facter
  module Resolvers
    module Macosx
      class SwapMemory < BaseResolver
        init_resolver

        class << self
          private

          def post_resolve(fact_name, _options)
            @fact_list.fetch(fact_name) { read_swap_memory(fact_name) }
          end

          def read_swap_memory(fact_name) # rubocop:disable Metrics/AbcSize
            output = Facter::Core::Execution.execute('sysctl -n vm.swapusage', logger: log)
            data = output.match(/^total = ([\d.]+)M  used = ([\d.]+)M  free = ([\d.]+)M  (\(encrypted\))$/)

            if data[1].to_f.positive?
              @fact_list[:total_bytes] = megabytes_to_bytes(data[1])
              @fact_list[:used_bytes] = megabytes_to_bytes(data[2])
              @fact_list[:available_bytes] = megabytes_to_bytes(data[3])
              @fact_list[:capacity] = compute_capacity(@fact_list[:used_bytes], @fact_list[:total_bytes])
              @fact_list[:encrypted] = data[4] == '(encrypted)'
            end

            @fact_list[fact_name]
          end

          def megabytes_to_bytes(quantity)
            (quantity.to_f * 1_048_576).to_i
          end

          def compute_capacity(used, total)
            "#{format('%<value>.2f', value: (used / total.to_f * 100))}%"
          end
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
facter-4.4.0 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.3.1 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.3.0 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.14 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.13 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.12 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.11 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.10 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.9 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.8 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.7 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.6 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.5 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.4 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.3 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.2 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.1 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.2.0 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.1.1 lib/facter/resolvers/macosx/swap_memory.rb
facter-4.1.0 lib/facter/resolvers/macosx/swap_memory.rb