Class: LoadBalance

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.commandsObject (readonly)

Returns the value of attribute commands.



4
5
6
# File 'lib/load_balance/load_balance.rb', line 4

def commands
  @commands
end

Class Method Details

.build_commands(isps) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/load_balance/load_balance.rb', line 6

def build_commands(isps)
  @commands = []
  alive_isps = []
  isps.each do |isp|
	 if is_alive(isp) 
alive_isps << isp 
       IspUnityLog.info("#{isp.name} is online")
	 else
       IspUnityLog.info("#{isp.name} is offline")
	 end
  end
  if alive_isps.size == 1
    IspUnityLog.info("Only 1 isp is alive")
    @commands << "/sbin/ip route replace default via #{alive_isps[0].gateway} dev #{alive_isps[0].interface}" 
  else
    IspUnityLog.info("Multiple isps are alive")
    @commands << "/sbin/ip route replace default scope global "
    alive_isps.each do |isp|
      @commands[0] += " nexthop via #{isp.gateway} dev #{isp.interface} weight #{isp.weight} "  
    end
  end
  return @commands[0]
end

.is_alive(isp) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/load_balance/load_balance.rb', line 30

def is_alive(isp)
  return true if ENV['GEM_ENV']=='test'
  return false unless isp.enabled == 'true'
  result = `/bin/ping -c 3 -I #{isp.ip_address} #{$ip_cluster.sample}`
  if result.match("100% packet loss")
       IspUnityLog.info("100% packet loss for #{isp.name}")
      return false
  else
      IspUnityLog.info("0% packet loss for #{isp.name}")
      return true
  end
end