Skip to content

Instantly share code, notes, and snippets.

@chmac
Created February 12, 2014 12:48
Show Gist options
  • Save chmac/8955000 to your computer and use it in GitHub Desktop.
Save chmac/8955000 to your computer and use it in GitHub Desktop.
Script called from monit to check replication delay
#!/bin/bash
# Specify the threshold over which an error should be flagged
# Defaults to 60, but can be passed like ./script threshold
if [ -z $1 ]
then
threshold=60
else
threshold="$1"
if [[ $threshold != +([0-9]) ]]
then
echo "The supplied threshold was not an integer. Ooops..." 1>&2
exit 1
fi
fi
# Specify the path to the file where pt-heartbeat writes the data
#filepath="pt-heartbeat"
filepath="/var/spool/pt-heartbeat"
if [ ! -f $filepath ]
then
echo "Could not find the pt-heartbeat file. Eeeek..." 1>&2
exit 1
fi
# Grep out the initial digits from the beginning of the line
# Format is ^0.00s [ 0.05s, 0.07s, 0.15s ]$
delay=$(cat $filepath | grep -oE '^[0-9]*')
# Check that $delay is an integer
if [[ $delay != +([0-9]) ]]
then
# If it fails the first time, wait and try reading it again
sleep 2.7
delay=$(cat $filepath | grep -oE '^[0-9]*')
# Check that $delay is an integer
if [[ $delay != +([0-9]) ]]
then
# If it fails the second time, wait and try reading it again
sleep 13.7
delay=$(cat $filepath | grep -oE '^[0-9]*')
# Check that $delay is an integer
if [[ $delay != +([0-9]) ]]
then
echo "Could not extract a number from the pt-heartbeat file. Baaaad..." 1>&2
exit 1
fi
fi
fi
# Check if the $delay is below the threshold
if [ "$delay" -lt "$threshold" ]
then
exit 0
else
echo "This slave is ${delay} seconds behind master, over threshold (${threshold})." 1>&2
fi
# By default, exit with an error code
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment