#!/bin/bash
dir=`dirname $0`
prg=`basename $0`
tmp=$dir/$prg.$$.tmp
function log
{
echo "`date +'%m/%d %H:%M:%S'` $1"
}
trap 'rm -f ${tmp}* ; exit' 0 1 2 3 9 15 19 23 24
DB_HOST="127.0.0.1"
DB_NAME=ui
delay=60
NOTIFICATION_MULTIPLE=12
STOP_WHEN_SECS=30
QUIET="N"
# Override default parameters
. $dir/admin.cnf
USAGE="$prg [-h <host>] [-d <db>] [-i <delay>] [-n <notification multiple>] [-s <stop when secs>]"
# Parse the command line arguments:
while getopts h:d:i:n:s:q c ; do
case $c in
h) DB_HOST="${OPTARG}" ;;
d) DB_NAME="${OPTARG}" ;;
i) delay="${OPTARG}" ;;
n) NOTIFICATION_MULTIPLE="${OPTARG}" ;;
s) STOP_WHEN_SECS="${OPTARG}" ;;
q) QUIET="Y" ;;
*) echo "$USAGE" ; exit 2 ;;
esac
done
MYSQL="mysql -D $DB_NAME -h $DB_HOST"
prev="999"
passed=0
count=0
while [ $prev -gt $STOP_WHEN_SECS ] ; do
#echo "S: prev=$prev curr=$curr start=$start passed=$passed STOP_WHEN_SECS=$STOP_WHEN_SECS"
count=$((count+1))
SBM=$($MYSQL -e 'show slave status\G' | grep Seconds_Behind_Master | awk '{print $2}')
if [ "$SBM" == "NULL" ] ; then
$MYSQL -e 'show slave status\G' | mail -s "$prg: Slave is broke. Exitting on $DB_HOST" $RECIPIENTS
exit
fi
curr=$($MYSQL -NB -e 'select unix_timestamp(now())-unix_timestamp(ts) from heartbeat')
if [ $prev == 999 ] ; then
prev=$curr
start=$curr
passed=$delay
echo " s_behind d_behind c_sec_s eta_d | O_c_sec_s O_eta_d O_eta_h ETA"
sleep $delay
#echo "M: prev=$prev curr=$curr start=$start passed=$passed STOP_WHEN_SECS=$STOP_WHEN_SECS"
continue
fi
if [[ $((count%10)) == 0 ]] ; then
echo " s_behind d_behind c_sec_s eta_d | O_c_sec_s O_eta_d O_eta_h ETA"
fi
passed=$((passed+delay))
echo "$prev $curr $start $passed $delay" | awk '
{
prev=$1; curr=$2; start=$3; passed=$4; delay=$5;
speed = (delay-(curr-prev))/delay;
o_speed = (start-(curr-passed))/passed
if (speed == 0) speed_d = 1;
else speed_d = speed;
eta = curr/speed_d;
if (eta<0) eta = -86400;
o_eta = curr/o_speed;
printf("%8d %8.6f %9.3f %7.3f | %9.3f %7.3f %7.2f %7s\n",
curr, curr/86400, speed, eta/86400, o_speed, o_eta/86400, o_eta/3600,
strftime("%H:%M",systime()+o_eta))
}' | while read s_behind d_behind c_sec_s eta_d pipe O_c_sec_s O_eta_d O_eta_h eta ; do
printf "%11s%8d %8.6f %9.3f %7.3f | %9.3f %7.3f %7.2f %7s\n" "`date +'%m/%d %H:%M'`" $s_behind $d_behind $c_sec_s $eta_d $O_c_sec_s $O_eta_d $O_eta_h $eta
if [ $curr -lt $STOP_WHEN_SECS ] ; then
if [ "$QUIET" == "N" ] ; then
$MYSQL -e 'show slave status\G' | mail -s "$prg: slave is caught up! on $DB_HOST" $RECIPIENTS
fi
exit
fi
if [ "$QUIET" == "N" ] ; then
if [[ $((count%NOTIFICATION_MULTIPLE)) == 0 ]] ; then
echo "Estimated time to catchup: ${O_eta_h}h, ETA: $eta" | mail -s "$prg: slave is ${curr}s behind on $DB_HOST" $RECIPIENTS
fi
fi
done
prev=$curr
sleep $delay
#echo "E: prev=$prev curr=$curr start=$start passed=$passed STOP_WHEN_SECS=$STOP_WHEN_SECS"
done