Failover WAN entre dois links

Publicado por Phillipe Farias Costa (última atualização em 02/03/2017)

[ Hits: 2.256 ]

Homepage: br.linkedin.com/pub/phillipe-farias/53/b2a/13b/

Download failover_wan.sh




Script desenvolvido para realizar o processo de FailOver WAN entre dois links.

Como requisito, é necessário a instalação do software sendEmail para envio dos logs por e-mail. Entretanto, o mesmo pode ser alterado a forma de envio.

  



Esconder código-fonte

#!/bin/bash
#===============================================================================#
#                               FailOver WAN                                                                                                                # 
#                                                                                                                                                                      #
# Created by.: pH                                                                                                                                            #
# Contact.: phillipe@phconsultoria.com.br                                                                                                     #
# Version.: 1.0                                                                                                                                                 #
#                                                                                                                                                                      #
# Requirements.: Application sendEmail installed                                                                                          #
#                                                                                                                                                                      #
#===============================================================================#

#==============#
#=== Settings ===#
#==============#

#=== General ===#

log=/var/log/failover_wan.log # Set the file log
check_delay="120" # Set the time for check gateways
gw01name="gwoperator01" # Name for Gateway (Telecom Operator)
gw02name="gwoperator02" # Name for Gateway (Telecom Operator)
packagelost=20 #Set the packages lost for gateways

#=== Network ===#

gw01=xxx.xxx.xxx.xxx # Primary Gateway
gw02=xxx.xxx.xxx.xxx # Secondary Gateway
iface01=ethx # Network adapter for gw01
iface02=ethx # Network adapter for gw02

#=== Mail Settings ===#

server="mail.domain.com" # Mail server FQDN
port="587" # Port mail server (SMTP)
user="no-reply@domain.com" # Account of domain
password="password" # Passoword for account fo domain
sender="no-reply@domain.com" # User Sender
recipients="support@domain.com" # Account you received the email
cc="it@domain.com" # Another Account you received the email (With copy)
                                        
#=== Commands ===#

ip=/sbin/ip # ip command (full path)
ping=/bin/ping # ping command ( full path )
grep=/bin/grep # grep command ( full path )
awk=/usr/bin/awk #awk command ( full path )
cut=/bin/cut # cut command ( full path )
sleep=/usr/bin/sleep # sleep command ( full path )
tail=/bin/tail # tail command ( full path )
sendemail=/usr/local/bin/sendEmail # sendEmail comand ( full path )

#=== Monitor ===#

link01_state=$($ping -c 10 -I $iface01 $ip_monitor | $awk '{ print }' | $grep "0% packet loss" | $awk '{ print $6 $7 $8 }' | $cut -d % -f 1)
link02_state=$($ping -c 10 -I $iface02 $ip_monitor | $awk '{ print }' | $grep "0% packet loss" | $awk '{ print $6 $7 $8 }' | $cut -d % -f 1)
ip_monitor=200.221.2.45 # Set the IP Monitor. By default, is usaged the IP Address of uol.com.br.

#==================================#
#===    Primary Configuration   ===#
#==================================#

# The primary configuration, apply the traffic for gateway gw01.

$ip route del default via $gw01
$ip route replace default via $gw01

#===============================#
#=== Start FailOver Function ===#
#===============================#

failover ()
{

while [ 1 ]
do

        # Checking the communication with WAN Primary

              if [ $link01_state -le $packagelost ]
       
                    then

                          $ip route replace default via $gw01

                    else

                          #==========================================#
                          #== Change the traffic for gateway gw02 ===#
                          #==========================================#

                          $ip route replace default via $gw02

                          #=========================================#
                          #=== Check if the secundary link is UP ===#
                          #=========================================#

                          if [ $link02_state -le $packagelost ]
               
                                  then

                                        echo "#================================================================================================#" >> $log
                                        echo "#                                         FailOver Monitor                                       #" >> $log
                                        echo "#                                                                                                #" >> $log
               echo "# The secundary Link ($gw02name) is UP on `date +%d/%m/%y-%H:%M:%S`                              #" >> $log
                                        echo "#                                                                                                #" >> $log
                                        echo "# Summary.:                                                                                      #" >> $log
                                        echo "#                                                                                                #" >> $log
                                        echo "# The Primary Link $gw01name it is DOWN with $link01_state % Packages Lost!                      #" >> $log
                                        echo "#                                                                                                #" >> $log
               echo "#================================================================================================#" >> $log
                                        echo >> $log
                                        echo >> $log
                        
                                        #==========================#
                                        #===    Send Logs       ===#
                                        #==========================#

                                        subject="FailOver Monitor - Primary Link is Down!"
 
                                        $tail -n 12 $log | $sendemail -s "$server:$port" -o tls=no -xu "$user" -xp "$password" -f "$sender" -u "$subject" -t "$recipients" -cc "$cc"


                                        $sleep $check_delay

                                        #==========================================#
                                        #=== Check if the primary link Recovery ===#
                                        #==========================================#

                                        $ip route replace $ip_monitor/32 via $gw01

                                                  if [ $link01_state -le $packagelost ]
                       
                                                          then

                                                                  $ip route replace default via $gw01
                                                                  $ip route del $ip_monitor/32 via $gw01

                                                                  echo "#================================================================================================#" >> $log
                                                                  echo "#                                        FailOver Monitor                                        #" >> $log
                                                                  echo "#                                                                                                #" >> $log
                          echo "# The primary Link ( $gw01name) RECOVERY on `date +%d/%m/%y-%H:%M:%S`                            #" >> $log
                                                                  echo "#                                                                                                #" >> $log
                          echo "#================================================================================================#" >> $log
                                                                  echo >> $log
                                                                  echo >> $log
                        
                                                                  #==========================#
                                                                  #===     Send Logs      ===#
                                                                  #==========================#

                                                                  subject="FailOver Monitor - Primary Link Recovery!"
 
                                                                  $tail -n 12 $log | $sendemail -s "$server:$port" -o tls=no -xu "$user" -xp "$password" -f "$sender" -u "$subject" -t "$recipients" -cc "$cc"


                                                          else 
        
                                                   echo "#================================================================================================#" >> $log
                                                   echo "#                                      FailOver Monitor                                          #" >> $log
                                                   echo "#                                                                                                #" >> $log
                          echo "# The primary Link ( $gw01name) remains DOWN on `date +%d/%m/%y-%H:%M:%S`                        #" >> $log
                                                   echo "#                                                                                                #" >> $log
                          echo "#================================================================================================#" >> $log
                                                                  echo >> $log
                                                                  echo >> $log
                        
                                                                  #==========================#
                                                                  #===     Send Logs      ===#
                                                                  #==========================#

                                                                  subject="FailOver Monitor - Primary Link Remains Down!"
 
                                                                  $tail -n 12 $log | $sendemail -s "$server:$port" -o tls=no -xu "$user" -xp "$password" -f "$sender" -u "$subject" -t "$recipients" -cc "$cc"

                                                  fi


                          fi


              fi

$sleep $check_delay

done

}

failover

Scripts recomendados

Dell 1390 WLAN (Broadcom 4311)

Manutencao de arquivos no Squid-Cache

shradio.sh - ouça rádios online

Avisos de segurança do espaço em uso!

Barra de progresso em shell script com cores roxa e verde


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts