Class: RRDNotifier::Server

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/rrd-grapher/notifier.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(alarm_manager, on_init_block = nil, redirect_to = nil) ⇒ Server

Called by eventmachine when the connection is created.

Parameters:

  • redirect_to (String, nil) (defaults to: nil)

    Redirect to this host any packet received



16
17
18
19
20
# File 'lib/rrd-grapher/notifier.rb', line 16

def initialize(alarm_manager, on_init_block = nil, redirect_to = nil)
  @alarm_manager = alarm_manager
  @on_init_block = on_init_block
  @redirect_to = redirect_to ? redirect_to.split(':') : nil
end

Class Method Details

.start(opts = {}, &block) ⇒ Object

Start the notifier handler, it will open an UDP socket to which collectd should send its data using the network module.

Parameters:

  • opts (Hash) (defaults to: {})

    options hash

Options Hash (opts):

  • :host (String)

    UDP address to bind on (default: 127.0.0.1)

  • :port (Integer)

    UDP port to bind on

  • :notification_handler (Object)

    This object

  • :redirect_to (String)

    Retransmit packets once received to this <host>:<port>



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rrd-grapher/notifier.rb', line 33

def self.start(opts = {}, &block)
  host = opts.delete(:host) || '127.0.0.1'
  port = opts.delete(:port) || 10000
  redirect_to = opts.delete(:redirect_to)
  
  if redirect_to
    # TODO: make it separate from redirect_to ?
    opts.merge!(:send_monitoring_to => redirect_to)
  end
  
  alarm_manager = AlarmManager.new(opts)
  
  unless opts.empty?
    raise "Unknown arguments: #{opts}"

  end
  EM::open_datagram_socket(host, port, Server, alarm_manager, block, redirect_to)
end

Instance Method Details

#post_initObject

Eventmachine callbacks



64
65
66
# File 'lib/rrd-grapher/notifier.rb', line 64

def post_init
  @on_init_block.call(self) if @on_init_block
end

#receive_data(data) ⇒ Object

EventMachine callback called when a packet is available

Parameters:

  • data (String)

    data received



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rrd-grapher/notifier.rb', line 73

def receive_data(data)
  if packets = RubyParser::parse(data)
    packets.each do |p|
      @alarm_manager.packet_received(p)
    end
  end
  
  if @redirect_to
    send_datagram(data, @redirect_to[0], @redirect_to[1])
  end
  
end

#register_alarm(*args) ⇒ Object

Register a new monitoring alert.

See Also:

  • AlertManager::register_alert


57
58
59
# File 'lib/rrd-grapher/notifier.rb', line 57

def register_alarm(*args)
  @alarm_manager.register_alarm(*args)
end