Class: Sensu::Plugins::Events::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Utils::Log
Defined in:
lib/sensu/plugins/events/dispatcher.rb

Overview

Helper class to dispatch events into Sensu socket.

Instance Method Summary collapse

Methods included from Utils::Log

#log, log

Constructor Details

#initializeDispatcher

Returns a new instance of Dispatcher.



12
13
14
15
16
17
18
# File 'lib/sensu/plugins/events/dispatcher.rb', line 12

def initialize
  @sensu_address = '127.0.0.1'
  @sensu_port = 3030
  read_env_address_and_port

  log.debug("Sensu at '#{@sensu_address}':'#{@sensu_port}'")
end

Instance Method Details

#dispatch(event) ⇒ Object

Send accumulated events into Sensu’s socket, unless environment variable PROM_DEBUG is set.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sensu/plugins/events/dispatcher.rb', line 22

def dispatch(event)
  if ENV.key?('PROM_DEBUG')
    log.debug("PROM_DEBUG set, not dispatching event to Sensu: #{event}")
    return
  end

  # :nocov:
  begin
    s = TCPSocket.open(@sensu_address, @sensu_port)
    s.puts(JSON.generate(event))
    s.close
  rescue SystemCallError => e
    log.error("Sensu is refusing connections! Error: '#{e}'")
    raise("Sensu is not avilable at '#{@sensu_address}:#{@sensu_port}'")
  end
  # :nocov:
end