Class: CpuMemoryStats::Mac

Inherits:
Object
  • Object
show all
Defined in:
lib/modules/mac.rb

Instance Method Summary collapse

Instance Method Details

#cpuObject



40
41
42
# File 'lib/modules/mac.rb', line 40

def cpu
  stats[:cpu]
end

#get_statsObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/modules/mac.rb', line 9

def get_stats
  top = `top -F -R -l 1 -n 0`
  
  output = {}
  output[:load_avg] = top.scan(/Load Avg:(.+),(.+),(.+)/i).flatten.collect{|i| i.strip.to_f.round(2)}
  
  cpu = top.scan(/CPU usage:\s+(.+)user,(.+)sys,(.+)idle/i).flatten.collect{|i| i.gsub("%","").strip.to_f.round(2)}
  output[:cpu] = Hash[[:user, :system, :idle].zip(cpu)]
  #fix missing columns
  output[:cpu][:nice] = 0
  output[:cpu][:interupt] = 0

  memory = top.scan(/PhysMem:\s+(.+)wired,(.+)active,(.+)inactive,(.+)used,(.+)free/i).flatten.collect{|i| i.strip}
  mem = Hash[[:wired, :active, :inactive, :used, :free].zip(memory)]
  output[:memory] = mem.inject({}) do |o,v|
    num = v.last.to_i
    o[v.first] = case v.last
    when /G/ then num*1024*1024*1024
    when /M/ then num*1024*1024
    when /K/ then num*1024
    else num end
    o
  end

  output
end

#load_avgObject



44
45
46
# File 'lib/modules/mac.rb', line 44

def load_avg
  stats[:load_avg]
end

#memoryObject



48
49
50
# File 'lib/modules/mac.rb', line 48

def memory
  stats[:memory]
end

#statsObject



36
37
38
# File 'lib/modules/mac.rb', line 36

def stats
  @stats ||= get_stats
end