#!/bin/sh

# Verify that replication is working on a MySQL slave.

cnf=/usr/local/cams/conf/replicatechecker.cnf

recipient=touchstone-support@mit.edu
body=/tmp/slavemail$$
mysql=/usr/local/mysql/bin/mysql
status=/tmp/slavestatus$$

# Get the slave status.
rm -f $status
$mysql --defaults-extra-file=$cnf -Bse "show slave status\G" \
  > $status 2>&1

# Parse the output.
eval `awk '/Slave_IO_Running/ { print "Slave_IO_Running=" $2 ; } \
           /Slave_SQL_Running/ { print "Slave_SQL_Running=" $2; }' $status`

# Check for the expected status.
if [ "$Slave_IO_Running" != "Yes" -o "$Slave_SQL_Running" != "Yes" ]; then
  rm -f $body
  echo "There is a problem with MySQL replication on `hostname`:" >> $body
  echo "" >> $body
  if [ -z "$Slave_IO_Running" ]; then
    echo "Unexpected output from status query:" >> $body
    cat $status >> $body
  else 
    egrep "Slave_IO_Running|Slave_SQL_Running|Last_Err" $status >> $body
  fi
  mail -s "MySQL replication problem on `hostname`" "$recipient" < $body
  rm -f $body
fi

rm -f $status
