環境:centos7.1centos
簡介:bash
當腳本檢測到某個RS的http服務掉線時,在LVS中自動移除RS;當全部RS的http服務掉線時,在LVS中移除全部RS,並將LVS調度器上的http服務加入到LVS中,做爲告警頁面。curl
當腳本檢測到某個RS的http服務活躍時,自動將其加入到LVS中,若是有調度器自己的存在LVS中,將其刪除。ide
該腳本經本人測試,徹底達到要求。測試
如下爲腳本:url
#!/bin/bash # rs=("152.168.1.12" "152.168.1.13") vip="152.168.1.10" port=80 logfile="/usr/local/scripts/lvs.log" function check_alldown { #有一個rs主機能訪問,就說明不是所有掉了 #檢查到一個rs主機存活就退出檢查 #若是所有rs不能訪問,說明主機全掉了 for www in `echo ${rs[*]}` do curl --connect-timeout 1 http://$www &> /dev/null if [ $? -eq 0 ] then echo 0 exit 0 fi done echo 100 } function lvs_add { ipvsadm -a -t $vip:$port -r $1 echo "add rs host:$1 to lvs" } function lvs_rm { ipvsadm -d -t $vip:$port -r $1 echo "remove rs host:$1 to lvs" } function lvs_local { #若是所有rs主機掉線,而且lvs中沒有127.0.0.1就添加它 #若是能夠訪問一個rs主機,而且lvs中有127.0.0.1就刪除它 all_down=`check_alldown` rip=$(ipvsadm -L -n | gawk '/127.0.0.1/') if [ $all_down -eq 100 ] then if [ "$rip" = "" ] then echo "`date +%F:%H-%M-%S` all rs host is down!" >> $logfile lvs_add "127.0.0.1" fi else if [ $all_down -eq 0 ] && [ ! "$rip" = "" ] then echo "`date +%F:%H-%M-%S` one rs host is up,remove local rs host!" >> $logfile lvs_rm "127.0.0.1" fi fi } function lvs_rs { #若是能夠訪問一個rs主機,而且lvs中沒有它就添加它 #若是不能訪問一個rs主機,而且lvs中有它就刪除它 lvs_local for www in `echo ${rs[*]}` do rip=$(ipvsadm -L -n | gawk "/$www/") curl --connect-timeout 1 http://$www &> /dev/null if [ $? -eq 0 ] then if [ "$rip" = "" ] then echo "`date +%F:%H-%M-%S` rs host:$www is up!" >> $logfile lvs_add "$www" fi else if [ ! "$rip" = "" ] then echo "`date +%F:%H-%M-%S` rs host:$www is down!" >> $logfile lvs_rm "$www" fi fi done } function lvs_monitor { while true do echo "check lvs rs health!" lvs_rs sleep 1 done } lvs_monitor
完畢。centos7