class Sensu::Transport::Base

Attributes

logger[RW]

@!attribute [rw] logger

@return [Logger] the Sensu logger object.

Public Class Methods

descendants() click to toggle source

Discover available transports (Subclasses)

# File lib/sensu/transport/base.rb, line 200
def self.descendants
  ObjectSpace.each_object(Class).select do |klass|
    klass < self
  end
end
new() click to toggle source
# File lib/sensu/transport/base.rb, line 19
def initialize
  @on_error = Proc.new {}
  @before_reconnect = Proc.new {}
  @after_reconnect = Proc.new {}
end

Public Instance Methods

ack(*args, &callback) click to toggle source

Alias for acknowledge(). This should be superseded by a proper alias via alias_method in the transport class.

# File lib/sensu/transport/base.rb, line 124
def ack(*args, &callback)
  acknowledge(*args, &callback)
end
acknowledge(info) { |info| ... } click to toggle source

Acknowledge the delivery of a message from the transport.

@param info [Hash] message information, eg. contains its id. @yield [info] passes acknowledgment info to an optional callback/block.

# File lib/sensu/transport/base.rb, line 118
def acknowledge(info)
  yield(info) if block_given?
end
after_reconnect(&callback) click to toggle source

Set the after reconnect callback.

@param callback [Proc] called after reconnecting to the

transport.

@return [Proc] the after reconnect callback.

# File lib/sensu/transport/base.rb, line 48
def after_reconnect(&callback)
  @after_reconnect = callback
end
before_reconnect(&callback) click to toggle source

Set the before reconnect callback.

@param callback [Proc] called before attempting to reconnect

to the transport.

@return [Proc] the before reconnect callback.

# File lib/sensu/transport/base.rb, line 39
def before_reconnect(&callback)
  @before_reconnect = callback
end
close() click to toggle source

Close the transport connection.

# File lib/sensu/transport/base.rb, line 70
def close; end
connect(options={}) click to toggle source

Transport connection setup.

@param options [Hash, String]

# File lib/sensu/transport/base.rb, line 55
def connect(options={}); end
connected?() click to toggle source

Indicates if connected to the transport.

@return [TrueClass, FalseClass]

# File lib/sensu/transport/base.rb, line 65
def connected?
  false
end
ip_address?(host) click to toggle source

Determine if a host is an IP address (or DNS hostname).

@param host [String] @return [TrueClass, FalseClass]

# File lib/sensu/transport/base.rb, line 143
def ip_address?(host)
  begin
    ip_address = IPAddr.new(host)
    ip_address.ipv4? || ip_address.ipv6?
  rescue IPAddr::InvalidAddressError
    false
  end
end
on_error(&callback) click to toggle source

Set the error callback.

@param callback [Proc] called in the event of a transport

error, the exception object should be passed as a parameter.

@return [Proc] the error callback.

# File lib/sensu/transport/base.rb, line 30
def on_error(&callback)
  @on_error = callback
end
publish(type, pipe, message, options={}) { |info| ... } click to toggle source

Publish a message to the transport.

@param type [Symbol] the transport pipe type, possible values

are: :direct and :fanout.

@param pipe [String] the transport pipe name. @param message [String] the message to be published to the transport. @param options [Hash] the options to publish the message with. @yield [info] passes publish info to an optional callback/block. @yieldparam info [Hash] contains publish information, which

may contain an error object (:error).
# File lib/sensu/transport/base.rb, line 82
def publish(type, pipe, message, options={})
  info = {:error => nil}
  yield(info) if block_given?
end
reconnect(force=false) click to toggle source

Reconnect to the transport.

@param force [Boolean] the reconnect.

# File lib/sensu/transport/base.rb, line 60
def reconnect(force=false); end
resolve_host(host) { |host| ... } click to toggle source

Resolve a hostname to an IP address for a host. This method will return the provided host to the provided callback if it is already an IP address. This method will return `nil` to the provided callback when the hostname cannot be resolved to an IP address.

@param host [String] @param callback [Proc] called with the result of the DNS

query (IP address).
# File lib/sensu/transport/base.rb, line 191
def resolve_host(host, &callback)
  if ip_address?(host)
    yield host
  else
    resolve_hostname(host, &callback)
  end
end
resolve_hostname(host, &callback) click to toggle source

Resolve a hostname to an IP address for a host. This method will return `nil` to the provided callback when the hostname cannot be resolved to an IP address.

@param host [String] @param callback [Proc] called with the result of the DNS

query (IP address).
# File lib/sensu/transport/base.rb, line 159
def resolve_hostname(host, &callback)
  resolve = Proc.new do
    begin
      info = case RUBY_PLATFORM
      when /linux/
        flags = Socket::AI_NUMERICSERV | Socket::AI_ADDRCONFIG
        Socket.getaddrinfo(host, nil, Socket::AF_UNSPEC, nil, nil, flags)
      else
        Socket.getaddrinfo(host, nil)
      end
      info.first.nil? ? nil : info.first[2]
    rescue => error
      @logger.error("transport connection error", {
        :reason => "unable to resolve hostname",
        :host => host,
        :error => error.to_s
      }) if @logger
      nil
    end
  end
  EM.defer(resolve, callback)
end
stats(funnel, options={}) { |info| ... } click to toggle source

Transport funnel stats, such as message and consumer counts.

@param funnel [String] the transport funnel to get stats for. @param options [Hash] the options to get funnel stats with. @yield [info] passes funnel stats a callback/block. @yieldparam info [Hash] contains funnel stats.

# File lib/sensu/transport/base.rb, line 134
def stats(funnel, options={})
  info = {}
  yield(info) if block_given?
end
subscribe(type, pipe, funnel=nil, options={}) { |info, message| ... } click to toggle source

Subscribe to a transport pipe and/or funnel.

@param type [Symbol] the transport pipe type, possible values

are: :direct and :fanout.

@param pipe [String] the transport pipe name. @param funnel [String] the transport funnel, which may be

connected to multiple pipes.

@param options [Hash] the options to consume messages with. @yield [info, message] passes message info and content to

the consumer callback/block.

@yieldparam info [Hash] contains message information. @yieldparam message [String] message.

# File lib/sensu/transport/base.rb, line 99
def subscribe(type, pipe, funnel=nil, options={})
  info = {}
  message = ''
  yield(info, message)
end
unsubscribe() { |info| ... } click to toggle source

Unsubscribe from all transport pipes and/or funnels.

@yield [info] passes info to an optional callback/block. @yieldparam info [Hash] contains unsubscribe information.

# File lib/sensu/transport/base.rb, line 109
def unsubscribe
  info = {}
  yield(info) if block_given?
end

Private Instance Methods

catch_errors() { || ... } click to toggle source

Catch transport errors and call the on_error callback, providing it with the error object as an argument. This method is intended to be applied where necessary, not to be confused with a catch-all. Not all transports will need this.

@yield [] callback/block to execute within a rescue block to

catch transport errors.
# File lib/sensu/transport/base.rb, line 215
def catch_errors
  begin
    yield
  rescue => error
    @on_error.call(error)
  end
end