lvs的健康检测脚本
写得不怎么样,基本实现吧,因为基本不会用到,有时间再改进了,嘻嘻
1 #!/bin/bash
2
3 rs=(‘192.168.61.130‘ ‘192.168.61.132‘)
4 vip="192.168.61.100"
5 dip="192.168.61.131"
6 checkcount=1
7 checkloop=4
8 i=1
9
10
11 while [ $i -lt 2 ];do
12 #sorry server检测,Director httpd默认启动
13 if [ $(ipvsadm -Ln |grep "\->" |wc-l) -lt 2 ];then
14 ipvsadm -a -t $vip":80" -r $dip -g
15 else
16 ipvsadm -d -t $vip":80" -r $dip -g
17 fi
18
19 #循环所有RS检测
20 for rs_x in ${rs[@]};do
21 #只要在线并且之前已经下线的,就上线处理
22 if curl --connect-timeout 2 -s "http://"$rs_x"/index.html" &>/dev/null;then
23 ipvsadm -Ln |grep $rs_x &>/dev/null
24 if [ $? -ne 0 ];then
25 ipvsadm -a -t $vip":80" -r $rs_x &>/dev/null
26 #echo "add rs1 server ok"
27 fi
28 else
29 #当RS是刚离线的,每隔1秒检测一次,检测3次还不在线就下线,如果是之前下线的则不做处理
30 ipvsadm -Ln |grep $rs_x &>/dev/null
31 if [ $? -eq 0 ];then
32 while [ $checkcount -le $checkloop ];do
33 if curl --connect-timeout 1 -s "http://"$rs_x"index.html" &>/dev/null;then
34 break
35 else
36 let checkcount++
37 fi
38 if [ $checkcount -eq 3 ];then
39 if ipvsadm -Ln |grep $rs_x &>/dev/null;then
40 ipvsadm -d -t $vip":80" -r $rs_x &>/dev/null
41 #echo "del rs1 server ok"
42 break
43 fi
44 fi
45 sleep 1
46 done
47 checkcount=1
48 fi
49
50 fi
51 done
52
53 sleep 3
54 done
原文地址:http://dengxi.blog.51cto.com/4804263/1704052