Page 1 of 1

How to configure Failover between 2 PBXs on WMS 3.0

PostPosted: 29/04/2015, 8:35
by eugenio.chernolyev@wildix.com
Failover service allows you to connect a secondary PBX in redundancy mode so that to backup the primary PBX. In case the primary PBX becomes unreachable, all the devices will be re-registered automatically on the secondary PBX.
For the moment it is not possible to configure Failover from WEB interface. You need to do the following custom settings:

- copy the code given below inside a text file and upload that file on the Master PBX using Winscp (scp for Linux or Mac) :

Code: Select all#!/bin/bash

function valid_ip()
{
   local  ip=$1
   local  res=1

   if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
      OIFS=$IFS
      IFS='.'
      ip=($ip)
      IFS=$OIFS
      [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
         && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
      res=$?
   fi
   return $res
}

function get_ip_addresses
{
   # Get primary IP addresses
   while true; do
      printf "\E[33mEnter PRIMARY node IP address\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      valid_ip $answer
      if [ $? -eq 0 ]; then
         PRIMARY_IP=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done

   # Get secondary IP addresses
   while true; do
      printf "\E[33mEnter SECONDARY node IP address\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      valid_ip $answer
      if [ $? -eq 0 ]; then
         SECONDARY_IP=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done

   # Get virtual IP addresses
   while true; do
      printf "\E[33mEnter VIRTUAL node IP address\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      valid_ip $answer
      if [ $? -eq 0 ]; then
         VIRTUAL_IP=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done

   # Get DefaultGW
   while true; do
      printf "\E[33mEnter Default GW IP address\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      valid_ip $answer
      if [ $? -eq 0 ]; then
         DEFAULT_GW=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done

   # Get Network
   while true; do
      printf "\E[33mEnter network address\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      valid_ip $answer
      if [ $? -eq 0 ]; then
         NETWORK=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done

   # Get netmask
   while true; do
      printf "\E[33mEnter network mask (1 - 32)\E[37m: "
      read answer

      if [ ! $answer ]; then
         continue
      fi

      if [[ $answer -ge 1 && $answer -le 32 ]]; then
         NETMASK=$answer
         break
      else
         printf "\E[31mCheck your answer and try again\E[37m\n"
         continue
      fi
   done
}

function configure_failover
{
   local host_specific="/etc/kamailio/cfg.d/host_specific.cfg"
   local redundancy_cfg="/etc/kamailio/cfg.d/redundancy.cfg"
   local interfaces_cfg="/etc/network/interfaces"

   printf "\E[33mAdd the following line to /etc/sysctl.conf on both PRIMARY and SECONDARY servers and then run\E[37m\n"
   printf "net.ipv4.ip_nonlocal_bind=1\n"

   printf "\E[33m and then run\E[37m sysctl -p\n"

   ######## redundancy.cfg #########
   printf "\n"
   printf "\E[33mPut the following lines to the $redundancy_cfg on the PRIMARY server\E[37m\n"
   printf "modparam(\"pv\", \"varset\", \"redundancy_mode=i:1\")\n"
   printf "modparam(\"pv\", \"varset\", \"is_primary=i:1\")\n"
   printf "modparam(\"pv\", \"varset\", \"primary_host=s:$PRIMARY_IP\")\n"
   printf "modparam(\"pv\", \"varset\", \"backup_host=s:$SECONDARY_IP\")\n"
   printf "modparam(\"pv\", \"varset\", \"virtual_host=s:$VIRTUAL_IP\")\n"
   printf "listen=$VIRTUAL_IP:5060\n"
   printf "listen=lo\n"
   printf "listen=eth0\n"

   printf "\n"
   printf "\E[33mPut the following lines to the $redundancy_cfg on the SECONDARY server\E[37m\n"
   printf "modparam(\"pv\", \"varset\", \"redundancy_mode=i:1\")\n"
   printf "modparam(\"pv\", \"varset\", \"is_primary=i:0\")\n"
   printf "modparam(\"pv\", \"varset\", \"primary_host=s:$PRIMARY_IP\")\n"
   printf "modparam(\"pv\", \"varset\", \"backup_host=s:$SECONDARY_IP\")\n"
   printf "modparam(\"pv\", \"varset\", \"virtual_host=s:$VIRTUAL_IP\")\n"
   printf "listen=$VIRTUAL_IP:5060\n"
   printf "listen=lo\n"
   printf "listen=eth0\n"

   printf "\n"
   printf "\E[33mAfter editing $host_specific and $redundancy_cfg restart kamailio by running\E[37m\n /etc/init.d/kamailio restart\n"

   ####### /etc/network/interfaces  ########
   printf "\n"
   printf "\E[33mAdd following lines to the $interfaces_cfg to the end of the eth0 configuration on PRIMARY SERVER\E[37m\n"
   printf "post-up /usr/sbin/ucarp -i eth0 -s $PRIMARY_IP -v 67 -p ad5a18 -a $VIRTUAL_IP -u /etc/ucarp/ucarp-up.sh -d /etc/ucarp/ucarp-down.sh -z -b 4 -B -k 1 -r 3 -P\n"
   printf "down pkill ucarp\n"

   printf "\n"
   printf "\E[33mAdd following lines to the $interfaces_cfg to the end of the eth0 configuration on SECONDARY SERVER\E[37m\n"
   printf "post-up /usr/sbin/ucarp -i eth0 -s $SECONDARY_IP -v 67 -p ad5a18 -a $VIRTUAL_IP -u /etc/ucarp/ucarp-up.sh -d /etc/ucarp/ucarp-down.sh -z -b 4 -B -k 100 -r 3\n"
   printf "down pkill ucarp\n"

   ####### ucarp   ########
   printf "\n"
   printf "\E[33mSubstitute /etc/ucarp/ucarp-up.sh file of both PRIMARY and SECONDARY servers with the following content\E[37m\n"
   printf "#!/bin/sh\n"
   printf "exec 2> /dev/null\n"
   printf "\n"
   printf "VIPDEV=\$1\n"
   printf "VIPADDR=$VIRTUAL_IP\n"
   printf "VIPNET=$NETWORK\n"
   printf "VIPMASK=$NETMASK\n"
   printf "GW=$DEFAULT_GW\n"
   printf "\n"
   printf "/sbin/ip a add \$VIPADDR/\$VIPMASK dev \$VIPDEV label \$VIPDEV:ucarp\n"
   printf "/sbin/ip r chg \$VIPNET/\$VIPMASK dev \$VIPDEV src \$VIPADDR\n"
   printf "/sbin/ip r chg default via \$GW src \$VIPADDR\n"
   printf "/usr/sbin/kamcmd cfg.seti redundancy backupactive 1\n"
   printf "echo 'redundancy.backupactive = 1' | tee /rw2/etc/kamailio/redundancy_state.cfg\n"
   printf "start-stop-daemon --start --pidfile /var/run/ucarp-arping.\$VIPADDR --make-pidfile --background --exec /usr/bin/arping -- -q -U \$VIPADDR\n"
   printf "if [ -f /etc/network/if-up.d/hosts ]; then\n"
   printf "export IFACE=eth\n"
   printf "/etc/network/if-up.d/hosts\n"
   printf "unset IFACE\n"
   printf "fi\n"

   printf "\n"
   printf "\E[33mSubstitute /etc/ucarp/ucarp-down.sh file of both PRIMARY and SECONDARY servers with the following content\E[37m\n"
   printf "#!/bin/sh\n"
   printf "exec 2> /dev/null\n"
   printf "\n"
   printf "VIPDEV=\$1\n"
   printf "VIPADDR=$VIRTUAL_IP\n"
   printf "VIPNET=$NETWORK\n"
   printf "VIPMASK=$NETMASK\n"
   printf "GW=$DEFAULT_GW\n"
   printf "\n"
   printf "/sbin/ip a del \$VIPADDR/\$VIPMASK dev \$VIPDEV\n"
   printf "/sbin/ip r add \$VIPNET/\$VIPMASK dev \$VIPDEV\n"
   printf "/sbin/ip r add 0.0.0.0/0 via \$GW\n"
   printf "\n"
   printf "/usr/sbin/kamcmd cfg.seti redundancy backupactive 0\n"
   printf "echo 'redundancy.backupactive = 0' | tee /rw2/etc/kamailio/redundancy_state.cfg\n"
   printf "start-stop-daemon --stop --pidfile /var/run/ucarp-arping.\$VIPADDR --exec /usr/bin/arping\n"
   printf "rm -f /var/run/ucarp-arping.\$VIPADDR\n"
   printf "if [ -f /etc/network/if-post-down.d/hosts ]; then\n"
   printf "export IFACE=eth\n"
   printf "/etc/network/if-post-down.d/hosts\n"
   printf "unset IFACE\n"
   printf "fi\n"

   ########## /usr/sbin/check_ucarp #########
   printf "\n"
        printf "\E[33mCreate /usr/sbin/check_ucarp file of both PRIMARY and SECONDARY servers with the following content\E[37m\n"
        printf "#!/bin/bash\n"
        printf "#set -x\n"
        printf "\n"
        printf "# set env\n"
        printf "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin\n"
        printf "\n"
        printf "# define variables\n"
        printf "ucarpInterfaceCount=\`ifconfig | grep ucarp -c\`\n"
        printf "ucarpProcessCount=\`ps aux|grep -v grep |grep ucarp -c\`\n"
        printf "defaultRouteIp=\`ip route | grep \"default via\" | awk -F \"src \" '{print \$2}' |sed 's/ .*\$//g'\`\n"
        printf "if [ -f /etc/ucarp/ucarp-up.sh ];then\n"
        printf "   ucarpVpnIp=\`cat /etc/ucarp/ucarp-up.sh |grep \"VIPADDR=\" | tr -d \"VIPADDR=\"\`\n"
        printf "else\n"
        printf "   ucarpVpnIp=\"\"\n"
        printf "fi\n"
        printf "\n"
        printf "# main\n"
        printf "if [ \$ucarpProcessCount -gt 0 ] && [ \$ucarpInterfaceCount -gt 0 ]; then\n"
        printf "   if [ \"\$ucarpVpnIp\" != \"\$defaultRouteIp\" ]; then\n"
        printf "      logger -t ucarp \"Default route mismatch\" -p warning\n"
        printf "   fi\n"
        printf "fi\n"
   printf "\n"
        printf "\E[33mAdd permission for sript on both PRIMARY and SECONDARY servers with the following command: \E[37mchmod a+x /usr/sbin/check_ucarp\n"
   printf "\n"
        printf "\E[33mAdd sript in crontab on both PRIMARY and SECONDARY servers with the following command: \E[37mcrontab -l 2>/dev/null | { cat; echo \"*/5 * * * * /usr/sbin/check_ucarp\"; } | crontab -\n"
   printf "\n"
}

######### MAIN ###########

# Get ip addresses
while true; do
   get_ip_addresses
   if [ "x$PRIMARY_IP" == "x$SECONDARY_IP" ]; then
      clear
      printf "\E[31m PRIMARY and SECONDARY addresses are equal\E[37m\n"
      continue
   fi

   if [ "x$PRIMARY_IP" == "x$VIRTUAL_IP" ]; then
      clear
      printf "\E[31m PRIMARY and VIRTUAL addresses are equal\E[37m\n"
      continue
   fi

   if [ "x$SECONDARY_IP" == "x$VIRTUAL_IP" ]; then
      clear
      printf "\E[31m PRIMARY and VIRTUAL addresses are equal\E[37m\n"
      continue
   fi

   break
done

# Configure failover
configure_failover



- connect to Master PBX via ssh
- select menu 11 (Shell)
- launch su followed by the password wildix
- launch mount -o remount rw /
- launch chmod 777 <name_of_txt_file>. For example : chmod 777 failover_enable
- launch ./failover_enable

After the last command you will be asked to :

- Enter PRIMARY node IP address (IP address of WAN interface of primary PBX). For example : 192.168.1.10
- Enter SECONDARY node IP address (IP address of WAN interface of secondary PBX). For example : 192.168.1.20
- Enter VIRTUAL node IP address (IP address that will be shared by both primary and secondary PBXs). For example : 192.168.1.30
- Enter Default GW IP address. For example : 192.168.1.254
- Enter network address. For example : 192.168.1.0
- Enter network mask. For example : 24

After the last step you will see a new output on the screen with the instructions about the configuration that must be done on primary and secondary PBXs (see the picture below)

failover.png
failover.png (173.24 KiB) Viewed 6180 times


Notes :
- To create or edit a file use the command nano-tiny. For example : nano-tiny /etc/sysctl.conf. Press CTRL-X -> Yes to save the file
- The phones and mediagateways should be connected to the WAN interface