#!/bin/bash
#
# MySQL Check Database Script
# Version 0.1
# Copyright (c) 2005 stefan@stefan-weigand.de
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#=====================================================================
#=====================================================================
# Set the following variables to your system needs
# (Detailed instructions below variables)
#=====================================================================

# Username to access the MySQL server e.g. dbuser
USERNAME=dbuser

# Username to access the MySQL server e.g. password
PASSWORD=password

# Host name (or IP address) of MySQL server e.g localhost
DBHOST=localhost

# List of DBNAMES for check e.g. "DB1 DB2 DB3"
DBNAMES="DB1 DB2"

# Email Address to send mail to? (user@domain.com)
MAILADDR="user@domain.com"

#=====================================================================
# Change Log
#=====================================================================
#
# Version 0.1 - (2005-11-08)
#   Initial Release
#
#
#=====================================================================
#=====================================================================
#=====================================================================
#
# Should not need to be modified from here down!!
#
#=====================================================================
#=====================================================================
#=====================================================================
PATH=/usr/local/mysql/bin:/usr/local/bin:/usr/bin:/bin
DATE=`date +%Y-%m-%d`                           # Datestamp e.g 2002-09-21
VER=0.1                                         # Version Number
MDBNAMES="mysql $DBNAMES"
LOGFILE=/var/log/mysqlcheck.log			# Logfile name


# IO redirection for logging.
touch $LOGFILE
exec 6>&1           # Link file descriptor #6 with stdout.
                    # Saves stdout.
exec > $LOGFILE     # stdout replaced with file $LOGFILE.

# Functions

# Databasecheck
dbcheck () {
mysqlcheck --user=$USERNAME --password=$PASSWORD --host=$DBHOST $1 
return 0
}

# Hostname for LOG information
if [ "$DBHOST" = "localhost" ]; then
        HOST=`hostname`
else
        HOST=$DBHOST
fi

echo ======================================================================
echo MySQL Checkdatabase VER $VER
echo
echo Checking Databases on Server - $HOST
echo ======================================================================
echo Start Time `date`
echo ======================================================================

for MDB in $MDBNAMES
do
	echo Check of $MDB...
	dbcheck "$MDB"
	echo ----------------------------------------------------------------------
done
echo End Time `date`
echo ======================================================================

#Clean up IO redirection
exec 1>&6 6>&-      # Restore stdout and close file descriptor #6.

cat "$LOGFILE" | mail -s "MySQL Check for $HOST - $DATE" $MAILADDR

# Clean up Logfile
eval rm -f "$LOGFILE"

exit 0

