Class: HAProxy::SocketReader

Inherits:
StatsReader show all
Defined in:
lib/haproxy/socket_reader.rb

Constant Summary collapse

TYPES =
{
  :frontend => 1,
  :backend => 2,
  :server => 4
}

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ SocketReader

Returns a new instance of SocketReader.

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/haproxy/socket_reader.rb', line 8

def initialize(path)
  raise ArgumentError, "Socket #{path} doesn't exists or is not a UNIX socket" unless File.exists?(path) and File.socket?(path)
  @path = path
end

Instance Method Details

#backendsObject



75
76
77
# File 'lib/haproxy/socket_reader.rb', line 75

def backends
  stats :frontend, :proxy => :all, :server => :all
end

#errorsObject



22
23
24
25
26
27
28
# File 'lib/haproxy/socket_reader.rb', line 22

def errors
  returning("") do |errors|
    send_cmd "show errors" do |line|
      errors << line
    end
  end
end

#frontendsObject



71
72
73
# File 'lib/haproxy/socket_reader.rb', line 71

def frontends
  stats :frontend, :proxy => :all, :server => :all
end

#infoObject



13
14
15
16
17
18
19
20
# File 'lib/haproxy/socket_reader.rb', line 13

def info
  returning({}) do |info|
    send_cmd "show info" do |line|
      key, value = line.split(': ')
      info[key.downcase.gsub('-', '_').to_sym] = value
    end
  end
end

#serversObject



79
80
81
# File 'lib/haproxy/socket_reader.rb', line 79

def servers
  stats :server, :proxy => :all, :server => :all
end

#sessionsObject



30
31
32
33
34
35
36
# File 'lib/haproxy/socket_reader.rb', line 30

def sessions
  returning([]) do |sess|
    send_cmd "show sess" do |line|
      sess << line
    end
  end
end

#stats(types = [:all], options = {}) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/haproxy/socket_reader.rb', line 44

def stats(types=[:all], options={})
  params = {
    :proxy => :all,
    :server => :all
  }.merge(options)

  params[:proxy] = "-1" if params[:proxy].eql?(:all)
  params[:server] = "-1" if params[:server].eql?(:all)

  types = [types] unless types.is_a?(Array)

  params[:type] = case
                  when types.eql?([:all])
                    "-1"
                  else
                    types.map{ |type| TYPES[type] }.inject(:+)
                  end

  cmd = "show stat #{params[:proxy]} #{params[:type]} #{params[:server]}"

  returning([]) do |stats|
    send_cmd(cmd) do |line|
      stats << CSVParser.parse(line) unless line.start_with?('#')
    end
  end
end