diff options
author | Urban Müller | 2007-01-17 19:05:11 +0000 |
---|---|---|
committer | Urban Müller | 2007-01-17 19:05:11 +0000 |
commit | 25778ef80fedaefaf16cea3a75dc554cfe7e2376 (patch) | |
tree | a89fb58846913c48ebcfb6c53455c06ee6b6139b | |
parent | ce579f09143160e84aba768bc38054d09a11063f (diff) | |
download | itools-25778ef80fedaefaf16cea3a75dc554cfe7e2376.tar.gz itools-25778ef80fedaefaf16cea3a75dc554cfe7e2376.tar.bz2 itools-25778ef80fedaefaf16cea3a75dc554cfe7e2376.zip |
it::error: fewer alerts, better performance
-rw-r--r-- | it.class | 36 | ||||
-rw-r--r-- | url.class | 2 |
2 files changed, 28 insertions, 10 deletions
@@ -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 "<pre>$title\n$body\n</pre>"; } 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"))) { @@ -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"); } |