lib/arborist/monitor.rb in arborist-0.2.0.pre20170519125456 vs lib/arborist/monitor.rb in arborist-0.2.0

- old
+ new

@@ -8,12 +8,13 @@ using Arborist::TimeRefinements # A declaration of an action to run against Manager nodes to update their state. class Arborist::Monitor - extend Loggability, - Arborist::MethodUtilities + extend Configurability, + Loggability, + Arborist::MethodUtilities # Loggability API -- write logs to the Arborist log host log_to :arborist @@ -28,15 +29,22 @@ ## # The default monitoring interval, in seconds DEFAULT_INTERVAL = 5.minutes - ## - # The default number of seconds to defer startup to splay common intervals - DEFAULT_SPLAY = 0 + # Configurability API -- use the 'arborist' section + configurability( 'arborist.monitor' ) do + ## + # A default splay to apply to all Monitors. + setting :splay, default: 0 do |value| + Float( value ) + end + end + + Arborist.add_dsl_constructor( self ) do |description=nil, key=nil, &block| Arborist::Monitor.new( description, key, &block ) end @@ -56,11 +64,12 @@ return if io.closed? nodes.each do |(identifier, data)| self.log.debug "Serializing node properties for %s" % [ identifier ] prop_map = data.collect do |key, val| - "%s=%s" % [key, Shellwords.escape(val)] + val = val.join( ',' ) if val.is_a?( Array ) + "%s=%s" % [ key, Shellwords.escape(val) ] end self.log.debug " writing %d properties to %p" % [ prop_map.size, io ] io.puts "%s %s" % [ identifier, prop_map.join(' ') ] self.log.debug " wrote the node to FD %d" % [ io.fileno ] @@ -143,17 +152,17 @@ ### Create a new Monitor with the specified +description+. If the +block+ is ### given, it will be evaluated in the context of the new Monitor before it's ### returned. def initialize( description=nil, key=nil, &block ) @key = key - @description = description + @description = description || self.class.name @interval = DEFAULT_INTERVAL - @splay = DEFAULT_SPLAY + @splay = Arborist::Monitor.splay @positive_criteria = {} @negative_criteria = {} - @include_down = false + @exclude_down = false @node_properties = [] @exec_command = nil @exec_block = nil @exec_callbacks_mod = Module.new @@ -196,11 +205,11 @@ attr_reader :negative_criteria ## # Flag for whether the monitor will include downed hosts in its search. Defaults # to +false+. - attr_predicate :include_down + attr_predicate :exclude_down ## # The list of node properties to include when running the monitor. attr_reader :node_properties @@ -222,11 +231,11 @@ attr_accessor :source ### Return a string representation of the object suitable for debugging. def inspect - return "#<%p:%#x %s (every %ds ±%ds)>" % [ + return "#<%p:%#x %s (every %ds +-%ds)>" % [ self.class, self.object_id * 2, self.description || "(no description)", @interval, @splay, @@ -328,11 +337,11 @@ ### Specify that the monitor should include the specified +criteria+ when searching ### for nodes it will run against. def match( criteria ) self.positive_criteria.merge!( criteria ) - @include_down = !self.include_down && + @exclude_down = self.exclude_down && Arborist::Node::UNREACHABLE_STATES.include?( self.positive_criteria[:status] ) end ### Specify that the monitor should exclude nodes which match the specified @@ -342,13 +351,13 @@ end ### Specify that the monitor should (or should not) include nodes which have been ### marked 'down'. - def include_down( flag=nil ) - @include_down = flag unless flag.nil? - return @include_down + def exclude_down( flag=nil ) + @exclude_down = flag unless flag.nil? + return @exclude_down end ### Specify properties from each node to provide to the monitor. def use( *properties ) @@ -361,13 +370,15 @@ def exec( *command, &block ) unless command.empty? self.log.warn "Ignored block with exec %s (%p)" % [ command.first, block ] if block if command.first.respond_to?( :run ) - @exec_block = command.first.method( :run ) + runner = command.first + @exec_block = runner.method( :run ) + @node_properties |= runner.node_properties if runner.respond_to?( :node_properties ) else - @exec_command = command + @exec_command = command.map( &:to_s ) end return end @exec_block = block @@ -406,9 +417,10 @@ ### Set the module to use for the callbacks when interacting with the executed ### external command. def exec_callbacks( mod ) self.log.info "Setting exec callbacks handler to: %p" % [ mod.name ] + @node_properties |= mod.node_properties if mod.respond_to?( :node_properties ) self.exec_callbacks_mod = mod end end # class Arborist::Monitor