Skip to content

Instantly share code, notes, and snippets.

@iphoting
Created July 4, 2012 06:47
Show Gist options
  • Save iphoting/3045764 to your computer and use it in GitHub Desktop.
Save iphoting/3045764 to your computer and use it in GitHub Desktop.
MySQL Slave Replication Heartbeat Monitor
#!/bin/bash
## Plug-in to Monitor MySQL Slave Replication Delay.
#
#$Author: imran $
#$Date: 2008-09-01 09:29:21 $
#$RCSfile: mysql_rep_delay,v $
#$Revision: 1.1 $
heartbeatdb=${heartbeatdb:-test}
cmd=${cmd:-`which pt-heartbeat`}
mysqluser=${mysqluser:-root}
if [ "$mysqlpassword" ]
then
passopt="-p$mysqlpassword"
fi
fullcmd="$cmd -D $heartbeatdb --check -h localhost -u $mysqluser $passopt"
case $1 in
config)
cat <<'EOM'
graph_title MySQL Slave Replication Delay
graph_vlabel seconds
mysql_rep_delay.label delay
mysql_rep_delay.warning 60
mysql_rep_delay.critical 3600
graph_category mysql
EOM
exit 0;;
autoconf)
if [ -e "$cmd" -a -x "$cmd" ]
then
$fullcmd > /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
echo "yes"
else
echo "pt-heartbeat check failed"
fi
else
echo "$cmd is invalid"
fi
;;
*)
echo -n "mysql_rep_delay.value "
$fullcmd
;;
esac
host=localhost
pass=password
user=username
database=test
pid=/var/run/pt-heartbeat.pid
#!/bin/bash
#
# Author: Fernando Ipar
#
# chkconfig: 2345 65 35
# description: pt-heartbeat startup script
### BEGIN INIT INFO
# Provides: pt-heartbeat
# Required-Start: $local_fs $network $remote_fs mysql
# Required-Stop: $local_fs $network $remote_fs mysql
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop pt-heartbeat
# Description: pt-heartbeat monitors MySQL replication delay
### END INIT INFO
# source function library
lsb_functions="/lib/lsb/init-functions"
if test -f $lsb_functions ; then
. $lsb_functions
else
log_success_msg()
{
echo " SUCCESS! $@"
}
log_failure_msg()
{
echo " ERROR! $@"
}
fi
# test for pt-heartbeat
[ $(type pt-heartbeat 2>&1| grep -c 'pt-heartbeat is') -gt 0 ] || {
echo "I can't find pt-heartbeat on the path">&2
exit 1
}
CONF=/etc/pt-heartbeat.conf
# test for conf file
[ -r $CONF ] || {
echo "I can't find or read $CONF">&2
exit 1
}
# test for pid option
. $CONF
piderr=0
[ -z "$pid" ] && piderr=1 #if unset, error
[ -f $pid ] || {
touch $pid || piderr=1 #if I can't touch, error
rm -f $pid
}
[ $piderr -eq 1 ] && {
echo "I can't find the pid option in $CONF, or it's value is invalid" >&2
exit 1
}
op="$1"
# functions implementing options
start()
{
echo -n "Starting pt-heartbeat ... "
pt-heartbeat --config $CONF --daemonize --update
[ $? -eq 0 ] && log_success_msg || log_failure_msg
}
stop()
{
[ -f $pid ] && [ -d /proc/$(cat $pid) ] && {
echo -n "Stopping pt-heartbeat ... "
kill $(cat $pid)
log_success_msg
}|| {
echo "pt-heartbeat is not running">&2
}
}
status()
{
[ -f $pid ] && [ -d /proc/$(cat $pid) ] && echo "pt-heartbeat is running" || echo "pt-heartbeat is not running">&2
}
case "$op" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status
;;
*)
echo "Usage: pt-heartbeat start|stop|restart|status">&2
exit 1
esac
exit 0
@tesink
Copy link

tesink commented Apr 16, 2013

Thanks for sharing. I added this line:

@@ -72,6 +72,7 @@
     echo -n "Stopping pt-heartbeat ... "
     kill $(cat $pid)
     log_success_msg 
+    rm $pid
   }|| {
     echo "pt-heartbeat is not running">&2
   }

As I kept getting this error:

Starting pt-heartbeat ... Overwriting PID file /var/run/pt-heartbeat/pt-heartbeat.pid because the PID that it contains, 17572, is not running at /usr/bin/pt-heartbeat line 2213.
Overwriting PID file /var/run/pt-heartbeat/pt-heartbeat.pid because the PID that it contains, 17572, is not running at /usr/bin/pt-heartbeat line 2213.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment