Class: HAProxyManager::Instance

Inherits:
Object
  • Object
show all
Defined in:
lib/haproxy_manager/instance.rb

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Instance

Returns a new instance of Instance.



4
5
6
7
8
9
# File 'lib/haproxy_manager/instance.rb', line 4

def initialize(socket)
  @socket = HAPSocket.new(socket)
  @print_response = Proc.new {|response| puts response}
  backends = @socket.execute( "show stat -1 4 -1" )[1..-1].collect{|item| item.split(",")[0..1]}
  @backends  = backends.inject({}){|hash, items| (hash[items[0]] ||=[]) << items[1]; hash}
end

Instance Method Details

#backendsObject



28
29
30
# File 'lib/haproxy_manager/instance.rb', line 28

def backends
  @backends.keys
end

#disable(serverid, backend = nil) ⇒ Object

Diables a server in the server in a backend for maintenance. If backend is not specified then all the backends in which the serverid exists are disabled. A disabled server shows up as in Maintance mode.



14
15
16
17
18
# File 'lib/haproxy_manager/instance.rb', line 14

def disable(serverid, backend = nil)
  all_servers(serverid, backend).each do |item|
    @socket.execute "disable server #{item[0]}/#{item[1]}", &@print_response
  end
end

#enable(serverid, backend = nil) ⇒ Object

Enables a server in the server in a backend. If backend is not specified then all the backends in which the serverid exists are enabled.



22
23
24
25
26
# File 'lib/haproxy_manager/instance.rb', line 22

def enable(serverid, backend = nil)
  all_servers(serverid, backend).each do |item|
    @socket.execute "enable server #{item[0]}/#{item[1]}", &@print_response
  end
end

#infoObject



32
33
34
# File 'lib/haproxy_manager/instance.rb', line 32

def info
  @socket.execute( "show info").inject({}){|hash, item| x = item.split(":"); hash.merge(x[0].strip =>  x[1].strip)}
end

#reset_counters(option = "") ⇒ Object

resets Haproxy counters. If no option is specified backend and frontend counters are cleared, but cumulative counters are not cleared. The cumulative counters can be cleared by passing the option of :all to the method, in that case all the counters are cleared. This is similar to a restart. This is useful to reset stats after for example an incident.



70
71
72
# File 'lib/haproxy_manager/instance.rb', line 70

def reset_counters(option = "")
  @socket.execute "clear counters {option}", &@print_response
end

#servers(backend = nil) ⇒ Object



62
63
64
# File 'lib/haproxy_manager/instance.rb', line 62

def servers(backend = nil)
  backend.nil? ? @backends.values.flatten : @backends[backend]
end

#statsObject



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/haproxy_manager/instance.rb', line 49

def stats
  stats = @socket.execute( "show stat -1 -1 -1" )
  headers = stats[0].split(",")
  stats[1..-1].inject({}) do |hash, line|
    data = line.split(","); backend = data[0]; server = data[1]; rest = data[2..-1]
    hash[backend] = {} if( hash[backend].nil?)
    hash[backend][server] = {}.tap do |server_hash|
      headers[2..-1].each_with_index{|x, i| server_hash[x]= rest[i]}
    end
    hash
  end
end

#weights(server, backend, weight = nil) ⇒ Object

Sets weight for the server. If a numeric value is provider, that will become the absolute weight. It can be between 0 -256 If a weight has been provided ending with % then the weight is reduced by that percentage. It has to be between 0% - 100% Weight of a server defines, how many requests are passed to it.



39
40
41
42
43
44
45
46
47
# File 'lib/haproxy_manager/instance.rb', line 39

def weights(server, backend, weight=nil)
  if(weight.nil?)
    weight = @socket.execute "get weight #{backend}/#{server}"
    /(\d*)\s\(initial\s(\d*)\)/.match( weight[0])
    {:current => $1.to_i, :initial => $2.to_i}
  else
    @socket.execute "set weight #{backend}/#{server} #{weight}"
  end
end