From 25778ef80fedaefaf16cea3a75dc554cfe7e2376 Mon Sep 17 00:00:00 2001 From: Urban Müller Date: Wed, 17 Jan 2007 19:05:11 +0000 Subject: it::error: fewer alerts, better performance --- it.class | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'it.class') diff --git a/it.class b/it.class index 5757b4f..588d279 100644 --- a/it.class +++ b/it.class @@ -66,10 +66,12 @@ function timerlog($label = '') * @param $to (optional) comma separated recipient list * @param named parameter ok indicates no error happened * @param named parameter ok_key indicates which variable to store failure counts in - * @param named parameter ok_delay gives number of seconds in which consecutive failures dont count + * @param named parameter ok_maxfail number of failures needed for alert; default 2. only 1 failure per 60 seconds counted */ function error($p=array(), $body='', $to='') { + $p += array('ok_key' => "it_error", 'ok_maxfail' => 2); + if (is_array($p)) { $title = $p['title']; $body = $p['body']; @@ -82,22 +84,38 @@ function error($p=array(), $body='', $to='') if (!$to) $to = strtr(trim(@file_get_contents($GLOBALS['ULTRAHOME'] . "/.diffnotice")), array("\n"=>',', ' '=>'')); - if ($p['ok']) { - system("alert -o " . escapeshellarg($p['ok_key']) . "=1" . escapeshellarg($to) . " 4 'everything ok'"); - } else { + if (!isset($p['ok'])) + $sendalert = true; + else { + $counterfn = $GLOBALS['ULTRAHOME'] . "/tmp/" . urlencode($p['ok_key']); + if (time() - @filemtime($counterfn) >= 60) { # only modify counter if unmodified for 60 secs + $oldcounter = intval(@file_get_contents($counterfn)); + $newcounter = $p['ok'] ? 0 : $oldcounter + 1; + if ($newcounter != $oldcounter) { + $fh = fopen($counterfn, "w"); + fputs($fh, "$newcounter\n"); + fclose($fh); + + if ($newcounter >= $p['ok_maxfail'] && (time() - @filemtime("$counterfn.mailsent")) >= 86400) { # 1 mail per day + $sendalert = true; + if (!ereg('twin|devel', $GLOBALS['ULTRASERVERTYPE'])) + touch("$counterfn.mailsent"); + } + } + } + } + + if ($sendalert) { $url = ($_SERVER['HTTPS'] ? "https://" : "http://") . $_SERVER['HTTP_HOST'] .$_SERVER['REQUEST_URI']; - $referer = $_SERVER['HTTP_REFERER']; if (!$title) $title="Error in $url"; - $body .= "\nUrl: $url\n\nReferer: $referer\n\n\$_REQUEST: ".print_r($_REQUEST, true). "\nStack:\n" . print_r(array_slice(debug_backtrace(), 1), true) . "\n\$_SERVER: " . print_r($_SERVER, true); + $body .= "\nUrl: $url\n\n\$_REQUEST: ".print_r($_REQUEST, true). "\nStack:\n" . print_r(array_slice(debug_backtrace(), 1), true) . "\n\$_SERVER: " . print_r($_SERVER, true); if (ereg('twin|devel', $GLOBALS['ULTRASERVERTYPE'])) { echo "
$title\n$body\n"; } else { - $okdelay = $p['ok_delay'] ? "-d " . escapeshellarg($p['ok_delay']) : ""; - $okmode = $p['ok_key'] ? "-o " . escapeshellarg($p['ok_key']) . "=0" : ""; - $cmd = "alert $okdelay $okmode -l " . escapeshellarg($to) . " 4 " . escapeshellarg($title); + $cmd = "alert -l " . escapeshellarg($to) . " 4 " . escapeshellarg($title); EDC('exec', $cmd); if (($pipe = popen($cmd, "w"))) { -- cgit v1.2.3