mysqld monitor

bash script for monitoring mysqld

This little script will test if mysql is running on your local or remote mysql-server. If there is any trouble, it will try to restart and send a report by mail.

The script can easily be modified to monitor some other process. Replace for example this line to monitor Apache:

$MADMIN -h $mhost -u $user -p${pass} ping 2>/dev/null 1>/dev/null

with this:

pgrep apache2 >/dev/null
#!/bin/bash

#####
# Mysql Monitoring Script
# - Tests if mysqld is running
# - if not, it will try to restart and send an email
# Run as cron Job
#####
user="user"
pass="password"
mhost="localhost"
restart="/etc/init.d/mysql start"
emailaddres="user@localhost "
mailcmd="$(which mail)"
MADMIN="$(which mysqladmin)"

######

MAILMESSAGE="/tmp/mysql.fail.$$"

$MADMIN -h $mhost -u $user -p${pass} ping 2>/dev/null 1>/dev/null
if [ $? -ne 0 ]; then
echo "" >$MAILMESSAGE
echo "Error: MySQL Server is not running/responding ping request">>$MAILMESSAGE
echo "Hostname: $(hostname)" >>$MAILMESSAGE
echo "Date & Time: $(date)" >>$MAILMESSAGE
cat $MAILMESSAGE

echo "...try restart"
$restart>/dev/null
# see if it is started or not
d=$(ps cax | grep -c ' mysqld$')
if [ $d -eq 1 ]; then
sMess="MySQL Server MySQL server successfully restarted"
echo $sMess
else
sMess="MySQL server FAILED to restart"
echo $sMess
fi
# Email status
echo "Current Status: $sMess" >>$MAILMESSAGE
echo "" >>$MAILMESSAGE
# send email
$mailcmd -s "MySQL server" $emailaddres < $MAILMESSAGE
else
echo "MySQL serverstatus: ok"
fi
# remove file
rm -f $MAILMESSAGE

Post your comment

Comments

No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments