diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server index 5c296b35f8..31068a1a34 100755 --- a/pandora_server/bin/pandora_server +++ b/pandora_server/bin/pandora_server @@ -270,6 +270,9 @@ sub pandora_server_tasks ($) { # Update forced alerts pandora_exec_forced_alerts ($pa_config, $dbh); + + # Rotate Log File + pandora_rotate_logfile($pa_config); } # TASKS EXECUTED EVERY 30 SECONDS (Mid latency tasks) diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 708a874ca7..8534af25c6 100644 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -62,6 +62,7 @@ our @EXPORT = qw( cron_next_execution_date pandora_daemonize logger + pandora_rotate_logfile limpia_cadena md5check float_equal @@ -441,7 +442,7 @@ sub logger ($$;$) { $level = 1 unless defined ($level); return if ($level > $pa_config->{'verbosity'}); - + if (!defined($pa_config->{'logfile'})) { print strftime ("%Y-%m-%d %H:%M:%S", localtime()) . " [V". $level ."] " . $message . "\n"; return; @@ -465,20 +466,33 @@ sub logger ($$;$) { syslog($security_level, $message); closelog(); } else { - # Log rotation - if (-e $file && (stat($file))[7] > $pa_config->{'max_log_size'}) { - foreach my $i (reverse 1..$pa_config->{'max_log_generation'}) { - rename ($file . "." . ($i - 1), $file . "." . $i); - } - rename ($file, "$file.0"); - } - open (FILE, ">> $file") or die "[FATAL] Could not open logfile '$file'"; + # Get an exclusive lock on the file (LOCK_EX) + flock (FILE, 2); print FILE strftime ("%Y-%m-%d %H:%M:%S", localtime()) . " " . $pa_config->{'servername'} . $pa_config->{'servermode'} . " [V". $level ."] " . $message . "\n"; close (FILE); } } +######################################################################## +# SUB pandora_rotate_log (pa_config) +# Log to file +######################################################################## +sub pandora_rotate_logfile ($) { + my ($pa_config) = @_; + + my $file = $pa_config->{'logfile'}; + + # Log File Rotation + if ($file ne 'syslog' && -e $file && (stat($file))[7] > $pa_config->{'max_log_size'}) { + foreach my $i (reverse 1..$pa_config->{'max_log_generation'}) { + rename ($file . "." . ($i - 1), $file . "." . $i); + } + rename ($file, "$file.0"); + + } +} + ######################################################################## # limpia_cadena (string) - Purge a string for any forbidden characters (esc, etc) ########################################################################