Browse Source

Replication verification CRON script

root 8 years ago
parent
commit
db7b4104bd
2 changed files with 89 additions and 0 deletions
  1. 77 0
      mysql_replication_cron.sh
  2. 12 0
      params.inc.dist

+ 77 - 0
mysql_replication_cron.sh

@@ -0,0 +1,77 @@
+#!/bin/bash
+
+#
+# CopyLeft Mathieu Moulin <lemathou@free.fr>
+# GNU GPL v2
+# 
+# Mysql Replication Verification Script
+# Usage : send an email when the replication process is stopped or too slow
+# to prevent desynchronization problems
+#
+
+#
+# You need to create a Mysql User replication_cron
+# with "replication client" global administration privilege
+#
+
+PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
+
+. ./params.inc
+
+if [ -f $tmp_replication_slave_reason_filepath ]; then
+        rm -rf $tmp_replication_slave_reason_filepath
+fi
+
+echo "show slave status\G;" |mysql $cmd_slave_username $cmd_slave_hostname $cmd_slave_userpass > $tmp_replication_slave_log_filepath
+
+grep "Access denied" $tmp_replication_slave_log_filepath
+if [ "$?" -ne "1" ]; then
+        msg="Access denied to MySQL slave user"
+	echo $msg
+        echo $msg >> $tmp_replication_slave_reason_filepath
+fi
+
+if [ ! -s $tmp_replication_slave_log_filepath ]; then
+        msg="Slave status empty => Slave not running ?"
+	echo $msg
+        echo $msg >> $tmp_replication_slave_reason_filepath
+fi
+
+grep "Slave_IO_Running: No" $tmp_replication_slave_log_filepath
+if [ "$?" -ne "1" ]; then
+        msg="Slave IO not Running"
+	echo $msg
+        echo $msg >> $tmp_replication_slave_reason_filepath
+fi
+
+grep "Slave_SQL_Running: No" $tmp_replication_slave_log_filepath
+if [ "$?" -ne "1" ]; then
+        msg="Slave SQL not Running"
+        echo $msg
+        echo $msg >> $tmp_replication_slave_reason_filepath
+fi
+
+regex="Seconds_Behind_Master: \(\d+\)"
+seconds=`grep "Seconds_Behind_Master" $tmp_replication_slave_log_filepath | tr -dc '0-9'`
+if [ $seconds="" ]; then
+	seconds=0
+fi
+if (( $seconds > $slave_verif_seconds_min )); then
+	seconds_orig=$seconds
+	minutes=$(( $seconds/60 ))
+	hours=$(( $minutes/60 ))
+	days=$(( $hours/24 ))
+	hours=$(( $hours - 24*$days ))
+	minutes=$(( $minutes - 60*(24*$days + $hours) ))
+	seconds=$(( $seconds - 60*(60*(24*$days + $hours) + $minutes) ))
+        msg="Seconds behind master : $seconds_orig => $days days, $hours hours, $minutes minutes, $seconds seconds"
+        echo $msg
+        echo $msg >> $tmp_replication_slave_reason_filepath
+fi
+
+
+if [ -f $tmp_replication_slave_reason_filepath ]; then
+        mail -r $slave_verif_email_from -s "$slave_verif_email_subject" $slave_verif_email_to < $tmp_replication_slave_reason_filepath
+        echo $tmp_replication_slave_reason_filepath
+fi
+

+ 12 - 0
params.inc.dist

@@ -15,3 +15,15 @@ master_replication_password=""
 # Slave server
 slave_server=""
 
+# Replication verif
+slave_hostname=`hostname`
+cmd_slave_hostname="-h localhost"
+cmd_slave_username="-ureplication_cron"
+cmd_slave_userpass="-ppassword"
+slave_verif_email_from="mysql@$server_hostname"
+slave_verif_email_to="root@$server_hostname"
+slave_verif_email_subject="Replication is Down on $server_hostname"
+tmp_replication_slave_log_filepath="/tmp/replication_slave_log.txt"
+tmp_replication_slave_reason_filepath="/tmp/replication_slave_reason.txt"
+slave_verif_seconds_min=3600
+