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
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
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
|