Class: GodWatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/heaven/god_watcher.rb

Overview

:nodoc:all

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ GodWatcher

Returns a new instance of GodWatcher.



3
4
5
6
7
8
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
34
35
36
37
38
39
40
# File 'lib/heaven/god_watcher.rb', line 3

def initialize(name, options = {})
  @name     = name
  @start    = options[:start]
  @stop     = options[:stop]
  @restart  = options[:restart]
  @pid_file = options[:pid_file]
  @log_file = options[:log_file]
  @uid      = options[:uid]
  @gid      = options[:gid]

  @interval = options[:interval] || 15.seconds
  @grace    = options[:grace]     || 15.seconds

  @behaviors     = options[:behaviors] || []

  @max_memory = options[:max_memory] || 250.megabytes
  @max_cpu    = options[:max_cpu]    || 50.percent
  @max_times  = options[:max_times]  || 5

  @check_flapping = options[:check_flapping].nil? ? true : options[:check_flapping]
  @check_memory   = options[:check_memory].nil? ? true : options[:check_memory]
  @check_cpu      = options[:check_cpu].nil? ? true : options[:check_cpu]
  @check_url      = options[:check_url].nil? ? false : options[:check_url]

  @email_notifications   = options[:email_notifications] || false
  @email_from_address    = options[:email_from_address]
  @email_from_name       = options[:email_from_name] || "God"
  @email_delivery_method = options[:email_delivery_method] || :sendmail
  @email_to              = options[:email_to] || []

  @campfire_notifications = options[:campfire_notifications] || false
  @campfire_subdomain     = options[:campfire_subdomain]
  @campfire_token         = options[:campfire_token]
  @campfire_room          = options[:campfire_room]

  @url                    = options[:url]
  @url_timeout            = options[:url_timeout] || 10.seconds
end

Instance Method Details

#enforce(var, msg) ⇒ Object



166
167
168
# File 'lib/heaven/god_watcher.rb', line 166

def enforce(var, msg)
  raise HeavenConfigurationError.new(msg) if var.blank?
end

#set_god_defaultsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/heaven/god_watcher.rb', line 137

def set_god_defaults
  raise HeavenConfigurationError, "You must specify at least one email address" if @email_notifications == true && @to_emails.empty?

  if @email_notifications
    God::Contacts::Email.defaults do |d|
	d.from_email = @email_from_address
	d.from_name =  @email_from_name
	d.delivery_method = @email_delivery_method
    end

    @email_to.each do |email|
	God.contact(:email) do |c|
 c.name     = email
 c.to_email = email
	end
    end
  end

  if @campfire_notifications 
    God::Contacts::Campfire.defaults do |d|
	d.subdomain = @campfire_subdomain
	d.token     = @campfire_token
	d.room      = @campfire_room
	d.ssl       = true
    end
  end
  @god_defaults_set = true
end

#watchObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/heaven/god_watcher.rb', line 42

def watch
  set_god_defaults unless @god_defaults_set

  God.watch do |w|
    w.name     = @name
    w.interval = @interval
    w.start    = @start
    w.stop     = @stop
    w.restart  = @restart  if @restart # not mandatory, if we don't specify it god will do a stop and a start
    w.pid_file = @pid_file if @pid_file
    w.log      = @log_file if @log_file
    w.uid      = @uid if @uid
    w.gid      = @gid if @gid

    w.start_grace   = @grace
    w.restart_grace = @grace

    @behaviors.each { |b| w.behavior(b) }

    unless @check_url
	# determine the state on startup
	w.transition(:init, { true => :up, false => :start }) do |on|
 on.condition(:process_running) do |c|
   c.running = true
 end
	end

	# determine when process has finished starting
	w.transition([:start, :restart], :up) do |on|
 on.condition(:process_running) do |c|
   c.running = true
 end

 # failsafe
 on.condition(:tries) do |c|
   c.times = @max_times
   c.transition = :start
 end
	end

	# start if process is not running
	w.transition(:up, :start) do |on|
 on.condition(:process_exits)
	end
    end
    
    if @check_memory || @check_cpu
	# restart if memory or cpu is too high
	w.transition(:up, :restart) do |on|
 if @check_memory
   on.condition(:memory_usage) do |c|
     c.above    = @max_memory
     c.times    = @max_times
   end
 end

 if @check_cpu 
   on.condition(:cpu_usage) do |c|
     c.above    = @max_cpu
     c.times    = @max_times
   end
 end
	end
    end

    if @check_url
	u = URI.parse(@url)
	w.transition(:up, :restart) do |on|
 on.condition(:http_response_code) do |c|        
   c.host = u.host
   c.port = u.port
   c.path = u.path
   c.code_is_not = 200        
   c.timeout = @url_timeout
   c.times = @max_times
 end    
	end
    end

    if @check_flapping
	w.lifecycle do |check|
 check.condition(:flapping) do |c|
   c.to_state = [:start, :restart]
   c.times    = 5
   c.within   = 5.minutes
   c.transition = :unmonitored
   c.retry_in = 10.minutes
   c.retry_times = 5
   c.retry_within = 2.hours
 end
	end
    end
  end
end