diff --git a/phplib/logger.php b/phplib/logger.php index 8c53362..015ee79 100644 --- a/phplib/logger.php +++ b/phplib/logger.php @@ -1,74 +1,91 @@ setFormatter($formatter); $logger->pushHandler($syslog); $logger->pushProcessor(new WebProcessor(null, ['ip'])); global $browserLogger; $browserLogger = new Logger('CDRTool'); $console= new BrowserConsoleHandler(); $browserLogger->pushHandler($console); function logger($message, $level = 'notice') { - global $logger; if ($level == 'notice') { notice($message); } elseif ($level == 'error') { error($message); } elseif ($level == 'critical') { critical($message); } } +function loggerAndPrint($message, $level = 'notice') +{ + if ($level == 'notice') { + noticeAndPrint($message); + } +} + function notice($message) { global $logger; $logger->notice($message); } function warning($message) { global $logger; $logger->warning($message); } function error($message) { global $logger; $logger->error($message); } function critical($message) { global $logger; $logger->critical($message); } +function noticeAndPrint($message) +{ + print "$message"; + notice($message); +} + function warningAndPrint($message) { global $logger; print "$message"; $logger->warning($message); } function errorAndPrint($message) { global $logger; print "$message"; $logger->error($message); } +function criticalAndPrint($message) +{ + print "$message"; + critical($message); +} diff --git a/scripts/normalize.php b/scripts/normalize.php index 8f41de7..6a8804b 100755 --- a/scripts/normalize.php +++ b/scripts/normalize.php @@ -1,110 +1,115 @@ #!/usr/bin/env php setFormatter($formatter); +$logger->pushHandler($syslog); + $lockFile = "/var/lock/CDRTool_normalize.lock"; if ($argv[1]) { if (preg_match("/^\d{4}\d{2}$/", $argv[1], $m)) { $table = 'radacct'.$argv[1]; } else { die("Error: Month must be in YYYYMM format\n"); } } if ($f = fopen($lockFile, "w")) { if (flock($f, LOCK_EX + LOCK_NB, $w)) { if ($w) { - print "Another CDRTool normalization is in progress. Aborting.\n"; - syslog(LOG_NOTICE, "Another CDRTool normalization is in progress. Aborting."); + criticalAndPrint("Another CDRTool normalization is in progress. Aborting.\n"); exit(2); } } else { - print "Another CDRTool normalization is in progress. Aborting.\n"; - syslog(LOG_NOTICE, "Another CDRTool normalization is in progress. Aborting."); + criticalAndPrint("Another CDRTool normalization is in progress. Aborting.\n"); exit(1); } } else { $log = sprintf("Error: Cannot open lock file %s for writing\n", $lockFile); - print $log; - syslog(LOG_NOTICE, $log); + criticalAndPrint($log); exit(2); } foreach ($DATASOURCES as $k => $v) { if (strlen($v["normalizedField"] and !$v["skipNormalize"])) { $b = time(); $class_name = $v["class"]; unset($CDRS); $CDRS = new $class_name($k); if (is_array($CDRS->db_class)) { $db_class = $CDRS->db_class[0]; } else { $db_class = $CDRS->db_class; } if ($table) $CDRS->table = $table; $log = sprintf("Normalize datasource %s, database %s, table %s\n", $k, $db_class, $CDRS->table); - print $log; - syslog(LOG_NOTICE, $log); + loggerAndPrint($log); $CDRS->NormalizeCDRS(); $e = time(); $d = $e - $b; if ($CDRS->status['cdr_to_normalize']) { $speed = 0; if ($d) $speed = number_format($CDRS->status['cdr_to_normalize']/$d, 0, "", ""); $log = sprintf( " %d CDRs, %d normalized in %s s @ %s cps\n", $CDRS->status['cdr_to_normalize'], $CDRS->status['normalized'], $d, $speed ); - print $log; - syslog(LOG_NOTICE, $log); + loggerAndPrint($log); } if (!$table && preg_match("/^(\w+)\d{6}$/", $CDRS->table, $m)) { $lastMonthTable=$m[1].date('Ym', mktime(0, 0, 0, date("m")-1, "01", date("Y"))); $log = sprintf("Normalize datasource %s, database %s, table %s\n", $k, $db_class, $lastMonthTable); - print $log; - syslog(LOG_NOTICE, $log); + loggerAndPrint($log); $b = time(); $CDRS->table = $lastMonthTable; $CDRS->NormalizeCDRS(); if ($CDRS->status['cdr_to_normalize']) { $e = time(); $d = $e - $b; $speed = 0; if ($d) $speed = number_format($CDRS->status['cdr_to_normalize']/$d, 0, "", ""); $log = sprintf( " %d CDRs, %d normalized in %s s @ %s cps\n", $CDRS->status['cdr_to_normalize'], $CDRS->status['normalized'], $d, $speed ); - print $log; - syslog(LOG_NOTICE, $log); + loggerAndPrint($log); } } } } keepAliveRatingEngine();