Last active
December 4, 2020 05:11
-
-
Save mansurali901/d95de8de0952a22f8085be536dc07521 to your computer and use it in GitHub Desktop.
MySql Replication Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Script is setup for Mysql Replication Monitoring | |
# Author Mansur UlHasan | |
# Mail [email protected] | |
# Mail Function | |
# Variables | |
HOME="/usr/local/cron" | |
Err1=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Last_Errno:` | |
Err2=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Seconds_Behind_Master:` | |
Err3=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Slave_IO_Running:` | |
Err4=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Last_SQL_Errno:` | |
mail_func () { | |
echo " | |
++++++++++++++++++++++++++++++++++++++++++ | |
Error Found in MySql Replication | |
++++++++++++++++++++++++++++++++++++++++++ | |
Hi Admins, | |
There is an issue found during replication due to this | |
replication is not working | |
Reference : | |
echo $Err1 | |
echo $Err2 | |
echo $Err3 | |
echo $Err4 | |
" > $HOME/mailtemplate | |
for mail in `cat $HOME/maillist` | |
do | |
mail -r [email protected] -s "[Replication Alert ] : Replication Issue Found on `hostname` " $mail < $HOME/mailtemplate | |
done | |
} | |
# Replication testing function | |
RepError=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Last_Errno: |awk '{print $2}'` | |
MasterBehind=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Seconds_Behind_Master: |awk '{print $2}'` | |
Slave_IO_Running=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Slave_IO_Running: |awk '{print $2}'` | |
Last_SQL_Errno=`mysql -pv1t@m1nC -e 'show slave status \G;' |grep Last_SQL_Errno: |awk '{print $2}'` | |
# Validation Statements | |
case $RepError in | |
0) | |
echo "Replication running normally" | |
;; | |
*) | |
mail_func | |
;; | |
esac | |
case $MasterBehind in | |
0) | |
echo "Replication running normally" | |
;; | |
*) | |
mail_func | |
;; | |
esac | |
case $Slave_IO_Running in | |
Yes) | |
echo "Replication running normally" | |
;; | |
*) | |
mail_func | |
;; | |
esac | |
case $Last_SQL_Errno in | |
0) | |
echo "Replication running normally" | |
;; | |
*) | |
mail_func | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment