Sha256: 202a42e7934a720e4cb4f83090dbe5cc848b44c75532cd17435228ac4962b3b9

Contents?: true

Size: 1.82 KB

Versions: 5

Compression:

Stored size: 1.82 KB

Contents

require 'city_watch/util/alerts'
require 'city_watch/util/rules'
require 'city_watch/util/flags'
require 'city_watch/util/notifications'
require 'city_watch/util/datasets'
require 'city_watch/util/status'

module Watchman
	
	def data
		{}
	end
	
	module ClassMethods
		
		def process(dat,rcv,host)
			@host = host
			@rcv_time = rcv
			CityWatch.redis.zadd "#{CityWatch.config[:prefix]}::#{host}::#{self.name}", rcv_time, Yajl::Encoder.encode(dat.merge({:received_at => dat[:received_at]}))
			if dat[:summary]
				sum = dat[:summary].is_a?(Array) ? dat[:summary].inject({}) {|acc,k| acc[k.to_sym] = dat[k.to_sym]; acc} : dat[:summary]
				CityWatch.redis.zadd "#{CityWatch.config[:prefix]}::#{host}::#{self.name}::summary", rcv_time, Yajl::Encoder.encode(sum)
			end
			run_post_processors!(dat,rcv,host)
			return 0, sum || nil
		end
		
		def host
			@host || nil
		end
		
		def rcv_time
			@rcv_time || Time.now.to_i
		end
		
		def set_default(k,val)
			opts[k] = val
		end
		
		def opts
			@options ||= {}
		end
		
		def options(*args)
			if args.count > 1
				return args.map {|k| opts[k]}
			else
				return opts[args.first]
			end
			return nil
		end
		alias_method :option, :options
		
		def run_post_processors!(dat,rcv,host)
			@post_processors.each do |proc|
				proc.call(dat,rcv,host)
			end if @post_processors
		end
		
		def add_post_processor(meth=nil,&block)
			@post_processors ||= []
			@post_processors << (block_given? ? block : meth)
		end
		
	end
	
	def self.included(base)
		base.extend(ClassMethods)
		base.extend(Alerts)
		base.add_post_processor base.method(:send_alerts!)
		base.extend(Rules)
		base.add_post_processor base.method(:run_rules)
		base.extend(Flags)
		base.extend(Notifications)
		base.extend(DataSets)
		base.add_post_processor base.method(:run_dataset_collector)
		base.extend(Status)
		Watchmen.register(base)
	end
	
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
city-watch-0.6.2 lib/city_watch/util/watchman.rb
city-watch-0.6.1 lib/city_watch/util/watchman.rb
city-watch-0.6.0 lib/city_watch/util/watchman.rb
city-watch-0.5.9 lib/city_watch/util/watchman.rb
city-watch-0.5.8 lib/city_watch/util/watchman.rb