Class: God::Conditions::CpuUsage

Inherits:
PollCondition show all
Defined in:
lib/god/conditions/cpu_usage.rb

Overview

Condition Symbol :cpu_usage Type: Poll

Trigger when the percent of CPU use of a process is above a specified limit. On multi-core systems, this number could conceivably be above 100.

Paramaters

Required
  +pid_file+ is the pid file of the process in question. Automatically
             populated for Watches.
  +above+ is the percent CPU above which to trigger the condition. You 
          may use #percent to clarify this amount (see examples).

Examples

Trigger if the process is using more than 25 percent of the cpu (from a Watch):

on.condition(:cpu_usage) do |c|
  c.above = 25.percent
end

Non-Watch Tasks must specify a PID file:

on.condition(:cpu_usage) do |c|
  c.above = 25.percent
  c.pid_file = "/var/run/mongrel.3000.pid"
end

Instance Attribute Summary collapse

Attributes inherited from PollCondition

#interval

Attributes inherited from God::Condition

#info, #notify, #phase, #transition

Attributes inherited from Behavior

#watch

Instance Method Summary collapse

Methods inherited from PollCondition

#after, #before

Methods inherited from God::Condition

#friendly_name, generate, valid?

Methods inherited from Behavior

#after_restart, #after_start, #after_stop, #before_restart, #before_start, #before_stop, #friendly_name, generate

Methods included from God::Configurable

#base_name, complain, #complain, #friendly_name

Constructor Details

#initializeCpuUsage

Returns a new instance of CpuUsage.



34
35
36
37
38
# File 'lib/god/conditions/cpu_usage.rb', line 34

def initialize
  super
  self.above = nil
  self.times = [1, 1]
end

Instance Attribute Details

#aboveObject

Returns the value of attribute above.



32
33
34
# File 'lib/god/conditions/cpu_usage.rb', line 32

def above
  @above
end

#pid_fileObject

Returns the value of attribute pid_file.



32
33
34
# File 'lib/god/conditions/cpu_usage.rb', line 32

def pid_file
  @pid_file
end

#timesObject

Returns the value of attribute times.



32
33
34
# File 'lib/god/conditions/cpu_usage.rb', line 32

def times
  @times
end

Instance Method Details

#pidObject



52
53
54
# File 'lib/god/conditions/cpu_usage.rb', line 52

def pid
  self.pid_file ? File.read(self.pid_file).strip.to_i : self.watch.pid
end

#prepareObject



40
41
42
43
44
45
46
# File 'lib/god/conditions/cpu_usage.rb', line 40

def prepare
  if self.times.kind_of?(Integer)
    self.times = [self.times, self.times]
  end
  
  @timeline = Timeline.new(self.times[1])
end

#resetObject



48
49
50
# File 'lib/god/conditions/cpu_usage.rb', line 48

def reset
  @timeline.clear
end

#testObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/god/conditions/cpu_usage.rb', line 63

def test
  process = System::Process.new(self.pid)
  @timeline.push(process.percent_cpu)
  
  history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}%%" }.join(", ") + "]"
  
  if @timeline.select { |x| x > self.above }.size >= self.times.first
    self.info = "cpu out of bounds #{history}"
    return true
  else
    self.info = "cpu within bounds #{history}"
    return false
  end
end

#valid?Boolean

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/god/conditions/cpu_usage.rb', line 56

def valid?
  valid = true
  valid &= complain("Attribute 'pid_file' must be specified", self) if self.pid_file.nil? && self.watch.pid_file.nil?
  valid &= complain("Attribute 'above' must be specified", self) if self.above.nil?
  valid
end