Class: Riemann::Client
- Inherits:
-
Object
- Object
- Riemann::Client
- Defined in:
- lib/riemann/client.rb,
lib/riemann/client/tcp.rb,
lib/riemann/client/udp.rb,
lib/riemann/client/tcp_socket.rb
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
-
#tcp ⇒ Object
readonly
Returns the value of attribute tcp.
-
#udp ⇒ Object
readonly
Returns the value of attribute udp.
Instance Method Summary collapse
-
#<<(event_opts) ⇒ Object
Send a state.
-
#[](query) ⇒ Object
Returns an array of states matching query.
-
#close ⇒ Object
Close both UDP and TCP sockets.
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #host ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #port ⇒ Object
-
#query(string = "true") ⇒ Object
Ask for states.
- #send_maybe_recv(*a) ⇒ Object
- #send_recv(*a) ⇒ Object
- #timeout ⇒ Object
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
#tcp ⇒ Object (readonly)
Returns the value of attribute tcp.
21 22 23 |
# File 'lib/riemann/client.rb', line 21 def tcp @tcp end |
#udp ⇒ Object (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 = Riemann::Message.new :events => [event] # Transmit send_maybe_recv 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 |
#close ⇒ Object
Close both UDP and TCP sockets.
86 87 88 89 |
# File 'lib/riemann/client.rb', line 86 def close @udp.close @tcp.close end |
#connect ⇒ Object
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
91 92 93 |
# File 'lib/riemann/client.rb', line 91 def connected? tcp.connected? and udp.connected? end |
#host ⇒ Object
40 41 42 |
# File 'lib/riemann/client.rb', line 40 def host @options[:host] end |
#port ⇒ Object
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 |
#timeout ⇒ Object
48 49 50 |
# File 'lib/riemann/client.rb', line 48 def timeout @options[:timeout] end |