summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUrban Müller2007-10-04 19:14:34 +0000
committerUrban Müller2007-10-04 19:14:34 +0000
commit06dd638946d9f0471bb9e267804eb3d0ee30365e (patch)
tree2877afca7ee4e62247e3ccf48ae5ae2c6a2d22d9
parentfdfbe9af607f280e40b13117451f624ddce74e4a (diff)
downloaditools-06dd638946d9f0471bb9e267804eb3d0ee30365e.tar.gz
itools-06dd638946d9f0471bb9e267804eb3d0ee30365e.tar.bz2
itools-06dd638946d9f0471bb9e267804eb3d0ee30365e.zip
eliminate alert binary
-rw-r--r--it.class99
1 files 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 "<pre>$title\n$body</pre>";
+ else if ($_SERVER['REMOTE_ADDR']) # in shell mode we rely on error_log below
+ echo "<pre>{$p['title']}\n$body</pre>";
}
- 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);
}
-
}
?>