class Riemann::Client

Constants

HOST
PORT
TIMEOUT

Attributes

tcp[R]
udp[R]

Public Class Methods

new(opts = {}) { |self| ... } click to toggle source
# File lib/riemann/client.rb, line 25
def initialize(opts = {})
  @options = opts.dup
  @options[:host] ||= HOST
  @options[:port] ||= PORT
  @options[:timeout] ||= TIMEOUT

  @udp = UDP.new(@options)
  @tcp = TCP.new(@options)
  return unless block_given?

  begin
    yield self
  ensure
    close
  end
end

Public Instance Methods

<<(event) click to toggle source

Send a state

# File lib/riemann/client.rb, line 55
def <<(event)
  # Create state
  case event
  when Riemann::State, Riemann::Event, Hash
    # Noop
  else
    raise(ArgumentError, "Unsupported event class: #{event.class.name}")
  end

  bulk_send([event])
end
[](query) click to toggle source

Returns an array of states matching query.

# File lib/riemann/client.rb, line 94
def [](query)
  response = query(query)
  (response.events || []) |
    (response.states || [])
end
bulk_send(events) click to toggle source
# File lib/riemann/client.rb, line 67
def bulk_send(events)
  raise ArgumentError unless events.is_a?(Array)

  message = Riemann::Message.new(events: normalize_events(events))

  send_maybe_recv(message)
end
close() click to toggle source

Close both UDP and TCP sockets.

# File lib/riemann/client.rb, line 106
def close
  @udp.close
  @tcp.close
end
connect() click to toggle source
# File lib/riemann/client.rb, line 100
def connect
  # NOTE: connections are made automatically on send
  warn 'Riemann client#connect is deprecated'
end
connected?() click to toggle source
# File lib/riemann/client.rb, line 111
def connected?
  tcp.connected? and udp.connected?
end
host() click to toggle source
# File lib/riemann/client.rb, line 42
def host
  @options[:host]
end
normalize_events(events) click to toggle source
# File lib/riemann/client.rb, line 75
def normalize_events(events)
  events.map do |event|
    case event
    when Riemann::State, Riemann::Event
      event
    when Hash
      e = if event.include?(:host)
            event
          else
            event.dup.merge(host: Socket.gethostname)
          end
      Riemann::Event.new(e)
    else
      raise(ArgumentError, "Unsupported event class: #{event.class.name}")
    end
  end
end
port() click to toggle source
# File lib/riemann/client.rb, line 46
def port
  @options[:port]
end
query(string = 'true') click to toggle source

Ask for states

# File lib/riemann/client.rb, line 116
def query(string = 'true')
  send_recv Riemann::Message.new(query: Riemann::Query.new(string: string))
end
send_maybe_recv(message) click to toggle source
# File lib/riemann/client.rb, line 124
def send_maybe_recv(message)
  @udp.send_maybe_recv(message)
rescue TooBig
  @tcp.send_maybe_recv(message)
end
send_recv(message) click to toggle source
# File lib/riemann/client.rb, line 120
def send_recv(message)
  @tcp.send_recv(message)
end
timeout() click to toggle source
# File lib/riemann/client.rb, line 50
def timeout
  @options[:timeout]
end