Module: Sensu::Server::Handle
- Included in:
- Process
- Defined in:
- lib/sensu/server/handle.rb
Instance Method Summary collapse
-
#handle_event(handler, event_data) ⇒ Object
Handle an event, providing event data to an event handler.
-
#handler_error(handler, event_data) ⇒ Proc
Create a handler error callback, for logging the error and decrementing the ‘@handling_event_count` by `1`.
-
#handler_extension(handler, event_data) ⇒ Object
Run a handler extension, within the Sensu EventMachine reactor (event loop).
-
#handler_type_router(handler, event_data) ⇒ Object
Route the event data to the appropriate handler type method.
-
#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.
-
#tcp_handler(handler, event_data) ⇒ Object
Connect to a TCP socket and transmit event data to it, then close the connection.
-
#transport_handler(handler, event_data) ⇒ Object
Publish event data to a Sensu transport pipe.
-
#udp_handler(handler, event_data) ⇒ Object
Transmit event data to a UDP socket, then close the connection.
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.
157 158 159 160 161 162 163 164 |
# File 'lib/sensu/server/handle.rb', line 157 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`.
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`.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/sensu/server/handle.rb', line 119 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`.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sensu/server/handle.rb', line 135 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.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/sensu/server/handle.rb', line 32 def pipe_handler(handler, event_data) = {:data => event_data, :timeout => handler[:timeout]} Spawn.process(handler[:command], ) do |output, status| @logger.info("handler output", { :handler => handler, :output => output.split("\n+") }) @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`.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# 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.set_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.
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sensu/server/handle.rb', line 98 def transport_handler(handler, event_data) unless event_data.nil? || event_data.empty? pipe = handler[:pipe] = pipe[:options] || {} @transport.publish(pipe[:type].to_sym, pipe[:name], event_data, ) 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.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/sensu/server/handle.rb', line 79 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 |