Skip to content

Instantly share code, notes, and snippets.

@AlekseyKorzun
Last active August 29, 2015 13:57
Show Gist options
  • Save AlekseyKorzun/9901982 to your computer and use it in GitHub Desktop.
Save AlekseyKorzun/9901982 to your computer and use it in GitHub Desktop.
MySQL replication monitoring script
#!/bin/bash
#
# Author: Aleksey Korzun
email='[email protected]'
default_max=60
echo "=========================================================================="
echo " Replicator Man v1.0.2 "
echo "=========================================================================="
help() {
echo "Usage: -h 127.0.0.1 [-a alias] [-m 120]" 1>&2;
exit 1;
}
while getopts ":h:m:a:" o; do
case "${o}" in
h)
h=${OPTARG}
;;
a)
a=${OPTARG}
;;
m)
m=${OPTARG}
;;
*)
help
;;
esac
done
shift $((OPTIND-1))
if [ -z "${h}" ]; then
help
fi
if [ -z "${a}" ]; then
a=$h
fi
if [ -z "${m}" ]; then
m=$default_max
fi
echo
response=`mysql -h $h -e 'SHOW SLAVE STATUS\G' | grep 'Seconds_Behind_Master' | awk -F': ' {' print $2 '}`
if [ "$response" == 'NULL' ] || ! [[ $response =~ ^-?[0-9]+$ ]] || [ "$response" -gt "$m" ]; then
if [ ! -f "/tmp/replicatorman_$h" ]; then
echo "Response: $response" | mail -s "Replication failure on $a" $email
touch "/tmp/replicatorman_$h"
fi
echo 'FAILURE'
exit 1
elif [ -f "/tmp/replicatorman_$h" ]; then
echo "Response: $response" | mail -s "Replication recovery on $a" $email
rm "/tmp/replicatorman_$h"
fi
echo 'SUCCESS'
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment