class RorVsWild::Metrics::Memory

Constants

BUFFERS
CACHED
MEM_AVAILABLE
MEM_FREE
MEM_TOTAL
PROC_MEMINFO
SWAP_FREE
SWAP_TOTAL

Attributes

ram_available[R]
ram_buffers[R]
ram_cached[R]
ram_free[R]
ram_total[R]
storage_total[R]
storage_used[R]
swap_free[R]
swap_total[R]

Public Instance Methods

ram_used() click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 8
def ram_used
  ram_total - ram_available
end
swap_used() click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 12
def swap_used
  swap_total - swap_free
end
update() click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 25
def update
  return unless info = read_meminfo
  @ram_total = convert_to_bytes(info[MEM_TOTAL])
  @ram_free = convert_to_bytes(info[MEM_FREE])
  @ram_available = convert_to_bytes(info[MEM_AVAILABLE])
  @ram_buffers = convert_to_bytes(info[BUFFERS])
  @ram_cached = convert_to_bytes(info[CACHED])
  @swap_total = convert_to_bytes(info[SWAP_TOTAL])
  @swap_free = convert_to_bytes(info[SWAP_FREE])
end

Private Instance Methods

convert_to_bytes(string) click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 51
def convert_to_bytes(string)
  value, unit = string.split
  value.to_i * units[unit.downcase]
end
read_meminfo() click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 42
def read_meminfo
  return unless File.readable?(PROC_MEMINFO)
  File.read(PROC_MEMINFO).split("\n").reduce({}) do |hash, line|
    name, value = line.split(":")
    hash[name] = value.strip
    hash
  end
end
units() click to toggle source
# File lib/rorvswild/metrics/memory.rb, line 38
def units
  @units ||= {"kb" => 1000, "mb" => 1000 * 1000, "gb" => 1000 * 1000 * 1000}.freeze
end