Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

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 = "[email protected]";
$to = "[email protected]";
$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.