egscripts/egdbstatus/egdbstatus-pinchy

81 lines
2.6 KiB
Text
Raw Permalink Normal View History

#!/bin/bash
#
# egdbback, copyright 2008 Egressive Limited, http://egressive.com
#
# this script checks the status of a mysql database, eg. replication status...
#
#==========================================
# The Variables - these must be define
# before they're referenced
#==========================================
VERSION="0.1"
EGDB_NAME=`basename $0`
EGDB_DIR=/etc/egscripts/egdbstatus
EGDB_CMD=$EGDB_DIR/egdbstatus-pinchy
SITE_CONF="$EGDB_DIR/site-pinchy.conf"
#
# this provides values for MACHINE_NAME and EMAIL
. $SITE_CONF
#
LOGS=/var/log
LOGFILE=$LOGS/egdbstatus-pinchy.log
#
LASTRUN=/tmp/.egdbstatus-pinchy
# required programs
MAIL=`which mail`
GREP=`which grep`
LS=`which ls`
ID=`which id`
DATE=`which date`
DF=`which df`
GZIP=`which gzip`
GZIPSUF=gz
RM=`which rm`
LN=`which ln`
CUT=`which cut`
AWK=`which awk`
CP=`which cp`
CAT=`which cat`
FIND=`which find`
#
#==========================================
# MySQL vars...
MYSQL=`which mysql`
# Set the correct socket to use
MYSQL="$MYSQL -S /var/run/mysqld/mysqld.pinchy.sock"
STATUS="[SUCCESS]"
date > $LOGFILE
ERRNO=`echo "SHOW SLAVE STATUS\G" | $MYSQL -u $USER -p$PASSWORD | $GREP "Last_Errno" | $AWK '{ print $2; }'`
if [ "$ERRNO" != "0" ]; then
echo "SHOW SLAVE STATUS\G" | $MYSQL -u $USER -p$PASSWORD | $GREP "Last_Error" | $AWK '{ print $2; }' >> $LOGFILE
STATUS="[ERROR]"
fi
SLAVE_STATUS=`echo "SHOW SLAVE STATUS\G" | $MYSQL -u $USER -p$PASSWORD | $GREP "Slave_IO_State" | $AWK -F':' '{ print $2; }' | sed "s/^[ \t]*//g"`
if [ "$SLAVE_STATUS" != "Waiting for master to send event" -a "$SLAVE_STATUS" != "Queueing master event to the relay log" ] ; then
echo $SLAVE_STATUS >> $LOGFILE
echo " See http://dev.mysql.com/doc/refman/5.0/en/slave-io-thread-states.html for more info" >> $LOGFILE
STATUS="[ERROR]"
fi
SECONDS_BEHIND=`echo "SHOW SLAVE STATUS\G" | $MYSQL -u $USER -p$PASSWORD | $GREP "Seconds_Behind_Master" | $AWK -F':' '{ print $2; }' | sed "s/^[ \t]*//g"`
if [ "$SECONDS_BEHIND" = "NULL" -o `echo "$SECONDS_BEHIND > 3600" | bc` -ne 0 ] ; then
echo "Slave is too far behind or null: $SECONDS_BEHIND" >> $LOGFILE
STATUS="[ERROR]"
fi
# We have found an error or we havn't reported in the 6 hours, report it
if [ "$STATUS" == "[ERROR]" ] || [ ! -e $LASTRUN ] || [ -n "`$FIND $LASTRUN -mmin +359`" ]; then
echo "+++++++++++++++++++++++++++++++++++++++++"
echo " Full status of the slave"
echo "+++++++++++++++++++++++++++++++++++++++++"
echo
echo "SHOW SLAVE STATUS\G" | $MYSQL -u $USER -p$PASSWORD >> $LOGFILE
HOST=`hostname`
$CAT $LOGFILE | $MAIL -s "$STATUS Replication on $HOST (Pinchy)" $EMAIL
touch $LASTRUN
fi