Monday, December 3, 2007

WordPress Plugin: MySQL Server Crash Monitor

Since end of October our MySQL Server crashed on or two times a day. We don't no exactly why. Maybe the Databases are to big. We run several WordPress system on this MySQL Server. but the MySQL services often crashed, then my WordPress can't connect to MySQL server and show the error. So I develop this program to monitor the WordPress MySQL server, when the server is down, WordPress will auto send me an e-mail and write to a text log file in a recorded. We can look trough the log file to analysis the MySQL server databse, in order to cantact the host administrators to fix the error.

Here is the source code of the MySQL Server Crash Monitor.


// Change the e-mail address below .
$from = "webmaster@moon-blog.com";
$to = "webmaster@moon-blog.com";
$subject = "MySQL Crashed!";
$body = date("Y-m-d H:i:s");
$headers = 'From: '.$from . "\r\n"
.'Reply-To: '.$from . "\r\n"
.'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $body, $headers);
// Log to file
$filename = 'log.txt';
$somecontent = date("Y-m-d H:i:s");
$somecontent = $somecontent . "\r\n";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
exit;
}
if (!fwrite($handle, $somecontent)) {
exit;
}
fclose($handle);
}


First, upload a file named log.txt to the WordPress root directory, chmod it to 666.

Second, replace the e-mail address of the code to your own e-mail.

Then,opening wp-includes/wp-db.php, find "if (! $ This-> dbh)", 66 lines in WordPress 2.3.1, paste the code after that. Save and upload the file.

Now, When the database lost connection or get problems, WordPress will automatically mail the error message to your mailbox, at the same time the message will be recorded in log.txt document.

2 comments:

  1. Matthew PatulskiJune 19, 2008 at 8:44 PM

    Hello,
    We are trying to debug a server that will run wordpress 2.3.3 but chokes on 2.5.1 will this work with wordpress 2.5.1?

    Best regards,
    Matthew

    ReplyDelete
  2. Check out these 26 traffic machines that will take a your product to a new level by pulling traffic from a lot of different places.

    ReplyDelete