#!/bin/sh
# WARNING: This file was auto-generated. Do not edit!
# $Log: myslave.in,v $
# Revision 2.2  2008-06-04 21:43:55+05:30  Cprogrammer
# added Warning for auto-generated
#
# Revision 2.1  2002-11-14 14:55:10+05:30  Cprogrammer
# script to monitor replication
#
#
# $Id: myslave.in,v 2.2 2008-06-04 21:43:55+05:30 Cprogrammer Stab mbhangui $
#
MYSQL=/mysql

usage()
{
more <<EOF
Usage: myslave [OPTION]

Known values for OPTION are:

--user=slave_user --pass=slave_pass --repl_pass=replication_user_pass [--host=slave_host] [--port=slave_mysql_port]
	[--socket=slave_mysql_socket] [--threshold=number]

slave_user       - User have access to show slave status on a slave
slave_pass       - Password for the above user
repl_pass        - Password for the Replication user
slave_host       - Host to connect to (optional, default localhost)
slave_mysql_port - Port to connect to mysql (optional, default 3306)
slave_socket     - Unix Socket to use for connection (optional, default /tmp/mysql.sock)
threshold        - Difference in updates between slave and master above which report error (optional, default 0)

parameters enclosed in [] are optional

--help

  display this help and exit

--version

  output version information
EOF
exit $1
}

slave_status()
{
query="show slave status;"
if [ " $SLAVE_HOST" = " " ] ; then
	host_opt=""
else
	host_opt="-h $SLAVE_HOST"
fi
if [ " $SLAVE_PORT" = " " ] ; then
	port_opt=""
else
	port_opt="-P $SLAVE_PORT"
fi
if [ " $SLAVE_SOCK" = " " ] ; then
	sock_opt=""
else
	sock_opt="-S $SLAVE_SOCK"
fi
$MYSQL -B -s -u $SLAVE_USER -p$SLAVE_PASS --execute="$query" $host_opt $port_opt $sock_opt 2>/tmp/my.$$
}

slave_version()
{
query="show variables;"
if [ " $SLAVE_HOST" = " " ] ; then
	host_opt=""
else
	host_opt="-h $SLAVE_HOST"
fi
if [ " $SLAVE_PORT" = " " ] ; then
	port_opt=""
else
	port_opt="-P $SLAVE_PORT"
fi
if [ " $SLAVE_SOCK" = " " ] ; then
	sock_opt=""
else
	sock_opt="-S $SLAVE_SOCK"
fi
#echo [$SLAVE_HOST] [$host_opt]
#echo "$MYSQL -B -s -u $SLAVE_USER -p$SLAVE_PASS --execute="$query" $host_opt $port_opt $sock_opt 2>/tmp/my.$$"
$MYSQL -B -s -u $SLAVE_USER -p$SLAVE_PASS --execute="$query" $host_opt $port_opt $sock_opt 2>/tmp/my.$$
}

master_status()
{
query="show master status;"
$MYSQL -B -s -u $1 -p$REPL_PASS -h $2 -P $3 --execute="$query" 2>/tmp/my.$$
}

###############################
#
###############################

if test $# -eq 0; then
    usage 1
fi

while test $# -gt 0; do
    case "$1" in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
    esac

    case "$1" in
    --user=*)
	SLAVE_USER=$optarg
	;;
    --pass=*)
	SLAVE_PASS=$optarg
	;;
    --port=*)
	SLAVE_PORT=$optarg
	;;
    --host=*)
	SLAVE_HOST=$optarg
	;;
    --socket=*)
	SLAVE_SOCK=$optarg
	;;
	--repl_pass=*)
	REPL_PASS=$optarg
	;;
	--threshold=*)
	threshold=$optarg
	;;
    --version)
	echo "\$Id: myslave.in,v 2.2 2008-06-04 21:43:55+05:30 Cprogrammer Stab mbhangui $"
	exit 0
	;;
    --help)
	usage 0
	;;
    *)
	echo "invalid option [$1]"
	read key
	usage 1
	;;
    esac
    shift
done
if [ " $SLAVE_USER" = " " -o " $SLAVE_PASS" = " " -o " $REPL_PASS" = " " ] ; then
	usage 1
fi
version=`slave_version | grep -w version | awk '{print $2}' | cut -d. -f1`
if [ $? -ne 0 -o -s /tmp/my.$$ ] ; then
	echo "error getting slave status"
	cat /tmp/my.$$
	rm -f /tmp/my.$$
	exit 1
fi
rm -f /tmp/my.$$

line=`slave_status | awk '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12}'`
if [ $? -ne 0 -o -s /tmp/my.$$ ] ; then
	echo "error getting slave status"
	cat /tmp/my.$$
	rm -f /tmp/my.$$
	exit 1
fi
rm -f /tmp/my.$$
set $line
log_file=$5
log_pos=$6
if [ $version -eq 3 ] ; then
	slave_running=$7
else
	slave_running=`echo $line|awk '{print $10}'`
fi
line=`master_status $2 $1 $3`
if [ $? -ne 0 -o -s /tmp/my.$$ ] ; then
	echo "error getting master status"
	cat /tmp/my.$$
	rm -f /tmp/my.$$
	exit 1
fi
rm -f /tmp/my.$$
if [ ! " $slave_running" = " Yes" ] ; then
	echo "Warning Slave not running"
	exit 1
fi

set $line
if [ ! " $log_file" = " $1" ] ; then
	logger -i -p daemon.notice "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Log Files Different"
	echo "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Log Files Different"
	exit 1
elif [ " $log_pos" -ne " $2" ] ; then
	diff=`expr $2 - $log_pos`
	if [ " $threshold" = " " ] ; then
		logger -i -p daemon.notice "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Difference $diff Threshold 0"
		echo "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Difference $diff Threshold 0"
		exit 1
	else
		if [ $diff -gt $threshold ] ; then
			logger -i -p daemon.notice "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Difference $diff Threshold $threshold"
			echo "Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Difference $diff Threshold $threshold"
			exit 1
		else
			echo "OK Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos, Difference $diff Threshold $threshold"
			exit 0
		fi
	fi
fi
echo "OK Master: Log File $1, Position $2 Slave: Log File $log_file, Position $log_pos"
exit 0
