#! /Bin/bash
# Create lvs dr or tunl realservr scripts.
# Chenjian 2011-09-23
VIP_LIST = "192.168.116.20 192.168.116.21"
USAGE = "Usage: $0 {start | stop | status} {dr | tunl }"
NAME = tunl
NUM = 0
If [-z $1]
Then
Echo $ USAGE
Exit 2
Fi
ACTION = 'echo $1 | tr A-Z a-Z'
If! [$1 = status]
Then
If [-z $2]
Then
Echo $ USAGE
Exit 2
Fi
Fi
TYPE = 'echo $2 | tr A-Z a-Z'
Case "$1" in
Start)
If [$ TYPE = dr]
Then
If/sbin/ifconfig | grep lo:>/dev/null
Then
/Sbin/ifdown lo
/Sbin/ifup lo
Fi
For vip in 'echo $ VIP_LIST '; do
/Sbin/ifconfig lo: $ NUM $ vip netmask 255.255.255.255 broadcast $ vip
/Sbin/route add-host $ vip dev lo: $ NUM
NUM = 'echo $ [$ NUM + 1]'
Done
Echo "1">/proc/sys/net/ipv4/conf/lo/arp_ignore
Echo "2">/proc/sys/net/ipv4/conf/lo/arp_announce
Echo "1">/proc/sys/net/ipv4/conf/all/arp_ignore
Echo "2">/proc/sys/net/ipv4/conf/all/arp_announce
Sysctl-p>/dev/null 2> & 1
Echo "$ TYPE RealServer Start [OK]"
Elif [$ TYPE = tunl]
Then
For vip in 'echo $ VIP_LIST '; do
If [$ NAME = tunl]
Then
NAME = tunl0
/Sbin/ifconfig "$ NAME" $ vip netmask 255.255.255.255
/Sbin/route add-host $ vip dev $ NAME
Else
/Sbin/ifconfig "$ NAME": "$ NUM" $ vip netmask 255.255.255.255
/Sbin/route add-host $ vip dev $ NAME
Fi
NUM = 'echo $ [$ NUM + 1]'
Done
Echo 1>/proc/sys/net/ipv4/conf/tunl0/arp_ignore
Echo 2>/proc/sys/net/ipv4/conf/tunl0/arp_announce
Echo 0>/proc/sys/net/ipv4/conf/tunl0/arp_filter
Echo 1>/proc/sys/net/ipv4/conf/tunl0/forwarding
Echo 1>/proc/sys/net/ipv4/conf/all/arp_ignore
Echo 2>/proc/sys/net/ipv4/conf/all/arp_announce
Sysctl-p>/dev/null 2> & 1
Echo "$ TYPE RealServer Start [OK]"
Else
Echo $ USAGE
Exit 2
Fi
;;
Stop)
If [$ TYPE = dr]
Then
For vip in 'echo $ VIP_LIST '; do
/Sbin/ifconfig lo: $ NUM down
/Sbin/route del $ vip
NUM = 'echo $ [$ NUM + 1]'
Done
Echo "0">/proc/sys/net/ipv4/conf/lo/arp_ignore
Echo "0">/proc/sys/net/ipv4/conf/lo/arp_announce
Echo "0">/proc/sys/net/ipv4/conf/all/arp_ignore
Echo "0">/proc/sys/net/ipv4/conf/all/arp_announce
Sysctl-p>/dev/null 2> & 1
Echo "$ TYPE RealServer Stop [OK]"
Elif [$ TYPE = tunl]
Then
/Sbin/ifconfig tunl0 down
Echo 0>/proc/sys/net/ipv4/conf/tunl0/arp_ignore
Echo 0>/proc/sys/net/ipv4/conf/tunl0/arp_announce
Echo 1>/proc/sys/net/ipv4/conf/tunl0/arp_filter
Echo 0>/proc/sys/net/ipv4/conf/all/arp_ignore
Echo 0>/proc/sys/net/ipv4/conf/all/arp_announce
Else
Echo $ USAGE
Exit 2
Fi
;;
Status)
If/sbin/ifconfig | grep-q 'lo :'
Then
TYPE = dr
Elif/sbin/ifconfig | grep-q 'tunl'
Then
TYPE = tunl
Else
Exit 2
Fi
For vip in 'echo $ VIP_LIST '; do
If [! '/Sbin/ifconfig | grep-q $ vip'-o! 'Netstat-rn | grep $ vip '];
Then
Echo "LVS-$ TYPE real server stopped, vip = $ vip"
Else
Echo "LVS-$ TYPE Running, vip = $ vip"
Fi
Done
;;
*)
Echo "$ USAGE"
Exit 1
;;
Esac
Author "If one day"