Module: Sensu::Server::Mutate

Included in:
Process
Defined in:
lib/sensu/server/mutate.rb

Instance Method Summary collapse

Instance Method Details

#mutate_event(handler, event, &callback) ⇒ Object

Mutate event data for a handler. By default, the “json” mutator is used, unless the handler specifies another mutator. If a mutator does not exist, not defined or a missing extension, an error will be logged and the ‘@in_progress` is decremented by `1`. This method first checks for the existence of a standard mutator, then checks for an extension if a standard mutator is not defined.

Parameters:

  • handler (Hash)

    definition.

  • event (Hash)

    data.

  • callback (Proc)

    to call when the mutator executes/runs successfully (event handler).



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/sensu/server/mutate.rb', line 74

def mutate_event(handler, event, &callback)
  mutator_name = handler[:mutator] || "json"
  case
  when @settings.mutator_exists?(mutator_name)
    mutator = @settings[:mutators][mutator_name]
    pipe_mutator(mutator, event, &callback)
  when @extensions.mutator_exists?(mutator_name)
    mutator = @extensions[:mutators][mutator_name]
    mutator_extension(mutator, event, &callback)
  else
    @logger.error("unknown mutator", {
      :mutator_name => mutator_name
    })
    @in_progress[:events] -= 1 if @in_progress
  end
end

#mutator_callback(mutator, event, &callback) ⇒ Proc

Create a mutator callback (Proc). A mutator callback takes two parameters, for the mutator output and status code. The created callback can be used for standard mutators and mutator extensions. The provided callback will only be called when the mutator status is ‘0` (OK). If the status is not `0`, an error is logged, and the `@in_progress` is decremented by `1`.

Parameters:

  • mutator (Object)

    definition or extension.

  • event (Hash)

    data.

  • callback (Proc)

    to call when the mutator status is ‘0`.

Returns:

  • (Proc)

    mutator callback.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/sensu/server/mutate.rb', line 16

def mutator_callback(mutator, event, &callback)
  Proc.new do |output, status|
    if status == 0
      callback.call(output)
    else
      definition = mutator.is_a?(Hash) ? mutator : mutator.definition
      @logger.error("mutator error", {
        :mutator => definition,
        :event => event,
        :output => output,
        :status => status
      })
      @in_progress[:events] -= 1 if @in_progress
    end
  end
end

#mutator_extension(mutator, event, &callback) ⇒ Object

Run a mutator extension, within the Sensu EventMachine reactor (event loop). The ‘mutator_callback()` method is used to create the mutator callback, wrapping the provided callback (event handler).

Parameters:

  • mutator (Object)

    extension.

  • event (Hash)

    data.

  • callback (Proc)

    to call when the mutator runs successfully.



57
58
59
60
# File 'lib/sensu/server/mutate.rb', line 57

def mutator_extension(mutator, event, &callback)
  block = mutator_callback(mutator, event, &callback)
  mutator.safe_run(event, &block)
end

#pipe_mutator(mutator, event, &callback) ⇒ Object

Execute a standard mutator (pipe), spawn a process using the mutator command and pipe the event data to it via STDIN. The ‘mutator_callback()` method is used to create the mutator callback, wrapping the provided callback (event handler).

Parameters:

  • mutator (Hash)

    definition.

  • event (Hash)

    data.

  • callback (Proc)

    to call when the mutator executes successfully.



42
43
44
45
46
# File 'lib/sensu/server/mutate.rb', line 42

def pipe_mutator(mutator, event, &callback)
  options = {:data => Sensu::JSON.dump(event), :timeout => mutator[:timeout]}
  block = mutator_callback(mutator, event, &callback)
  Spawn.process(mutator[:command], options, &block)
end