Class: RorVsWild::Metrics::Cpu

Inherits:
Object
  • Object
show all
Defined in:
lib/rorvswild/metrics/cpu.rb

Defined Under Namespace

Classes: Stat

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCpu

Returns a new instance of Cpu.



9
10
11
# File 'lib/rorvswild/metrics/cpu.rb', line 9

def initialize
  @old_stat = Stat.read
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



7
8
9
# File 'lib/rorvswild/metrics/cpu.rb', line 7

def count
  @count
end

#idleObject (readonly)

Returns the value of attribute idle.



6
7
8
# File 'lib/rorvswild/metrics/cpu.rb', line 6

def idle
  @idle
end

#load_averageObject (readonly)

Returns the value of attribute load_average.



7
8
9
# File 'lib/rorvswild/metrics/cpu.rb', line 7

def load_average
  @load_average
end

#stolenObject (readonly)

Returns the value of attribute stolen.



6
7
8
# File 'lib/rorvswild/metrics/cpu.rb', line 6

def stolen
  @stolen
end

#systemObject (readonly)

Returns the value of attribute system.



6
7
8
# File 'lib/rorvswild/metrics/cpu.rb', line 6

def system
  @system
end

#userObject (readonly)

Returns the value of attribute user.



6
7
8
# File 'lib/rorvswild/metrics/cpu.rb', line 6

def user
  @user
end

#waitingObject (readonly)

Returns the value of attribute waiting.



6
7
8
# File 'lib/rorvswild/metrics/cpu.rb', line 6

def waiting
  @waiting
end

Instance Method Details

#updateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rorvswild/metrics/cpu.rb', line 13

def update
  if @old_stat && (new_stat = Stat.read)
    if (total = new_stat.total - @old_stat.total) > 0
      @user = (new_stat.user - @old_stat.user) * 100 / total
      @system = (new_stat.system - @old_stat.system) * 100 / total
      @idle = (new_stat.idle - @old_stat.idle) * 100 / total
      @waiting = (new_stat.iowait - @old_stat.iowait) * 100 / total
      @stolen = (new_stat.steal - @old_stat.steal) * 100 / total
      @old_stat = new_stat
    end
  end
  @load_average = File.read("/proc/loadavg").to_f if File.readable?("/proc/loadavg")
  @count = `nproc`.to_i rescue nil
end