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 +++++++++++++++++++++++++++--------- url.class | 2 +- 2 files changed, 28 insertions(+), 10 deletions(-) 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"))) { diff --git a/url.class b/url.class index ebe42a5..c1be258 100644 --- a/url.class +++ b/url.class @@ -363,7 +363,7 @@ function get_cache($p = array()) if (($parts = parse_url($p['url'])) === false) it::error('malformed url ' . $p['url']); if ($p['safety'] == 1) - it::error(array('title'=>"get_cache: download failures on $path", 'ok_key'=>md5($parts['host']), 'ok_delay'=>$p['maxage'], 'ok'=>$result ? 1 : 0)); # send err only if multi failure + it::error(array('title'=>"get_cache: download failures on {$p['url']}", 'ok_key'=>"ok_".$parts['host'], 'ok'=>$result ? 1 : 0)); # send err only if multi failure @unlink("$path.lock"); } -- cgit v1.2.3