Class: OsCtl::Cli::Top::MemInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/osctl/cli/top/meminfo.rb

Instance Method Summary collapse

Constructor Details

#initialize(file = '/proc/meminfo') ⇒ MemInfo

Returns a new instance of MemInfo



3
4
5
# File 'lib/osctl/cli/top/meminfo.rb', line 3

def initialize(file = '/proc/meminfo')
  @content = File.read(file)
end

Instance Method Details

#buffersObject



30
31
32
# File 'lib/osctl/cli/top/meminfo.rb', line 30

def buffers
  read_param('Buffers')
end

#cachedObject



15
16
17
# File 'lib/osctl/cli/top/meminfo.rb', line 15

def cached
  read_param('Cached')
end

#free(without_cache = true) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/osctl/cli/top/meminfo.rb', line 19

def free(without_cache = true)
  v = read_param('MemFree')

  if without_cache
    v + buffers + swap_cached

  else
    v
  end
end

#read_param(name) ⇒ Object (protected)



47
48
49
50
51
52
53
54
# File 'lib/osctl/cli/top/meminfo.rb', line 47

def read_param(name)
  if @content =~ /^#{Regexp.escape(name)}:\s*(\d+)\s+kB$/
    $1.to_i

  else
    nil
  end
end

#swap_cachedObject



38
39
40
# File 'lib/osctl/cli/top/meminfo.rb', line 38

def swap_cached
  read_param('SwapCached')
end

#swap_freeObject



42
43
44
# File 'lib/osctl/cli/top/meminfo.rb', line 42

def swap_free
  read_param('SwapFree')
end

#swap_totalObject



34
35
36
# File 'lib/osctl/cli/top/meminfo.rb', line 34

def swap_total
  read_param('SwapTotal')
end

#totalObject



7
8
9
# File 'lib/osctl/cli/top/meminfo.rb', line 7

def total
  read_param('MemTotal')
end

#used(without_cache = true) ⇒ Object



11
12
13
# File 'lib/osctl/cli/top/meminfo.rb', line 11

def used(without_cache = true)
  total - free(without_cache)
end