Class: NSCA::Client::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/nsca/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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
	iv_key_and_timestamp = @socket.recv 132
	@iv_key, ts = iv_key_and_timestamp.unpack 'a128N'
	@timestamp = Time.at ts
	@password = opts[:pass]
end

Instance Attribute Details

#iv_keyObject (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_versionObject (readonly)

Returns the value of attribute packet_version.



4
5
6
# File 'lib/nsca/client.rb', line 4

def packet_version
  @packet_version
end

#passwordObject (readonly)

Returns the value of attribute password.



4
5
6
# File 'lib/nsca/client.rb', line 4

def password
  @password
end

#socketObject (readonly)

Returns the value of attribute socket.



4
5
6
# File 'lib/nsca/client.rb', line 4

def socket
  @socket
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



4
5
6
# File 'lib/nsca/client.rb', line 4

def timestamp
  @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.

Parameters:

  • timestamp (Time, Integer, nil)

    Checked at this time

  • return_code (0..3)

    ‘NSCA::ReturnCode`

  • String(length<64),nil] (String(length<64),nil] hostname If nil, local hostname will be used. Must be known by Nagios.)

    hostname If nil, local hostname will be used. Must be known by Nagios.

  • String(length<128)] (String(length<128)] service Name of Service. Must be known by Nagios.)

    service Name of Service. Must be known by Nagios.

  • String(length<512)] (String(length<512)] status Status-line inclusive optional Performance Data.)

    status Status-line inclusive optional Performance Data.



62
63
64
65
# File 'lib/nsca/client.rb', line 62

def build_packet timestamp, return_code, hostname, service, status
	packet = @packet_version.new timestamp || @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

Parameters:



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

def send *results
	results.flatten.each do |r|
		send_packet r.timestamp, r.retcode, r.hostname, r.service, r.text
	end
end

#send_packet(*a) ⇒ Object

Sends a check-result.

See Also:



69
# File 'lib/nsca/client.rb', line 69

def send_packet( *a)  @socket.write build_packet( *a)  end