Script to Monitor Mysql Replication

Script to Monitor Mysql Replication  

#!/bin/bash
# Checks MySQL Replication status.  Sends user(s) a notification of status
status=0
MasterHost="192.168.1.10"
SlaveHost="192.168.1.11"
emails="webmaster@techsuccor.com" #multiple emails space separated
DownSubject="Replication Status Down on $HOSTNAME"
GoodSubject="Replication Status Good on $HOSTNAME"
GoodMessage="Everything regarding MySQL replication on $HOSTNAME is good.nHave a great day!nnn"

#Grab the lines for each and use Gawk to get the last part of the string(Yes/No)
SQLresponse=`mysql -uroot -ppassword  -e "show slave status G" |grep -i "Slave_SQL_Running"|gawk '{print $2}'`
IOresponse=`mysql -uroot -ppassword  -e "show slave status G" |grep -i "Slave_IO_Running"|gawk '{print $2}'`


if [ "$SQLresponse" = "No" ]; then
       error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_SQL_Running: Non"
      status=1
fi

if [ "$IOresponse" = "No" ]; then
      error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_IO_Running: Non"
      status=1
fi

# If the replication is not working
if [ $status = 1 ]; then
      for address in $emails; do
                echo -e $error | mail -s "$DownSubject" $address
                echo "Replication down, sent email to $address"
      done
fi

# If the replication is working fine
if [ $status = 0 ]; then
      for address in $emails; do
                echo -e $GoodMessage | mail -s "$GoodSubject" $address
                echo "Replication is up, still sent email to $address"
      done
fi

 

 

 

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *