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:



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, try_again = true) ⇒ 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
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/haproxystats/socket.rb', line 19

def run(command, try_again=true)
  begin
    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
    if first_char
      out = first_char + sock.read
    end
    sock.close
    out
  rescue Errno::EPIPE
    if try_again 
      sleep 0.2
      sock.close
      run(command, try_again=false)
    else
      raise IOError, "IO Error 32 with socket"
    end
  end
end

#show_infoObject

Show info from haproxy



47
48
49
# File 'lib/haproxystats/socket.rb', line 47

def show_info
    run('show info')
end