Class: NSCA::Client::Connection
- Inherits:
-
Object
- Object
- NSCA::Client::Connection
- Defined in:
- lib/nsca/client.rb
Instance Attribute Summary collapse
-
#iv_key ⇒ Object
readonly
Returns the value of attribute iv_key.
-
#packet_version ⇒ Object
readonly
Returns the value of attribute packet_version.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#socket ⇒ Object
readonly
Returns the value of attribute socket.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
Class Method Summary collapse
Instance Method Summary collapse
-
#build_packet(timestamp, return_code, hostname, service, status) ⇒ Object
Builds a check-result-line for NSCA.
-
#close(*a) ⇒ Object
Closes connection to NSCA.
-
#initialize(*args) ⇒ Connection
constructor
opts must be a hash Connection.new host, port [, opts] Connection.new socket [, opts] Connection.new host, opts # need ‘opts = Port`! Connection.new opts # need `opts = Port, hostname: Hostname`! Connection.new opts # need `opts = Port, socket: Socket`!.
-
#send(*results) ⇒ Object
Sends check-results.
-
#send_packet(*a) ⇒ Object
Sends a check-result.
Constructor Details
#initialize(*args) ⇒ Connection
opts must be a hash Connection.new host, port [, opts] Connection.new socket [, opts] Connection.new host, opts # need ‘opts = Port`! Connection.new opts # need `opts = Port, hostname: Hostname`! Connection.new opts # need `opts = Port, socket: Socket`!
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/nsca/client.rb', line 22 def initialize *args opts = {} opts = args.pop.dup if args.last.is_a? Hash opts[:host] ||= opts[:hostname] opts[:sock] ||= opts[:socket] opts[:pass] ||= opts[:password] case args[0] when String opts[:host] = args[0] opts[:port] ||= args[1] when IO opts[:sock] = args[0] end @socket = if opts[:sock].is_a? IO opts[:sock] elsif opts[:host].is_a? String TCPSocket.new opts[:host], opts[:port] else raise ArgumentError, "Socket or hostname+port expected." end @packet_version = opts[:packet_version] || PacketV3 # read iv_key and timestamp = @socket.recv 132 @iv_key, ts = .unpack 'a128N' @timestamp = Time.at ts @password = opts[:pass] end |
Instance Attribute Details
#iv_key ⇒ Object (readonly)
Returns the value of attribute iv_key.
4 5 6 |
# File 'lib/nsca/client.rb', line 4 def iv_key @iv_key end |
#packet_version ⇒ Object (readonly)
Returns the value of attribute packet_version.
4 5 6 |
# File 'lib/nsca/client.rb', line 4 def packet_version @packet_version end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
4 5 6 |
# File 'lib/nsca/client.rb', line 4 def password @password end |
#socket ⇒ Object (readonly)
Returns the value of attribute socket.
4 5 6 |
# File 'lib/nsca/client.rb', line 4 def socket @socket end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
4 5 6 |
# File 'lib/nsca/client.rb', line 4 def @timestamp end |
Class Method Details
.open(*args) ⇒ Object
6 7 8 9 10 11 12 13 14 |
# File 'lib/nsca/client.rb', line 6 def self.open *args conn = new *args if block_given? begin yield conn ensure conn && conn.close end else conn end end |
Instance Method Details
#build_packet(timestamp, return_code, hostname, service, status) ⇒ Object
Builds a check-result-line for NSCA.
Will be terminated by end-of-terminate.
62 63 64 65 |
# File 'lib/nsca/client.rb', line 62 def build_packet , return_code, hostname, service, status packet = @packet_version.new || @timestamp, return_code, hostname, service, status packet.build @iv_key, @password end |
#close(*a) ⇒ Object
Closes connection to NSCA.
80 |
# File 'lib/nsca/client.rb', line 80 def close( *a) @socket.close( *a) end |
#send(*results) ⇒ Object
Sends check-results
73 74 75 76 77 |
# File 'lib/nsca/client.rb', line 73 def send *results results.flatten.each do |r| send_packet r., r.retcode, r.hostname, r.service, r.text end end |
#send_packet(*a) ⇒ Object
Sends a check-result.
69 |
# File 'lib/nsca/client.rb', line 69 def send_packet( *a) @socket.write build_packet( *a) end |