#vim /etc/keepalived/keepalived.conf

global_defs {
   notification_email {
        [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_script monitor {
script "/etc/keepalived/checkversion.sh &> /dev/null"
interval 30
weight -5
}
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 211
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass skymobi
    }
    virtual_ipaddress {
        172.18.1.224
    }
    track_script {
       monitor
    }
}

#vim /etc/keepalived/checkversion.sh

#!/bin/bash
logfile="/tmp/check-version.log"
ret=0
ret_sendmsg_file_1="/tmp/sendmsg_1.ret"
ret_sendmsg_file_2="/tmp/sendmsg_2.ret"
time=`date "+%F %T"`
function sendmsg()
{
        local sendmsg
        #phones="15800329316,15121132075,13817488875"
        phones="15800329316"
        CURL="/usr/bin/curl"
        URL="http://192.168.6.13/fetion.php?phone=PHONE&msg=MSG"
        ##拼装告警信息
        sendmsg=$1
        for phone in `echo $phones|sed 's/,/ /g'`
        do
                MSGURL=`echo $URL|sed "s/PHONE/$phone/g;s/MSG/$sendmsg/g"`
                $CURL "$MSGURL" >> $logfile
        done
}
function check_version()
{
        local sendmsg
        command='/usr/bin/curl -x172.18.1.233:3417 -d format=json&commonkey=bWNjPTk5OQ==BODY:{id:Y29tLmZhY2Vib29rmthdGFuYQ==} -i http://460.pplaystore.com/api/v21/apks/checkversion'
        $command |grep "HTTP/1.1 200 OK"
        if [ $? -ne 0 ]
        then
                sleep 10
                $command|grep "HTTP/1.1 200 OK"
                if [ $? -ne 0 ]
                then
                        tomcat_ret=`cat $ret_sendmsg_file_1`
                        if [ $tomcat_ret -eq 0 ]
                        then
                                /etc/init.d/tomcat_3417 restart
                        fi
                        sleep 10
                        $command|grep "HTTP/1.1 200 OK"
                        if [ $? -ne 0 ]
                        then
                                ret=1
                                ret_sendmsg=`cat $ret_sendmsg_file_1`
                                sum=`expr $ret_sendmsg + 1`
                                echo $sum > $ret_sendmsg_file_1
                                if [ "x$ret_sendmsg" != "x" ]
                                then
                                        if [ $ret_sendmsg -eq 1 ]
                                        then
                                                sendmsg=`echo $time 'vip:172.18.1.233-->172.18.1.249'|/usr/bin/perl -e 'use URI::Escape;while(){print uri_escape($_);}'`
                                                sendmsg $sendmsg
                                        fi
                                fi
                        fi
                fi
                echo 0 > $ret_sendmsg_file_2
        else
                sleep 15
                ip addr|grep "172.18.1.224"
                if [ $? -ne 0 ]
                then
                        ret_sendmsg=`cat $ret_sendmsg_file_2`
                        sum=`expr $ret_sendmsg + 1`
                        echo $sum > $ret_sendmsg_file_2
                        if [ "x$ret_sendmsg" != "x" ]
                        then
                                if [ $ret_sendmsg -eq 1 ]
                                then       
                                        sendmsg=`echo $time 'vip:172.18.1.233-->172.18.1.249'|/usr/bin/perl -e 'use URI::Escape;while(){print uri_escape($_);}'`
                                        sendmsg $sendmsg
                                fi     
                        fi     
                fi
                echo 0 > $ret_sendmsg_file_1
        fi
}
function monitor()
{
        local sendmsg
        ip addr|grep "172.18.1.224"
        if [ $? -eq 0 ]
        then
                echo 0 > $ret_sendmsg_file_2
        fi
        ping -c 1 -w 1 172.18.1.233
        if [ $? -ne 0 ]
        then
                sendmsg=`echo $time 'ping 172.18.1.233 100% unreacheable.'|/usr/bin/perl -e 'use URI::Escape;while(){print uri_escape($_);}'`
                sendmsg $sendmsg
        else
                date >> $logfile
                check_version >> $logfile
                exit $ret
        fi
}
monitor