Class: CpuMemoryStats::Bsd

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

Instance Method Summary collapse

Instance Method Details

#cpuObject



39
40
41
# File 'lib/modules/bsd.rb', line 39

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
# File 'lib/modules/bsd.rb', line 9

def get_stats

  output = { :cpu => {} }

  output[:load_avg] = `sysctl -n vm.loadavg`.gsub(/\{|\}/,"").strip.split(" ").collect{|i| i.strip.to_f.round(2)}

  cpu = `sysctl -n kern.cp_time`.strip.split(" ").collect{|i| i.strip.to_i}
  cpu_start = Hash[[:user, :nice, :system, :interupt, :idle].zip(cpu)]
  sleep(0.01)
  cpu = `sysctl -n kern.cp_time`.strip.split(" ").collect{|i| i.strip.to_i}
  cpu_end = Hash[[:user, :nice, :system, :interupt, :idle].zip(cpu)]

  cpu_end.each do |key, value|
    output[:cpu][key] = value - cpu_start[key]
  end

  output[:memory] = {
    :wired => `sysctl -n vm.stats.vm.v_wire_count`.to_i,
    :active => `sysctl -n vm.stats.vm.v_active_count`.to_i,
    :inactive => `sysctl -n vm.stats.vm.v_inactive_count`.to_i,
    :used => 0,       #fix missing columns
    :free => `sysctl -n vm.stats.vm.v_free_count`.to_i
  }
  output
end

#load_avgObject



43
44
45
# File 'lib/modules/bsd.rb', line 43

def load_avg
  stats[:load_avg]
end

#memoryObject



47
48
49
# File 'lib/modules/bsd.rb', line 47

def memory
  stats[:memory]
end

#statsObject



35
36
37
# File 'lib/modules/bsd.rb', line 35

def stats
  @stats ||= get_stats
end