Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,61 @@ Installation
Add the following where approporiate(eg. deploy.rb)
require "haproxy_manager"

Release Notes
=============
Version 0.2 opens up a bunch of new possibilities by allowing the user to interact with the backend or server object
directly. Additionally the API is now more object oriented. Example:

```Ruby

serverobj.disable('foo-farm')

# get the stats for the server
serverobj.stats

# use any stat name and get value for all backends and calculate total across all backends
serverobj.smax => {"foo-farm/bin"=>"34839081", "foo-https-farm/bin"=>"2577996", "total"=>37417077}

```

API
======
```Ruby

haproxy = HAProxyManager::Instance.new ('path to haproxy socket')



# New API Commands 0.2 API
haproxy.backend_instances # Returns a hash of backend instances where the backend name is the key
haproxy.server_instances # Returns a hash of server instances where the server name is the key

# New Backend API (See class for more info)
foo_farm = haproxy.backend_instances('foo-farm')
foo_farm_servers = foo_farm.servers
foo_farm.up? # returns the status of the backend
foo_farm.up_count # returns the number of servers up in the backend
foo_farm.disable # disable the entire backend
foo_farm.smax # returns the value of smax for the backend (uses method missing)
foo_farm.bin # returns the value of bytes in for the backend (uses method missing)

# New Server API (See class for more info)
# because the server can be used in multiple backends many methods require the backend name to be passed in
all_servers = haproxy.server_instances
foo_farm_servers = haproxy.backend_instances('foo-farm').servers
foo_farm_servers.each do | name, server|
puts "Info for #{server.name} = #{server.status('foo-farm')}"
puts "Backends: #{server.backends.join("\n")}"
puts "#{server.weight('foo-farm')"
puts "#{server.smax}"
end

# New Hapsocket API 0.2 API
# The socket class has been moved to its own file and a new Class method has been added to determine if the socket is
# available should you need to determine if the socket can be opened
HAPSocket.available?('/home/haproxy/sockets/socketname') # returns boolean

# below is a list of API calls that were used primarly in the 0.1 API but are still valid in 0.2 API
haproxy.backends # Lists all the backends available
haproxy.servers("foo-farm") # List all the servers available in the given backend

Expand Down
2 changes: 1 addition & 1 deletion haproxy_manager.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Gem::Specification.new do |s|
s.name = "haproxy_manager"
s.version = HAProxyManager::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ["Sreekanth(sreeix)", "Smita Bhat(sbhat)"]
s.authors = ["Corey Osman(logicminds)", "Sreekanth(sreeix)", "Smita Bhat(sbhat)"]
s.email = ["gabbar@activesphere.com", "sbhat@altheasystems.com"]
s.homepage = "https://github.com/althea/haproxy-manager"
s.summary = 'HAproxy manager for controlling haproxy'
Expand Down
5 changes: 4 additions & 1 deletion lib/haproxy_manager.rb
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
require 'haproxy_manager/instance'
require 'haproxy_manager/instance'
require 'haproxy_manager/backend'
require 'haproxy_manager/server'
require 'haproxy_manager/hapsocket'
145 changes: 145 additions & 0 deletions lib/haproxy_manager/backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
require 'json'
module HAProxyManager
class Backend
attr_reader :name, :servers, :socket

def initialize(backend_name, socket_conn)
@name = backend_name
@print_response = Proc.new {|response| puts response}
@socket = socket_conn
@servers = {}
end

def server_names
servers.keys
end

# returns the number of servers in the pool no matter what status they have
def count
servers.length
end

def servers
if @servers.length < 1
srv_names = stats.keys - ['FRONTEND', 'BACKEND']
srv_names.each do | srv|
@servers[srv] = HAProxyManager::Server.new(srv, socket)
end
end
@servers
end

def has_server?(name)
servers.include?(name)
end

def method_missing(method, *args, &block)
if not backend_stats.has_key?(method.to_s)
raise NoMethodError
else
backend_stats[method.to_s]
end
end

def stats_to_json
JSON.pretty_generate(stats)
end

def backend_stats
stats['BACKEND']
end

def frontend_stats
stats['FRONTEND']
end

def stats
mystats = {}
stats_data = socket.execute( "show stat -1 -1 -1" )
headers = stats_data[0].split(",").collect do |name|
name.gsub('#', '').strip
end
stats_data[1..-1].inject({}) do |hash, line|
data = line.split(",")
if data.first == name
i = 0
datahash = {}
data.each do |item|
header = headers[i]
datahash[header] = item
i = i + 1
end
sv_name = datahash['svname']
mystats[sv_name] = datahash
end

end
mystats
end

def up?
status == 'UP'
end

def down?
status == 'DOWN'
end

def disable
servers.each do |key, server|
server.disable
end
end

def enable
servers.each do |key, server|
server.enable
end
end

def status
backend_stats['status']
end

def stats_names
stats.keys
end

# returns the number of servers that have status of up
def up_count
servers_up.length
end

# returns the number of servers that have status of down
def down_count
servers_down.length
end

# returns the number of servers that have status of maint
def maint_count
servers_maint.length
end

# returns an array of the servers that are currently up
def servers_up
servers.values.find_all do |server|
server.up?(name)
end
end

# returns an array of the servers that are currently down
def servers_down
servers.values.find_all do |server|
server.down?(name)
end
end

# returns an array of the servers that are currently in maint mode
def servers_maint
servers.values.find_all do |server|
server.maint?(name)
end
end

end
end
38 changes: 38 additions & 0 deletions lib/haproxy_manager/hapsocket.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'socket'
module HAProxyManager

class HAPSocket
def initialize(file)
@file = file
end

def execute(cmd, &block)
socket = UNIXSocket.new(@file)
socket.write("#{cmd};")
response = []
socket.each do |line|
data = line.strip
next if data.empty?
response << data
end
yield response if block_given?
response
end

# returns bool if socket is available
def self.available?(file)
HAPSocket.new(file).available?
end

# returns bool if socket is available
def available?
begin
execute('show info')
true
rescue Exception => e
false
end
end

end
end
Loading