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/tcp_socket.rb

Direct Known Subclasses

TCP, UDP

Defined Under Namespace

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



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

def initialize(opts = {})
  @options = opts.dup
  @options[:host] ||= HOST
  @options[:port] ||= PORT
  @options[:timeout] ||= TIMEOUT

  @udp = UDP.new(@options)
  @tcp = TCP.new(@options)
end

Instance Attribute Details

#tcpObject (readonly)

Returns the value of attribute tcp.



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

def tcp
  @tcp
end

#udpObject (readonly)

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/riemann/client.rb', line 46

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.



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

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

#closeObject

Close both UDP and TCP sockets.



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

def close
  @udp.close
  @tcp.close
end

#connectObject

Connect both UDP and TCP sockets.



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

def connect
  udp.connect
  tcp.connect
end

#connected?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/riemann/client.rb', line 85

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

#hostObject



33
34
35
# File 'lib/riemann/client.rb', line 33

def host
  @options[:host]
end

#portObject



37
38
39
# File 'lib/riemann/client.rb', line 37

def port
  @options[:port]
end

#query(string = "true") ⇒ Object

Ask for states



90
91
92
# File 'lib/riemann/client.rb', line 90

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

#send_maybe_recv(*a) ⇒ Object



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

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

#send_recv(*a) ⇒ Object



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

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

#timeoutObject



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

def timeout
  @options[:timeout]
end