lib/sensu/client/process.rb
# Execute a check command, capturing its output (STDOUT/ERR),
# exit status code, execution duration, timestamp, and publish
# the result. This method guards against multiple executions for
# the same check. Check attribute value tokens are substituted
# with the associated client attribute values, via
# `object_substitute_tokens()`. The original check command and
# hooks are always published, to guard against publishing
# sensitive/redacted client attribute values. If there are
# unmatched check attribute value tokens, the check will not be
# executed, instead a check result will be published reporting
# the unmatched tokens.
#
# @param check [Hash]
Execute a check command, capturing its output (STDOUT/ERR),
exit status code, execution duration, timestamp, and publish
the result. This method guards against multiple executions for
the same check. Check attribute value tokens are substituted
with the associated client attribute values, via
`object_substitute_tokens()`. The original check command and
hooks are always published, to guard against publishing
sensitive/redacted client attribute values. If there are
unmatched check attribute value tokens, the check will not be
executed, instead a check result will be published reporting
the unmatched tokens.
Execute a check command capturing its output ( STDOUT / ERR ) exit status code execution duration timestamp and publish the result . This method guards against multiple executions for the same check . Check attribute value tokens are substituted with the associated client attribute values via ` object_substitute_tokens () ` . The original check command and hooks are always published to guard against publishing sensitive / redacted client attribute values . If there are unmatched check attribute value tokens the check will not be executed instead a check result will be published reporting the unmatched tokens .
def execute_check_command(check)
        @logger.debug("attempting to execute check command", :check => check)
        in_progress_key = check_in_progress_key(check)
        unless @checks_in_progress.include?(in_progress_key)
          @checks_in_progress << in_progress_key
          substituted, unmatched_tokens = object_substitute_tokens(check.dup, @settings[:client])
          check = substituted.merge(:command => check[:command], :hooks => check[:hooks])
          check.delete(:hooks) if check[:hooks].nil?
          started = Time.now.to_f
          check[:executed] = started.to_i
          if unmatched_tokens.empty?
            options = {:timeout => check[:timeout]}
            if check[:stdin]
              options[:data] = Sensu::JSON.dump({
                :client => @settings[:client],
                :check => check
              })
            end
            Spawn.process(substituted[:command], options) do |output, status|
              check[:duration] = ("%.3f" % (Time.now.to_f - started)).to_f
              check[:output] = output
              check[:status] = status
              if check[:hooks] && !check[:hooks].empty?
                execute_check_hook(check) do |check|
                  publish_check_result(check)
                  @checks_in_progress.delete(in_progress_key)
                end
              else
                publish_check_result(check)
                @checks_in_progress.delete(in_progress_key)
              end
            end
          else
            check[:output] = "Unmatched client token(s): " + unmatched_tokens.join(", ")
            check[:status] = 3
            check[:handle] = false
            publish_check_result(check)
            @checks_in_progress.delete(in_progress_key)
          end
        else
          @logger.warn("previous check command execution in progress", :check => check)
        end
      end
def execute_check_command ( check ) @logger . debug ( "attempting to execute check command" , :check => check ) in_progress_key = check_in_progress_key ( check ) unless @checks_in_progress . include? ( in_progress_key ) @checks_in_progress << in_progress_key substituted , unmatched_tokens = object_substitute_tokens ( check . dup , @settings [ :client ] ) check = substituted . merge ( :command => check [ :command ] , :hooks => check [ :hooks ] ) check . delete ( :hooks ) if check [ :hooks ] . nil? started = Time . now . to_f check [ :executed ] = started . to_i if unmatched_tokens . empty? options = { :timeout => check [ :timeout ] } if check [ :stdin ] options [ :data ] = Sensu :: JSON . dump ( { :client => @settings [ :client ] , :check => check } ) end Spawn . process ( substituted [ :command ] , options ) do | output , status | check [ :duration ] = ( "%.3f" % ( Time . now . to_f - started ) ) . to_f check [ :output ] = output check [ :status ] = status if check [ :hooks ] && ! check [ :hooks ] . empty? execute_check_hook ( check ) do | check | publish_check_result ( check ) @checks_in_progress . delete ( in_progress_key ) end else publish_check_result ( check ) @checks_in_progress . delete ( in_progress_key ) end end else check [ :output ] = "Unmatched client token(s): " + unmatched_tokens . join ( ", " ) check [ :status ] = 3 check [ :handle ] = false publish_check_result ( check ) @checks_in_progress . delete ( in_progress_key ) end else @logger . warn ( "previous check command execution in progress" , :check => check ) end end
Execute a check command, capturing its output (STDOUT/ERR),
exit status code, execution duration, timestamp, and publish
the result.
Execute a check command capturing its output ( STDOUT / ERR ) exit status code execution duration timestamp and publish the result .
check
None