Monitory MySQL Error Log

A good dba keeps an eye on the Error Logs, and slow query logs often. This should be a proactive activity to prevent the reactive cleanup. If you’re like me you’d often forget to look, or the log fills at strange times of the day or night. Like about 99% of all other maint work lets automate this one. This is how I do it:

$LOG_PATH = ‘/var/log/mysql/error.log’;
$SERVER_NAME = php_uname(“n”);

// CRONTAB 59 * * * * /var/dbscripts/db_error_monitor.php


$EDATE = date(‘ymd H’);

$error = exec(“sudo grep -e ‘”.$EDATE.”‘ “.$LOG_PATH.” | grep -i ‘Error’”);
$warning = exec(“sudo grep -e ‘”.$EDATE.”‘ “.$LOG_PATH.” | grep -i ‘Warning’”);
$note = exec(“sudo grep -e ‘”.$EDATE.”‘ “.$LOG_PATH.” | grep -i ‘Note’”);

if ($error){
echo “ERROR”;
sendError($ADMIN_EMAIL,”ERROR: “.$SERVER_NAME,$error);
if ($warning){
echo “WARNING”;
sendError($ADMIN_EMAIL,”WARNING: “.$SERVER_NAME,$warning);
if ($note){
echo “NOTE”;
sendError($ADMIN_EMAIL,”NOTE: “.$SERVER_NAME,$note);

function sendError($ADMIN_EMAIL,$subject,$error){
if (mail($ADMIN_EMAIL, $subject, $error)) {
echo(“<p>Message successfully sent!</p>”);
} else {
echo(“<p>Message delivery failed…</p>”);

Posted in MySQL

Leave a Reply