Class: Riemann::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/riemann/client.rb,
lib/riemann/client/tcp.rb,
lib/riemann/client/udp.rb,
lib/riemann/client/ssl_socket.rb,
lib/riemann/client/tcp_socket.rb

Direct Known Subclasses

TCP, UDP

Defined Under Namespace

Classes: Error, InvalidResponse, SSLSocket, ServerError, TCP, TcpSocket, TooBig, UDP, Unsupported

Constant Summary collapse

HOST =
'127.0.0.1'
PORT =
5555
TIMEOUT =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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

Instance Attribute Details

#tcpObject (readonly)

Returns the value of attribute tcp.



23
24
25
# File 'lib/riemann/client.rb', line 23

def tcp
  @tcp
end

#udpObject (readonly)

Returns the value of attribute udp.



23
24
25
# File 'lib/riemann/client.rb', line 23

def udp
  @udp
end

Instance Method Details

#<<(event) ⇒ Object

Send a state



55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object

Returns an array of states matching query.



94
95
96
97
98
# File 'lib/riemann/client.rb', line 94

def [](query)
  response = query(query)
  (response.events || []) |
    (response.states || [])
end

#bulk_send(events) ⇒ Object

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
# 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

#closeObject

Close both UDP and TCP sockets.



106
107
108
109
# File 'lib/riemann/client.rb', line 106

def close
  @udp.close
  @tcp.close
end

#connectObject



100
101
102
103
# File 'lib/riemann/client.rb', line 100

def connect
  # NOTE: connections are made automatically on send
  warn 'Riemann client#connect is deprecated'
end

#connected?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'lib/riemann/client.rb', line 111

def connected?
  tcp.connected? and udp.connected?
end

#hostObject



42
43
44
# File 'lib/riemann/client.rb', line 42

def host
  @options[:host]
end

#normalize_events(events) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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

#portObject



46
47
48
# File 'lib/riemann/client.rb', line 46

def port
  @options[:port]
end

#query(string = 'true') ⇒ Object

Ask for states



116
117
118
# 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) ⇒ Object



124
125
126
127
128
# 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) ⇒ Object



120
121
122
# File 'lib/riemann/client.rb', line 120

def send_recv(message)
  @tcp.send_recv(message)
end

#timeoutObject



50
51
52
# File 'lib/riemann/client.rb', line 50

def timeout
  @options[:timeout]
end