#!/bin/sh
# Describe:check the real server status
# history:2015-08-25 22:21:38
# Author:tim
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
Export PATH
vip=192.168.1.121
dip=192.168.1.112
rip= ("192.168.1.122" "192.168.1.123")
Vport=80
status= (0 0)
Type=g
Schema=rr
Weight= (1 3)
Let count=0
Initstats () {
Local I
For I in ' seq 0 1 ';d o
If Ipvsadm-l-n |grep ${rip[$I]} &>/dev/null; then
status[$I]=1
Else
status[$I]=0
Fi
Done
}
#初始化realserver状态
Add () {
Ipvsadm-a-T $VIP: $CPORT-R ${rip[$1]}-$TYPE-W ${weight[$1]}
[$?-eq 0] && return 0 | | Return 1
}
#添加realserver
Del () {
Ipvsadm-d-T $VIP: $CPORT-R ${rip[$1]}
[$?-eq 0] && return 0 | | Return 1
}
#删除realserver
Check () {
If Curl--connect-timeout 1 http://$1 &>/dev/null;then
return 0
Else
Return 1
Fi
}
#检测realserver状态
Initstats
While:;d o
For N in ' seq 0 1 ';d o
If check ${rip[$N]}; then
If [${status[$N]}-eq 0]; then
Add $N &&status[$N]=1 && echo "' Date +%f-%t ', ${rip[$N]} is back" >>/var/log/ipvs
Fi
Else
If [${status[$N]}-eq 1];then
Del $N &&status[$N]=0 &&echo "' Date +%f-%t ', ${rip[$N]} is Gone" >>/var/log/ipvs
Fi
Fi
Done
If [${status[0]}-eq 0-a ${status[1]}-eq 0]; then
Ipvsadm-a-T $VIP: $CPORT-R $DIP-$TYPE
Else
Ipvsadm-d-T $VIP: $CPORT-R $DIP &>/dev/null
Fi
Sleep 5
Done
Note: This script is written when learning Marco Linux video tutorials.
This article is from the "Technical Life" blog, please be sure to keep this source http://hgmz123.blog.51cto.com/9413337/1688590
Use shell scripts to detect realserver status in LVS and implement automatic removal of attached hosts