Class: SensuGenerator::Client
- Inherits:
-
Object
- Object
- SensuGenerator::Client
- Defined in:
- lib/sensu_generator/client.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connection ⇒ Object
- #flush_results ⇒ Object
-
#initialize ⇒ Client
constructor
A new instance of Client.
- #write_file(data) ⇒ Object
Constructor Details
#initialize ⇒ Client
Returns a new instance of Client.
6 7 8 9 10 |
# File 'lib/sensu_generator/client.rb', line 6 def initialize @logger = Application.logger @config = Application.config connection end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
12 13 14 |
# File 'lib/sensu_generator/client.rb', line 12 def config @config end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
12 13 14 |
# File 'lib/sensu_generator/client.rb', line 12 def logger @logger end |
Instance Method Details
#close ⇒ Object
44 45 46 47 48 |
# File 'lib/sensu_generator/client.rb', line 44 def close @connection.close @connection = nil logger.info "Client: connection closed" end |
#connect ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/sensu_generator/client.rb', line 18 def connect logger.info "Client: connecting to server #{server_addr}:#{server_port}" s = TCPSocket.new(server_addr, server_port) logger.info "Client: connected" s rescue => e raise ClientError, "Client: connection failed #{e.inspect} #{e.backtrace}\n" end |
#connection ⇒ Object
14 15 16 |
# File 'lib/sensu_generator/client.rb', line 14 def connection @connection ||= connect end |
#flush_results ⇒ Object
36 37 38 39 40 41 42 |
# File 'lib/sensu_generator/client.rb', line 36 def flush_results connection.puts JSON.fast_generate({"FLUSH_WITH_PREFIX" => "#{config.file_prefix}" }) rescue => e close raise ClientError, "Client: write failed #{e.inspect} #{e.backtrace}\n" close end |
#write_file(data) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/sensu_generator/client.rb', line 27 def write_file(data) connection.puts data logger.info "Client: data transferred successfully" true rescue => e close raise ClientError, "Client: write failed #{e.inspect} #{e.backtrace}\n" end |