Class: Jah::Mem

Inherits:
Object
  • Object
show all
Includes:
Command
Defined in:
lib/jah/commands/mem.rb

Constant Summary

Constants included from Command

Command::COMM

Class Method Summary collapse

Methods included from Command

find, included

Class Method Details

.readObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/jah/commands/mem.rb', line 14

def read
  mem_info = {}
  `cat /proc/meminfo`.each do |line|
    _, key, value = *line.match(/^(\w+):\s+(\d+)\s/)
    mem_info[key] = value.to_i
  end

  # memory info is empty - operating system may not support it (why doesn't an exception get raised earlier on mac osx?)
  raise "No such file or directory" if mem_info.empty?
  @res = {}
  @res[:total] = total = mem_info['MemTotal'] / 1024
  @res[:free] = free = (mem_info['MemFree'] + mem_info['Buffers'] + mem_info['Cached']) / 1024
  @res[:used] = total - free
  @res[:percent] = (free / total.to_f * 100).to_i

  @res[:swap_total] = stotal = mem_info['SwapTotal'] / 1024
  @res[:swap_free] = sfree = mem_info['SwapFree'] / 1024
  # @res[:swap_used] = stotal - sfree
  # unless stotal == 0
  #   @swap_percent = (@swap_used / @swap_total.to_f * 100).to_i
  # end
rescue Exception => e
  if e.message =~ /No such file or directory/
    puts "/proc/meminfo not found.. trying top!"
    top = `top -l 1`.to_a[5].split.map!{|m| m[0..-2].to_i}.reject(&:zero?)
    @res[:used], @res[:free] = top[3,4]
    @res[:total] = @res[:used] + @res[:free]
    @res[:percent] = (@res[:free] / @res[:total].to_f * 100).to_i
  else
    raise e
  end
end

.snapObject



9
10
11
12
# File 'lib/jah/commands/mem.rb', line 9

def snap
  read
  "Free: #{@res[:free]} Used: #{@res[:used]} #{@res[:percent]}%"
end