lib/sensu/client/process.rb
def execute_check_hook(check)
        @logger.debug("attempting to execute check hook", :check => check)
        severity = SEVERITIES[check[:status]] || "unknown"
        hook = check[:hooks][check[:status].to_s.to_sym] || check[:hooks][severity.to_sym]
        if hook.nil? && check[:status] != 0
          hook = check[:hooks]["non-zero".to_sym]
        end
        if hook
          command, unmatched_tokens = substitute_tokens(hook[:command].dup, @settings[:client])
          started = Time.now.to_f
          hook[:executed] = started.to_i
          if unmatched_tokens.empty?
            options = {:timeout => hook.fetch(:timeout, 60)}
            if hook[:stdin]
              options[:data] = Sensu::JSON.dump({
                :client => @settings[:client],
                :check => check
              })
            end
            Spawn.process(command, options) do |output, status|
              hook[:duration] = ("%.3f" % (Time.now.to_f - started)).to_f
              hook[:output] = output
              hook[:status] = status
              yield(check)
            end
          else
            hook[:output] = "Unmatched client token(s): " + unmatched_tokens.join(", ")
            hook[:status] = 3
            yield(check)
          end
        else
          yield(check)
        end
      end
# Execute a check hook, capturing its output (STDOUT/ERR),
# exit status code, executed timestamp, and duration. This
# method determines which hook command to run by inspecting the
# check execution status. Check hook command tokens are
# substituted with the associated client attribute values, via
# `substitute_tokens()`. If there are unmatched check attribute
# value tokens, the check hook will not be executed, instead the
# hook command output will be set to report the unmatched
# tokens. Hook commands may expect/read and utilize JSON
# serialized Sensu client and check data via STDIN, if the hook
# definition includes `"stdin": true` (default is `false`). A
# hook may have a configured execution timeout, e.g. `"timeout":
# 30`, if one is not specified, the timeout defaults to 60
# seconds.
#
# @param check [Hash]
# @yield [check] callback/block called after executing the check
#   hook (if any).
Execute a check hook, capturing its output (STDOUT/ERR),
exit status code, executed timestamp, and duration. This
method determines which hook command to run by inspecting the
check execution status. Check hook command tokens are
substituted with the associated client attribute values, via
`substitute_tokens()`. If there are unmatched check attribute
value tokens, the check hook will not be executed, instead the
hook command output will be set to report the unmatched
tokens. Hook commands may expect/read and utilize JSON
serialized Sensu client and check data via STDIN, if the hook
definition includes `"stdin": true` (default is `false`). A
hook may have a configured execution timeout, e.g. `"timeout":
30`, if one is not specified, the timeout defaults to 60
seconds.
Execute a check hook capturing its output ( STDOUT / ERR ) exit status code executed timestamp and duration . This method determines which hook command to run by inspecting the check execution status . Check hook command tokens are substituted with the associated client attribute values via ` substitute_tokens () ` . If there are unmatched check attribute value tokens the check hook will not be executed instead the hook command output will be set to report the unmatched tokens . Hook commands may expect / read and utilize JSON serialized Sensu client and check data via STDIN if the hook definition includes ` " stdin " : true ` ( default is ` false ` ) . A hook may have a configured execution timeout e . g . ` " timeout " : 30 ` if one is not specified the timeout defaults to 60 seconds .
def execute_check_hook(check)
        @logger.debug("attempting to execute check hook", :check => check)
        severity = SEVERITIES[check[:status]] || "unknown"
        hook = check[:hooks][check[:status].to_s.to_sym] || check[:hooks][severity.to_sym]
        if hook.nil? && check[:status] != 0
          hook = check[:hooks]["non-zero".to_sym]
        end
        if hook
          command, unmatched_tokens = substitute_tokens(hook[:command].dup, @settings[:client])
          started = Time.now.to_f
          hook[:executed] = started.to_i
          if unmatched_tokens.empty?
            options = {:timeout => hook.fetch(:timeout, 60)}
            if hook[:stdin]
              options[:data] = Sensu::JSON.dump({
                :client => @settings[:client],
                :check => check
              })
            end
            Spawn.process(command, options) do |output, status|
              hook[:duration] = ("%.3f" % (Time.now.to_f - started)).to_f
              hook[:output] = output
              hook[:status] = status
              yield(check)
            end
          else
            hook[:output] = "Unmatched client token(s): " + unmatched_tokens.join(", ")
            hook[:status] = 3
            yield(check)
          end
        else
          yield(check)
        end
      end
def execute_check_hook ( check ) @logger . debug ( "attempting to execute check hook" , :check => check ) severity = SEVERITIES [ check [ :status ] ] || "unknown" hook = check [ :hooks ] [ check [ :status ] . to_s . to_sym ] || check [ :hooks ] [ severity . to_sym ] if hook . nil? && check [ :status ] != 0 hook = check [ :hooks ] [ "non-zero" . to_sym ] end if hook command , unmatched_tokens = substitute_tokens ( hook [ :command ] . dup , @settings [ :client ] ) started = Time . now . to_f hook [ :executed ] = started . to_i if unmatched_tokens . empty? options = { :timeout => hook . fetch ( :timeout , 60 ) } if hook [ :stdin ] options [ :data ] = Sensu :: JSON . dump ( { :client => @settings [ :client ] , :check => check } ) end Spawn . process ( command , options ) do | output , status | hook [ :duration ] = ( "%.3f" % ( Time . now . to_f - started ) ) . to_f hook [ :output ] = output hook [ :status ] = status yield ( check ) end else hook [ :output ] = "Unmatched client token(s): " + unmatched_tokens . join ( ", " ) hook [ :status ] = 3 yield ( check ) end else yield ( check ) end end
Execute a check hook, capturing its output (STDOUT/ERR),
exit status code, executed timestamp, and duration.
Execute a check hook capturing its output ( STDOUT / ERR ) exit status code executed timestamp and duration .
check
None
]
callback/block called after executing the check hook (if any).
callback / block called after executing the check hook ( if any ) .