Class: God::Conditions::CpuUsage
- Inherits:
-
PollCondition
- Object
- Behavior
- God::Condition
- PollCondition
- God::Conditions::CpuUsage
- 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.
Parameters
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
-
#above ⇒ Object
Returns the value of attribute above.
-
#pid_file ⇒ Object
Returns the value of attribute pid_file.
-
#times ⇒ Object
Returns the value of attribute times.
Attributes inherited from PollCondition
Attributes inherited from God::Condition
#info, #notify, #phase, #transition
Attributes inherited from Behavior
Instance Method Summary collapse
-
#initialize ⇒ CpuUsage
constructor
A new instance of CpuUsage.
- #pid ⇒ Object
- #prepare ⇒ Object
- #reset ⇒ Object
- #test ⇒ Object
- #valid? ⇒ Boolean
Methods inherited from PollCondition
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
#initialize ⇒ CpuUsage
Returns a new instance of CpuUsage.
35 36 37 38 39 |
# File 'lib/god/conditions/cpu_usage.rb', line 35 def initialize super self.above = nil self.times = [1, 1] end |
Instance Attribute Details
#above ⇒ Object
Returns the value of attribute above.
33 34 35 |
# File 'lib/god/conditions/cpu_usage.rb', line 33 def above @above end |
#pid_file ⇒ Object
Returns the value of attribute pid_file.
33 34 35 |
# File 'lib/god/conditions/cpu_usage.rb', line 33 def pid_file @pid_file end |
#times ⇒ Object
Returns the value of attribute times.
33 34 35 |
# File 'lib/god/conditions/cpu_usage.rb', line 33 def times @times end |
Instance Method Details
#pid ⇒ Object
51 52 53 |
# File 'lib/god/conditions/cpu_usage.rb', line 51 def pid pid_file ? File.read(pid_file).strip.to_i : watch.pid end |
#prepare ⇒ Object
41 42 43 44 45 |
# File 'lib/god/conditions/cpu_usage.rb', line 41 def prepare self.times = [times, times] if times.is_a?(Integer) @timeline = Timeline.new(times[1]) end |
#reset ⇒ Object
47 48 49 |
# File 'lib/god/conditions/cpu_usage.rb', line 47 def reset @timeline.clear end |
#test ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/god/conditions/cpu_usage.rb', line 62 def test process = System::Process.new(pid) @timeline.push(process.percent_cpu) self.info = [] history = @timeline.map { |x| "#{x > above ? '*' : ''}#{x}%%" }.join(', ') if @timeline.count { |x| x > above } >= times.first self.info = "cpu out of bounds [#{history}]" true else false end end |
#valid? ⇒ Boolean
55 56 57 58 59 60 |
# File 'lib/god/conditions/cpu_usage.rb', line 55 def valid? valid = true valid &= complain("Attribute 'pid_file' must be specified", self) if pid_file.nil? && watch.pid_file.nil? valid &= complain("Attribute 'above' must be specified", self) if above.nil? valid end |