additional condition rdoc
[god.git] / lib / god / conditions / cpu_usage.rb
blobf87ae5db6117b371e18622f9cfec0085db2bf650
1 module God
2   module Conditions
3     
4     # Condition Symbol :cpu_usage
5     # Type: Poll
6     # 
7     # Trigger when the percent of CPU use of a process is above a specified limit.
8     # On multi-core systems, this number could conceivably be above 100.
9     #
10     # Paramaters
11     #   Required
12     #     +pid_file+ is the pid file of the process in question. Automatically
13     #                populated for Watches.
14     #     +above+ is the percent CPU above which to trigger the condition. You 
15     #             may use #percent to clarify this amount (see examples).
16     #
17     # Examples
18     #
19     # Trigger if the process is using more than 25 percent of the cpu (from a Watch):
20     #
21     #   on.condition(:cpu_usage) do |c|
22     #     c.above = 25.percent
23     #   end
24     #
25     # Non-Watch Tasks must specify a PID file:
26     #
27     #   on.condition(:cpu_usage) do |c|
28     #     c.above = 25.percent
29     #     c.pid_file = "/var/run/mongrel.3000.pid"
30     #   end
31     class CpuUsage < PollCondition
32       attr_accessor :above, :times
33     
34       def initialize
35         super
36         self.above = nil
37         self.times = [1, 1]
38       end
39       
40       def prepare
41         if self.times.kind_of?(Integer)
42           self.times = [self.times, self.times]
43         end
44         
45         @timeline = Timeline.new(self.times[1])
46       end
47       
48       def reset
49         @timeline.clear
50       end
51       
52       def valid?
53         valid = true
54         valid &= complain("Attribute 'pid_file' must be specified", self) if self.watch.pid_file.nil?
55         valid &= complain("Attribute 'above' must be specified", self) if self.above.nil?
56         valid
57       end
58       
59       def test
60         return false unless File.exist?(self.watch.pid_file)
61         
62         pid = File.read(self.watch.pid_file).strip
63         process = System::Process.new(pid)
64         @timeline.push(process.percent_cpu)
65         
66         history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}%%" }.join(", ") + "]"
67         
68         if @timeline.select { |x| x > self.above }.size >= self.times.first
69           self.info = "cpu out of bounds #{history}"
70           return true
71         else
72           self.info = "cpu within bounds #{history}"
73           return false
74         end
75       end
76     end
77     
78   end
79 end