Class: ProcessWanker::EventHookContext

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/events.rb

Constant Summary

Constants included from Log

Log::DEBUG, Log::ERROR, Log::INFO, Log::WARN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Log

debug, error, info, log, set_level, warn

Instance Attribute Details

#eventObject

Returns the value of attribute event.



99
100
101
# File 'lib/events.rb', line 99

def event
  @event
end

#serviceObject

Returns the value of attribute service.



98
99
100
# File 'lib/events.rb', line 98

def service
  @service
end

#should_stop_hooksObject

Returns the value of attribute should_stop_hooks.



100
101
102
# File 'lib/events.rb', line 100

def should_stop_hooks
  @should_stop_hooks
end

Instance Method Details

#emailObject

helpers



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
136
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
165
166
167
168
169
170
171
172
# File 'lib/events.rb', line 108

def email()
	
	# find smtp config
	smtp_config=service.config_node.find_attributes("smtp").first
	if(!smtp_config)
		Log::error("attempting to send an email, but no SMTP configuration found")
		return
	end
	from_user,from_domain=smtp_config.from_addr.split("@")
	
	# construct email
	@email_addresses=smtp_config.to_addrs.clone
	@email_subject="ProcessWanker event: #{@event.type} from #{event.service_name}"
	@email_body="At #{@event.time}, #{event.service_name} triggered a <#{@event.type}> event\n"
	@email_body << "The current state is #{@event.service_state}.\n"
	@email_body << "Service stats:\n"
	@event.service_stats.each do |k,v|
		@email_body << "  #{sprintf("%-20s",k)} : #{v}\n"
	end
	@email_body << "\n"
	
	# allow configuration to override some values
	if(block_given?)
		yield
	end
	
	# send the email
	Log::debug("SENDING EMAIL: #{@email_addresses.inspect}")
	Log::debug(puts "Subject: #{@email_subject}")
	Log::debug(puts "Body: \n#{@email_body}")
	
	msg="From: #{smtp_config.from_addr}\n"
	msg << "To: #{@email_addresses.join(",")}\n"
	msg << "Subject: #{@email_subject}\n\n"
	msg << @email_body
	
	#
	# TODO: consider putting this in a different thread, or at least adding a timeout
	#
	
	ProcessWanker::with_logged_rescue("sending emails via #{smtp_config.server}") do
		
		smtp=Net::SMTP.new(smtp_config.server,smtp_config.port)
		
		if(smtp_config.secure == :tls || smtp_config.secure == :ssl)
			smtp.enable_tls
		elsif(smtp_config.secure == :starttls)
			smtp.enable_starttls
		end
		
		smtp.start(from_domain,
		           smtp_config.userid,
		           smtp_config.password,
		           smtp_config.auth_method)
		@email_addresses.each do |to|
			ProcessWanker::with_logged_rescue("sending email to #{to}") do
				smtp.send_message(msg,smtp_config.from_addr,to)
			end
		end
		smtp.finish
	end
	
	Log::debug("sent email")
	
end

#email_body(body) ⇒ Object



188
189
190
# File 'lib/events.rb', line 188

def email_body(body)
	@email_body=body
end

#email_subject(subject) ⇒ Object



184
185
186
# File 'lib/events.rb', line 184

def email_subject(subject)
	@email_subject = subject
end

#email_to_addrs(*addr) ⇒ Object

email helpers



180
181
182
# File 'lib/events.rb', line 180

def email_to_addrs(*addr)
	@email_addresses += addr
end

#ignoreObject



217
218
219
220
# File 'lib/events.rb', line 217

def ignore
	info("hook requested service ignore")
	@event.service.set_want_state(:ignore)
end

#restartObject



212
213
214
215
# File 'lib/events.rb', line 212

def restart
	info("hook requested service restart")
	@event.service.set_want_state(:restart)
end

#startObject



202
203
204
205
# File 'lib/events.rb', line 202

def start
	info("hook requested service start")
	@event.service.set_want_state(:up)
end

#stopObject



207
208
209
210
# File 'lib/events.rb', line 207

def stop
	info("hook requested service stop")
	@event.service.set_want_state(:down)
end

#stop_hooksObject

general helpers



198
199
200
# File 'lib/events.rb', line 198

def stop_hooks
	@should_stop_hooks=true
end