Class: OnesnooperServer::RequestHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/onesnooper_server/request_handler.rb

Overview

Request handler for validating and dispatching datagram processing on defined types of datagrams. Invalid or unknown datagrams will be processed by the `OnesnooperServer::Datagrams::InvalidDatagram` sub-handler.

Constant Summary collapse

MONITORING_DATA_REGEXP =

Validation constant for incoming datagrams

/^MONITOR\s(?<result>[[:alpha:]]+)\s(?<host_id>\d+)\s(?<payload>\S+)$/
DATAGRAMS =

Registered allowed datagram processing classes & the default fallback

{
  "SUCCESS" => ::OnesnooperServer::Datagrams::SuccessDatagram,
  "FAILURE" => ::OnesnooperServer::Datagrams::FailureDatagram,
}
STORES =

Registered allowed stores for monitoring data

{
  "mongodb" => ::OnesnooperServer::Stores::MongodbStore,
  "mysql"   => ::OnesnooperServer::Stores::MysqlStore,
  "sqlite"  => ::OnesnooperServer::Stores::SqliteStore,
}

Class Method Summary collapse

Class Method Details

.parse(monitoring_datagram, source_ip, source_port) ⇒ Object

Static parsing method for identifying types of incoming datagrams and choosing the right processing class for each datagram. Always returns an instance responding to `run(callback)`.


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/onesnooper_server/request_handler.rb', line 33

def self.parse(monitoring_datagram, source_ip, source_port)
  unless valid_data?(monitoring_datagram)
    ::OnesnooperServer::Log.fatal "[#{self.name}] Dropping invalid monitoring data #{monitoring_datagram.inspect}"
    return DATAGRAMS.default.new
  end

  unless valid_peer?(source_ip)
    ::OnesnooperServer::Log.warn "[#{self.name}] Dropping monitoring data from #{source_ip}, not allowed!"
    return DATAGRAMS.default.new
  end

  match_data = monitoring_datagram.match(MONITORING_DATA_REGEXP)
  return DATAGRAMS.default.new unless match_data

  DATAGRAMS[match_data[:result]].new({
    host_id: match_data[:host_id],
    payload: match_data[:payload],
    stores: store_instances,
  })
end