class Amphibian::Runner

Public Class Methods

new(balancer_manager_url, test_page='/', test_regex=nil, dry_run=false) click to toggle source

TODO: log everything, and set log level, so we don’t have to print anything TODO: Email at the end

# File lib/amphibian/runner.rb, line 6
def initialize(balancer_manager_url, test_page='/', test_regex=nil, dry_run=false)
  @balancer_manager_url = balancer_manager_url
  @test_page = test_page
  @test_regex = test_regex
  @dry_run = dry_run
  @errors = []

  @balancer_manager = BalancerManager.new(@balancer_manager_url, @dry_run)

  @min_hosts = -1
end

Public Instance Methods

do_check() click to toggle source
# File lib/amphibian/runner.rb, line 18
def do_check
  check
end

Private Instance Methods

check() click to toggle source

Checks the balancer members, enabling/disabling each depending on the status.

# File lib/amphibian/runner.rb, line 25
def check
  puts
  puts "Load Apache Balancer-Manager at '#{@balancer_manager_url}'"
  puts
  puts "Loading Details"
  puts
  puts "  Balancer: #{get_balancer_manager.balancer_name}"
  puts
  get_balancer_manager.hosts_with_status.each{|h,s| puts "  #{h} => #{s}"}

  @live_hosts = get_balancer_manager.enabled_hosts
  puts
  puts "Checking #{@live_hosts.size} live hosts"
  puts

  @live_hosts.each do |host, state|
    status = check_host(host, @test_page)
    puts "  #{host} is #{status ? 'OK' : 'not responsive. Disabling via BalancerManager.'}"
    disable_host(host) if !status
  end
  puts
  puts "#{@live_hosts.size}/#{get_balancer_manager.hosts.size} hosts are enabled"
  puts
end
check_host(host, path = '/', timeout = 5) click to toggle source

Checks a host with an optional path. Checks for a 200 response, and that the body of the response matches the test regex, if defined.

# File lib/amphibian/runner.rb, line 62
def check_host(host, path = '/', timeout = 5)
  begin
    status = Timeout::timeout(timeout) do
      Net::HTTP.start(URI.parse(host).host) do |http|
        response = http.get(path)

        if not response.code.match(/200/)
          log_error("Web Server down or not responding: #{response.code} #{response.message}")
          return false;
        end

        if @test_regex && ! response.body.match(@test_regex)
          log_error("The response did not contain the regex '#{@test_regex}'")
          return false
        end
      end
    end
  rescue SocketError => socket_error
    log_error("Error Connecting To Web: #{socket_error}")
    return false;
  rescue TimeoutError => timeout_error
    log_error("Timeout occured checking #{host}#{path} after #{timeout} seconds.")
    return false;
  rescue Exception => e
    log_error("An unknown error occured checking #{host}#{path}: #{e}")
    return false;
  end

  return true
end
disable_host(host) click to toggle source

Disables a host from the balancer.

# File lib/amphibian/runner.rb, line 94
def disable_host(host)
  if @live_hosts.size <= @min_hosts
    puts "Will not take #{host} down, alreay at lower limit #{@min_hosts}"
    return
  end

  #puts "Disabling host '#{host}'"
  get_balancer_manager.disable_host(host)
end
get_balancer_manager() click to toggle source

Returns the BalancerManager object.

# File lib/amphibian/runner.rb, line 56
def get_balancer_manager
  @balancer_manager
end
log_error(error) click to toggle source
# File lib/amphibian/runner.rb, line 50
def log_error(error)
  @errors << error
  puts "ERROR: #{error}"
end