Class: OsCtl::Lib::MemInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/meminfo.rb

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MemInfo.



3
4
5
6
# File 'lib/libosctl/meminfo.rb', line 3

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

Instance Method Details

#buffersObject



31
32
33
# File 'lib/libosctl/meminfo.rb', line 31

def buffers
  read_param('Buffers')
end

#cachedObject



16
17
18
# File 'lib/libosctl/meminfo.rb', line 16

def cached
  read_param('Cached')
end

#free(without_cache = true) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/libosctl/meminfo.rb', line 20

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)



53
54
55
56
57
58
59
60
61
# File 'lib/libosctl/meminfo.rb', line 53

def read_param(name)
  if @values.has_key?(name)
    @values[name]

  elsif @content =~ /^#{Regexp.escape(name)}:\s*(\d+)\s+kB$/
    @values[name] = ::Regexp.last_match(1).to_i

  end
end

#swap_cachedObject



43
44
45
# File 'lib/libosctl/meminfo.rb', line 43

def swap_cached
  read_param('SwapCached')
end

#swap_freeObject



47
48
49
# File 'lib/libosctl/meminfo.rb', line 47

def swap_free
  read_param('SwapFree')
end

#swap_totalObject



35
36
37
# File 'lib/libosctl/meminfo.rb', line 35

def swap_total
  read_param('SwapTotal')
end

#swap_usedObject



39
40
41
# File 'lib/libosctl/meminfo.rb', line 39

def swap_used
  swap_total - swap_free
end

#totalObject



8
9
10
# File 'lib/libosctl/meminfo.rb', line 8

def total
  read_param('MemTotal')
end

#used(without_cache = true) ⇒ Object



12
13
14
# File 'lib/libosctl/meminfo.rb', line 12

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