Module: Sensu::Server::Handle

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

Instance Method Summary collapse

Instance Method Details

#handle_event(handler, event_data) ⇒ Object

Handle an event, providing event data to an event handler. This method logs event data and the handler definition at the debug log level, then calls the ‘handler_type_router()` method.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to pass to an event handler.



158
159
160
161
162
163
164
165
# File 'lib/sensu/server/handle.rb', line 158

def handle_event(handler, event_data)
  definition = handler.is_a?(Hash) ? handler : handler.definition
  @logger.debug("handling event", {
    :event_data => event_data,
    :handler => definition
  })
  handler_type_router(handler, event_data)
end

#handler_error(handler, event_data) ⇒ Proc

Create a handler error callback, for logging the error and decrementing the ‘@handling_event_count` by `1`.

Parameters:

  • handler (Object)
  • event_data (Object)

Returns:

  • (Proc)

    error callback.



12
13
14
15
16
17
18
19
20
21
# File 'lib/sensu/server/handle.rb', line 12

def handler_error(handler, event_data)
  Proc.new do |error|
    @logger.error("handler error", {
      :handler => handler,
      :event_data => event_data,
      :error => error.to_s
    })
    @handling_event_count -= 1 if @handling_event_count
  end
end

#handler_extension(handler, event_data) ⇒ Object

Run a handler extension, within the Sensu EventMachine reactor (event loop). The extension API ‘safe_run()` method is used to guard against most errors. The `safe_run()` callback is always called, logging the extension run output and status, and decrementing the `@handling_event_count` by `1`.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to pass to the handler extension.



120
121
122
123
124
125
126
127
128
129
# File 'lib/sensu/server/handle.rb', line 120

def handler_extension(handler, event_data)
  handler.safe_run(event_data) do |output, status|
    @logger.info("handler extension output", {
      :extension => handler.definition,
      :output => output,
      :status => status
    })
    @handling_event_count -= 1 if @handling_event_count
  end
end

#handler_type_router(handler, event_data) ⇒ Object

Route the event data to the appropriate handler type method. Routing is done using the handler definition, ‘:type`.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to pass to the handler type method.



136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/sensu/server/handle.rb', line 136

def handler_type_router(handler, event_data)
  case handler[:type]
  when "pipe"
    pipe_handler(handler, event_data)
  when "tcp"
    tcp_handler(handler, event_data)
  when "udp"
    udp_handler(handler, event_data)
  when "transport"
    transport_handler(handler, event_data)
  when "extension"
    handler_extension(handler, event_data)
  end
end

#pipe_handler(handler, event_data) ⇒ Object

Execute a pipe event handler, using the defined handler command to spawn a process, passing it event data via STDIN. Log the handler output lines and decrement the ‘@handling_event_count` by `1` when the handler executes successfully.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    provided to the spawned handler process via STDIN.



32
33
34
35
36
37
38
39
40
41
# File 'lib/sensu/server/handle.rb', line 32

def pipe_handler(handler, event_data)
  options = {:data => event_data, :timeout => handler[:timeout]}
  Spawn.process(handler[:command], options) do |output, status|
    @logger.info("handler output", {
      :handler => handler,
      :output => output.lines
    })
    @handling_event_count -= 1 if @handling_event_count
  end
end

#tcp_handler(handler, event_data) ⇒ Object

Connect to a TCP socket and transmit event data to it, then close the connection. The ‘Sensu::Server::Socket` connection handler is used for the socket. The socket timeouts are configurable via the handler definition, `:timeout`. The `handler_error()` method is used to create the `on_error` callback for the connection handler. The `on_error` callback is call in the event of any error(s). The `@handling_event_count` is decremented by `1` when the data is transmitted successfully, `on_success`.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to transmit to the TCP socket.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/sensu/server/handle.rb', line 55

def tcp_handler(handler, event_data)
  on_error = handler_error(handler, event_data)
  begin
    EM::connect(handler[:socket][:host], handler[:socket][:port], Socket) do |socket|
      socket.on_success = Proc.new do
        @handling_event_count -= 1 if @handling_event_count
      end
      socket.on_error = on_error
      timeout = handler[:timeout] || 10
      socket.pending_connect_timeout = timeout
      socket.comm_inactivity_timeout = timeout
      socket.send_data(event_data.to_s)
      socket.close_connection_after_writing
    end
  rescue => error
    on_error.call(error)
  end
end

#transport_handler(handler, event_data) ⇒ Object

Publish event data to a Sensu transport pipe. Event data that is ‘nil` or empty will not be published, to prevent transport errors. The `@handling_event_count` is decremented by `1`, even if the event data is not published.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to publish to the transport pipe.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/sensu/server/handle.rb', line 99

def transport_handler(handler, event_data)
  unless event_data.nil? || event_data.empty?
    pipe = handler[:pipe]
    pipe_options = pipe[:options] || {}
    @transport.publish(pipe[:type].to_sym, pipe[:name], event_data, pipe_options) do |info|
      if info[:error]
        handler_error(handler, event_data).call(info[:error])
      end
    end
  end
  @handling_event_count -= 1 if @handling_event_count
end

#udp_handler(handler, event_data) ⇒ Object

Transmit event data to a UDP socket, then close the connection. The ‘@handling_event_count` is decremented by `1` when the data is assumed to have been transmitted.

Parameters:

  • handler (Hash)

    definition.

  • event_data (Object)

    to transmit to the UDP socket.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/sensu/server/handle.rb', line 80

def udp_handler(handler, event_data)
  begin
    EM::open_datagram_socket("0.0.0.0", 0, nil) do |socket|
      socket.send_datagram(event_data.to_s, handler[:socket][:host], handler[:socket][:port])
      socket.close_connection_after_writing
      @handling_event_count -= 1 if @handling_event_count
    end
  rescue => error
    handler_error(handler, event_data).call(error)
  end
end