Class: Riemann::Client

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

Direct Known Subclasses

TCP, UDP

Defined Under Namespace

Classes: Error, InvalidResponse, ServerError, TCP, 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.



23
24
25
26
27
28
29
# File 'lib/riemann/client.rb', line 23

def initialize(opts = {})
  @host = opts[:host] || HOST
  @port = opts[:port] || PORT
  @timeout = opts[:timeout] || TIMEOUT
  @udp = UDP.new opts
  @tcp = TCP.new opts
end

Instance Attribute Details

#hostObject

Returns the value of attribute host.



21
22
23
# File 'lib/riemann/client.rb', line 21

def host
  @host
end

#portObject

Returns the value of attribute port.



21
22
23
# File 'lib/riemann/client.rb', line 21

def port
  @port
end

#tcpObject

Returns the value of attribute tcp.



21
22
23
# File 'lib/riemann/client.rb', line 21

def tcp
  @tcp
end

#timeoutObject

Returns the value of attribute timeout.



21
22
23
# File 'lib/riemann/client.rb', line 21

def timeout
  @timeout
end

#udpObject

Returns the value of attribute udp.



21
22
23
# File 'lib/riemann/client.rb', line 21

def udp
  @udp
end

Instance Method Details

#<<(event_opts) ⇒ Object

Send a state



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/riemann/client.rb', line 32

def <<(event_opts)
  # Create state
  case event_opts
  when Riemann::State
    event = event_opts
  when Riemann::Event
    event = event_opts
  else
    unless event_opts.include? :host
      event_opts[:host] = Socket.gethostname
    end
    event = Riemann::Event.new(event_opts)
  end

  message = Riemann::Message.new :events => [event]

  # Transmit
  send_maybe_recv message
end

#[](query) ⇒ Object

Returns an array of states matching query.



53
54
55
56
57
# File 'lib/riemann/client.rb', line 53

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

#closeObject

Close both UDP and TCP sockets.



60
61
62
63
# File 'lib/riemann/client.rb', line 60

def close
  @udp.close
  @tcp.close
end

#connectObject

Connect both UDP and TCP sockets.



66
67
68
69
# File 'lib/riemann/client.rb', line 66

def connect
  udp.connect
  tcp.connect
end

#connected?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/riemann/client.rb', line 71

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

#query(string = "true") ⇒ Object

Ask for states



76
77
78
# File 'lib/riemann/client.rb', line 76

def query(string = "true")
  send_recv Riemann::Message.new(:query => Riemann::Query.new(:string => string))
end

#send_maybe_recv(*a) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/riemann/client.rb', line 84

def send_maybe_recv(*a)
  begin
    @udp.send_maybe_recv *a
  rescue TooBig
    @tcp.send_maybe_recv *a
  end
end

#send_recv(*a) ⇒ Object



80
81
82
# File 'lib/riemann/client.rb', line 80

def send_recv(*a)
  @tcp.send_recv *a
end