added db errorlogging and admin-email throttling

This commit is contained in:
following
2013-04-17 16:05:44 +02:00
parent 98b1e0858d
commit 7ec5312960
4 changed files with 78 additions and 25 deletions

View File

@ -2,6 +2,6 @@ The Firefox seach plugin has been traditionally provided at
http://cms.opencaching.de/fileadmin/files/search. This domain is now
redirected to www.opencaching.de. We try to migrate the search plugin
to http://www.opencaching.de/resource2/misc/mozilla, but as there is
an unknown number of old copues of the .src file out there, the old
an unknown number of old copies of the .src file out there, the old
path is still needed for a while. (Analyze webserver accesslog to
find out wenn it can be discarded!)

View File

@ -445,7 +445,8 @@
$email_content = $msql_error;
$email_content .= "\n--------------------\n";
$email_content .= print_r(debug_backtrace(), true);
mb_send_mail($sql_errormail, 'sql_error: ' . $absolute_server_URI, $email_content, $emailheaders);
if (admin_errormail($sql_errormail, 'sql_error', $email_content, $emailheaders))
mb_send_mail($sql_errormail, 'sql_error: ' . $absolute_server_URI, $email_content, $emailheaders);
}
if ($interface_output == 'html')
@ -490,7 +491,8 @@
$email_content .= "\n--------------------\n";
$email_content .= print_r(debug_backtrace(), true);
@mb_send_mail($sql_errormail, 'sql_warn: ' . $absolute_server_URI, $email_content, $emailheaders);
if (admin_errormail($sql_errormail, 'sql_warn', $email_content, $emailheaders))
@mb_send_mail($sql_errormail, 'sql_warn: ' . $absolute_server_URI, $email_content, $emailheaders);
}
/*

View File

@ -910,20 +910,26 @@
if ($opt['db']['error']['mail'] != '')
{
require_once($opt['rootpath'] . 'lib2/mail.class.php');
$mail = new mail();
$mail->subject = $opt['db']['error']['subject'];
$mail->to = $opt['db']['error']['mail'];
if (admin_errormail($opt['db']['error']['mail'],
$opt['db']['error']['subject'],
str_replace("\n","\r\n",$error) . "\n" . print_r(debug_backtrace(), true),
"From: ".$opt['mail']['from']))
{
require_once($opt['rootpath'] . 'lib2/mail.class.php');
$mail = new mail();
$mail->subject = $opt['db']['error']['subject'];
$mail->to = $opt['db']['error']['mail'];
$mail->name = 'sql_error';
$mail->name = 'sql_error';
$mail->assign('errno', $errno);
$mail->assign('error', str_replace("\n","\r\n",$error));
$mail->assign('trace', print_r(debug_backtrace(), true));
$mail->assign('errno', $errno);
$mail->assign('error', str_replace("\n","\r\n",$error));
$mail->assign('trace', print_r(debug_backtrace(), true));
$mail->send();
$mail = null;
}
$mail->send();
$mail = null;
}
}
if ($opt['gui'] == GUI_HTML)
{
@ -958,18 +964,24 @@
if ($opt['db']['error']['mail'] != '')
{
require_once($opt['rootpath'] . 'lib2/mail.class.php');
$mail = new mail();
$mail->name = 'sql_warn';
$mail->subject = $opt['db']['warn']['subject'];
$mail->to = $opt['db']['warn']['mail'];
if (admin_errormail($opt['db']['error']['mail'],
$opt['db']['warn']['subject'],
$warnmessage . "\n" . print_r(debug_backtrace(), true),
"From: ".$opt['mail']['from']))
{
require_once($opt['rootpath'] . 'lib2/mail.class.php');
$mail = new mail();
$mail->name = 'sql_warn';
$mail->subject = $opt['db']['warn']['subject'];
$mail->to = $opt['db']['warn']['mail'];
$mail->assign('warnmessage', $warnmessage);
$mail->assign('trace', print_r(debug_backtrace(), true));
$mail->assign('warnmessage', $warnmessage);
$mail->assign('trace', print_r(debug_backtrace(), true));
$mail->send();
$mail = null;
}
$mail->send();
$mail = null;
}
}
}
function sql_export_recordset($f, $rs, $table, $truncate=true)

View File

@ -54,7 +54,7 @@ function shutdownhandler()
{
$error_handled = true;
$error = "(" . $error['type'] . ") " . $error['message'] .
$error = "(" . $error['type'] . ") " . $error['message'] .
" at line " . $error['line'] . " of " . $error['file'];
send_errormail($error);
@ -90,4 +90,43 @@ function send_errormail($errmsg)
}
}
// throttle admin error mails;
// currently used only for SQL errors and warnings
function admin_errormail($to, $errortype, $message, $headers)
{
global $opt;
$errorlog_dir = $opt['rootpath'] . 'cache2/errorlog';
$errorlog_path = $errorlog_dir . "/errorlog-" . date("Y-m-d");
$error_mail_limit = 65536; // send max 64 KB = ca. 10-30 errors per day/logfile
// All errors which may happen here are ignored, to avoid error recursions.
if (!is_dir($errorlog_dir))
@mkdir($errorlog_dir);
$old_logsize = @filesize($errorlog_path) + 0;
$msg = date("Y-m-d H:i:s.u")." ".$errortype."\n" . $message."\n" .
"-------------------------------------------------------------------------\n\n";
@error_log($msg,
3, // log to file
$errorlog_path);
// @filesize() may still return the old size here, because logging takes place
// asynchronously. Instead we calculate the new size:
$new_logsize = $old_logsize + strlen($msg);
if ($old_logsize < $error_mail_limit && $new_logsize >= $error_mail_limit)
{
mb_send_mail($to, "too many ".$errortype,
"Errors/Warnings are recorded in ".$errorlog_path.".\n" .
"Email Reporting is DISABLED for today now. Please check the logfile\n" .
"and RENAME or delete it when done, so that logging is re-enabled.",
$headers);
return false;
}
else
return ($old_logsize < $error_mail_limit);
}
?>