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
32
33
34
35
36
37
38
# 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)
  if block_given?
    begin
      yield self
    ensure
      close
    end
  end
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



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/riemann/client.rb', line 53

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.



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

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

#closeObject

Close both UDP and TCP sockets.



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

def close
  @udp.close
  @tcp.close
end

#connectObject



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

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

#connected?Boolean

Returns:

  • (Boolean)


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

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

#hostObject



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

def host
  @options[:host]
end

#portObject



44
45
46
# File 'lib/riemann/client.rb', line 44

def port
  @options[:port]
end

#query(string = "true") ⇒ Object

Ask for states



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

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

#send_maybe_recv(*a) ⇒ Object



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

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

#send_recv(*a) ⇒ Object



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

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

#timeoutObject



48
49
50
# File 'lib/riemann/client.rb', line 48

def timeout
  @options[:timeout]
end