Module: Alerts

Defined in:
lib/city_watch/util/alerts.rb

Instance Method Summary collapse

Instance Method Details

#alertsObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/city_watch/util/alerts.rb', line 27

def alerts
	@alerts ||= []
	if block_given?
		@alerts.each do |a|
			yield a
		end
	else
		@alerts
	end
	nil
end

#get_alerts(host = host, num = 5) ⇒ Object



39
40
41
# File 'lib/city_watch/util/alerts.rb', line 39

def get_alerts(host=host,num=5)
	CityWatch.redis.zrevrange("#{CityWatch.config[:prefix]}::#{host}::#{self.name}::alerts", 0, num - 1).map {|dat| Yajl::Parser.new(:symbolize_keys => true).parse(dat) }
end

#send_alert(message, dat = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/city_watch/util/alerts.rb', line 13

def send_alert(message,dat=nil)
	CityWatch.redis.zadd "#{CityWatch.config[:prefix]}::#{host}::#{self.name}::alerts", rcv_time, Yajl::Encoder.encode({:message => message, :data => dat, :when => rcv_time})
	if eml = CityWatch.config[:alert_by_email]
		mail = Mail.new {
			from "citywatch@#{Socket.gethostbyname(Socket.gethostname).first}"
			to eml
			subject "CityWatch: ALERT #{message}"
			body "Alert data: #{data.inspect}"
		}
		mail.delivery_method :sendmail
		mail.deliver!
	end
end

#send_alerts!(*args) ⇒ Object



43
44
45
46
47
# File 'lib/city_watch/util/alerts.rb', line 43

def send_alerts!(*args)
	get_alerts.map do |alert|
		puts "Alert: #{alert.inspect}" if CityWatch.debug?
	end
end