Class: SensuGenerator::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

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

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/sensu_generator/client.rb', line 12

def config
  @config
end

#loggerObject (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

#closeObject



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

#connectObject



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

#connectionObject



14
15
16
# File 'lib/sensu_generator/client.rb', line 14

def connection
  @connection ||= connect
end

#flush_resultsObject



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