From 06dd638946d9f0471bb9e267804eb3d0ee30365e Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Thu, 4 Oct 2007 19:14:34 +0000 Subject: eliminate alert binary --- it.class | 99 ++++++++++++++++++++++++++++------------------------------------ 1 file changed, 43 insertions(+), 56 deletions(-) diff --git a/it.class b/it.class index 281853a..77b4b89 100644 --- a/it.class +++ b/it.class @@ -127,77 +127,64 @@ function timerlog($label = '') */ function error($p = array(), $body = "", $to = "") { - if (is_array($p)) - { - $title = $p['title']; - $body = $p['body']; - $to = $p['to']; - } - else - { - $title = $p; - $p = array(); - } - $p += array('timewindow' => 25*3600, 'graceperiod' => 60, 'backtraceskip' => 0); + if (!is_array($p)) + $p = array('title' => $p, 'body' => $body, 'to' => $to); + + if ($_SERVER['REMOTE_ADDR']) + $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; - if (!$to) - $to = strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>',', ' '=>'')); + $p += array( + 'timewindow' => 25*3600, + 'graceperiod' => 60, + 'backtraceskip' => 0, + 'to' => strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>', ')), + 'title' => "Error in $url", + ); - if ($p['id']) + $toscreen = !ereg('live', $GLOBALS['ULTRASERVERTYPE']); + if (!$toscreen) # this error can only be sent by mail: find out if we want to suppress it { - $stampfn = $GLOBALS['ULTRAHOME'] . "/tmp/errstamp_" . urlencode($p['id']); - $stampage = time() - @filemtime($stampfn); - $lastsentfn = "/tmp/alertdata/" . $p['id']; + @mkdir("/tmp/alertdata"); + @chmod("/tmp/alertdata", 0777); - if ($stampage >= $p['graceperiod'] && $stampage <= $p['graceperiod'] + $p['timewindow']) - $sendalert = true; + if ($p['id']) + { + $stampfn = "/tmp/alertdata/errstamp_" . urlencode($p['id']); + $stampage = time() - @filemtime($stampfn); + $sendmail = $stampage >= $p['graceperiod'] && $stampage <= $p['graceperiod'] + $p['timewindow']; + } + else + { + $stampfn = "/tmp/alertdata/lastsent_" . getmyuid() . "." . md5($p['to']); + $sendmail = time() - @filemtime($stampfn) > 3600; + } - if ($stampage >= $p['graceperiod']) # constantly occurring errors should not suppress mail + if ($sendmail || $stampage > $p['graceperiod']) # mark time of sending mail or of encountering initial $p['id'] error + { + @unlink($stampfn); touch($stampfn); + } } - else - { - $lastsentfn = "/tmp/alertdata/" . getmyuid() . "." . $to; - $sendalert = true; - } - - $toscreen = !ereg('live', $GLOBALS['ULTRASERVERTYPE']); - if ($sendalert && ($toscreen || (time() - @filemtime($lastsentfn)) > 3600)) + if ($toscreen || $sendmail) { - @mkdir("/tmp/alertdata"); - @chmod("/tmp/alertdata", 0777); - @unlink($lastsentfn); - touch($lastsentfn); - @chmod($lastsentfn, 0666); - - if ($_SERVER['REMOTE_ADDR']) - $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; - if (!$title) - $title="Error in " . trim($url); - - if (!function_exists('memory_get_usage') || (memory_get_usage() < 50000000)) - $trace = it_debug::backtrace($p['backtraceskip']); + $trace = it_debug::backtrace($p['backtraceskip']); # in here for performance - $body .= ($url && !$toscreen? "Url: $url\n" : "") . ($trace ? " Trace: $trace\n" : ""); + $body = ($body ? trim($body)."\n\n" : "") . ($url && !$toscreen? "Url: $url\n\n" : "") . ($trace ? ($sendmail?"" :" ")."Trace: $trace\n\n" : ""); - if (!$toscreen) + if ($sendmail) # we're mailing: send maximum info { - $cmd = "alert -l " . escapeshellarg($to) . " 4 " . escapeshellarg($title); + $body .= "Locals: " . print_r($p['locals'], true) . "\n\n\$_REQUEST: ".print_r($_REQUEST, true) . "\n\$_SERVER: " . print_r($_SERVER, true) . "\n\n"; + if ($trace) # this is only set if no excessive memory usage + $body .= "Stack: " . print_r(debug_backtrace(), true) . "\n\n"; - EDC('exec', $cmd); - if (($pipe = popen($cmd, "w"))) - { - $body .= "\n\nLocals" . print_r($p['locals'], true) . "\n\n\$_REQUEST: ".print_r($_REQUEST, true) . "\n\$_SERVER: " . print_r($_SERVER, true) . "\n\nStack:\n" . print_r(debug_backtrace(), true); - fputs($pipe, "$body\n"); - pclose($pipe); - } + mail($p['to'], $p['title'], $body); } - else if ($_SERVER['REMOTE_ADDR']) - echo "
$title\n$body"; + else if ($_SERVER['REMOTE_ADDR']) # in shell mode we rely on error_log below + echo "
{$p['title']}\n$body"; } - error_log("it::error: ".$title . ": " . $trace . (EDC('verbose') ? D($p['locals']) : "")); + error_log("it::error: " . $p['title'] . " in " . ($trace ? $trace : $p['line']) . (EDC('verbose') ? D($p['locals']) : "")); } @@ -453,6 +440,7 @@ function imageconvert($p) * Option text must be indented; if long and short option present, value is stored in long option * Options without arguments store true or false under their key * Options -h and --help will be handled internally by printing usage and exiting + * When printing, the usage will be de-indented so the first line starts in the first column * Two or more blanks must be in front of option explanation * @param $helplines Usage parsed to determine options * @return Associative array of options @@ -605,7 +593,6 @@ function date($format = "", $stamp = null) return date($formatstring, $stamp); } - } ?> -- cgit v1.2.3