class ElasticAPM::Metrics::CpuMem::Linux::Meminfo

@api private

def read!

rubocop:disable Metrics/CyclomaticComplexity
rubocop:disable Metrics/PerceivedComplexity
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def read!
  # rubocop:disable Style/RescueModifier
  @page_size = `getconf PAGESIZE`.chomp.to_i rescue 4096
  # rubocop:enable Style/RescueModifier
  info =
    IO.readlines('/proc/meminfo')
      .lazy
      .each_with_object({}) do |line, hsh|
        if line.start_with?('MemTotal:')
          hsh[:total] = line.split[1].to_i * 1024
        elsif line.start_with?('MemAvailable:')
          hsh[:available] = line.split[1].to_i * 1024
        elsif line.start_with?('MemFree:')
          hsh[:free] = line.split[1].to_i * 1024
        elsif line.start_with?('Buffers:')
          hsh[:buffers] = line.split[1].to_i * 1024
        elsif line.start_with?('Cached:')
          hsh[:cached] = line.split[1].to_i * 1024
        end
        break hsh if hsh[:total] && hsh[:available]
      end
  @total = info[:total]
  @available =
    info[:available] || info[:free] + info[:buffers] + info[:cached]
  self
end