Environment: centos7.1
Brief introduction:
When the script detects that an RS HTTP service is dropped, RS is automatically removed from the LVS, and when all RS HTTP services are dropped, all RS is removed in the LVS and the HTTP service on the LVS Scheduler is added to the LVS as an alarm page.
When the script detects that an RS HTTP service is active, it is automatically added to the LVS and deleted if there is a LVS in the scheduler itself.
The script is tested by me and fully meets the requirements.
Here is the script:
#!/bin/bash#rs= ("152.168.1.12" "152.168.1.13") vip= "152.168.1.10" port=80logfile= "/usr/local/scripts/ Lvs.log "function check_alldown { #有一个rs主机能访问, that means it's not all gone # Check to an RS host to survive and exit the check #如果全部rs不能访问, indicating that the host has lost 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主机掉线 and add it #如果可以访问一个rs主机 without 127.0.0.1 in LVs, And the LVS have 127.0.0.1 to delete it 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主机, and the LVS does not add it #如果不能访问一个rs主机, and it removes it in 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
Complete.
This article is from the "Super_color" blog, make sure to keep this source http://small.blog.51cto.com/259970/1728082
LVS Health Check