Use shell scripts to detect realserver status in LVS and implement automatic removal of attached hosts

Source: Internet
Author: User

#!/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/1688588

Use shell scripts to detect realserver status in LVS and implement automatic removal of attached hosts

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.