Class: HAProxySocket

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxystats/socket.rb

Overview

Container class for the socket Intended to be inherited by othere classes that require use fo the stats socket (e.g. HAProxyStats)

Direct Known Subclasses

HAProxyStats

Instance Method Summary collapse

Constructor Details

#initialize(location) ⇒ HAProxySocket

Check we have a socket at location and that we can open it

Raises:

  • (ArgumentError)


12
13
14
15
16
# File 'lib/haproxystats/socket.rb', line 12

def initialize(location)
  @location = location
  raise ArgumentError, "#{location} does not appear to be a socket." unless File.socket?(location)
  raise IOError, 'Cannot read/write to socket at ${location}' unless show_info
end

Instance Method Details

#run(command) ⇒ Object

Open a socket, run a command, collect output and close



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/haproxystats/socket.rb', line 19

def run(command)
  sock = UNIXSocket.new @location
  sock.puts command
  out = ''
  first_char = sock.read 2
  if first_char == '# '
    out = CSV.parse(sock.read)
    sock.close
    return out
  end
  out = first_char + sock.read
  sock.close
  out
end

#show_infoObject

Show info from haproxy



35
36
37
# File 'lib/haproxystats/socket.rb', line 35

def show_info
    run('show info')
end