lib/arborist/monitor.rb in arborist-0.0.1.pre20161005182540 vs lib/arborist/monitor.rb in arborist-0.1.0

- old
+ new

@@ -33,12 +33,12 @@ ## # The default number of seconds to defer startup to splay common intervals DEFAULT_SPLAY = 0 - Arborist.add_dsl_constructor( self ) do |description, &block| - Arborist::Monitor.new( description, &block ) + Arborist.add_dsl_constructor( self ) do |description=nil, key=nil, &block| + Arborist::Monitor.new( description, key, &block ) end # The module that contains the default logic for invoking an external program # to do the work of a Monitor. @@ -141,11 +141,12 @@ ### 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, &block ) + def initialize( description=nil, key=nil, &block ) + @key = key @description = description @interval = DEFAULT_INTERVAL @splay = DEFAULT_SPLAY @positive_criteria = {} @@ -158,22 +159,29 @@ @exec_callbacks_mod = Module.new @source = nil self.instance_exec( &block ) if block + + self.check_config end ###### public ###### ## - # The object's description - attr_accessor :description + # The monitor's key. This key should be shared between monitors that check the + # same resources. + attr_writer :key ## + # The monitor's (human) description. + attr_writer :description + + ## # The interval between runs in seconds, as set by `every`. attr_writer :interval ## # The number of seconds of splay to use when running the monitor. @@ -224,10 +232,30 @@ @splay, ] end + ### Check the monitor for sanity, raising an Arborist::ConfigError if it isn't. + def check_config + raise Arborist::ConfigError, "No description set" unless self.description + raise Arborist::ConfigError, "No key set" unless self.key + end + + + ### Get/set the description of the monitor. + def description( new_value=nil ) + self.description = new_value if new_value + return @description + end + + + ### Get/set the key used by the monitor. + def key( new_value=nil ) + self.key = new_value if new_value + return @key + end + ### Run the monitor def run( nodes ) if self.exec_block return self.exec_block.call( nodes ) elsif self.exec_command @@ -250,14 +278,14 @@ child_stdin, parent_writer = IO.pipe parent_reader, child_stdout = IO.pipe parent_err_reader, child_stderr = IO.pipe self.log.debug "Spawning command: %s" % [ Shellwords.join(command) ] - pid = Process.spawn( *command, out: child_stdout, in: child_stdin, err: child_stderr ) + pid = Process.spawn( *command, out: child_stdout, in: child_stdin, err: child_stderr ) - child_stdout.close - child_stdin.close + child_stdout.close + child_stdin.close child_stderr.close context.exec_input( nodes, parent_writer ) parent_writer.close @@ -300,9 +328,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 && + Arborist::Node::UNREACHABLE_STATES.include?( self.positive_criteria[:status] ) end ### Specify that the monitor should exclude nodes which match the specified ### +criteria+ when searching for nodes it will run against.