mySQL Slow Query Log Email Monitor

By Hawkee on Mar 09, 2011

This is a very useful script for monitoring mySQL load on a server. It will check your mySQL slow query log file and email the contents to you. It also appends the size of the file to the email subject to give you an idea of how severe the problem is at a glance. Here are the settings that go into your my.cnf file:

slow_query_log = 1
slow_query_log_file = /var/log/mysql-slow.log
long_query_time = 30

Make sure mysql-slow.log is created and has the proper permissions. In most cases it should be owned by the "mysql" user. Feel free to adjust the long_query_time as you see fit.

You will probably want to put this in your crontab. This command will execute the script every 15 minutes. Just be sure to give the .sh file the proper permissions to execute.

0,15,30,45 * * * *  /path/to/db_slow_queries.sh > /dev/null 2>&1
file="/var/log/mysql-slow.log"
email="myemail@domain.com"

size=$(stat -c %s $file)
if [ "$size" -gt 1 ]
then
   cat $file | mail -s "mySQL slow query log - $size" $email
   :>$file
fi

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.