use POSIX qw(locale_h sys_wait_h);
setlocale(LC_NUMERIC, '');
setlocale(LC_ALL, 'C');
-use File::Temp qw/ :seekable /;
+use File::Temp qw/ :seekable tempfile /;
use Proc::Queue size => 1, ':all';
$VERSION = '2.3';
+$SIG{'CHLD'} = 'DEFAULT';
+
####
# method used to fork as many child as wanted
##
-my @global_pids = ();
sub spawn
{
my $coderef = shift;
print STDERR "Error: cannot fork: $!\n";
return;
} elsif ($pid) {
- push(@global_pids, $pid);
return; # the parent
}
# the child -- go spawn
}
# With multiprocess we need to wait all childs
+my $abort = 0;
sub wait_child
{
+ my $sig = shift;
+ $abort = 1;
+ print STDERR "Received terminating signal ($sig).\n";
1 while wait != -1;
$SIG{INT} = \&wait_child;
$SIG{TERM} = \&wait_child;
}
$SIG{INT} = \&wait_child;
$SIG{TERM} = \&wait_child;
-$SIG{QUIT} = \&wait_child;
-$SIG{'CHLD'} = 'DEFAULT';
+
$| = 1;
"G|nograph!" => \$nograph,
"h|help!" => \$help,
"i|ident=s" => \$ident,
- "j|jobs=s" => \$queue_size,
- "J!job_per_file" => \$job_per_file,
+ "j|jobs=i" => \$queue_size,
+ "J|job_per_file=i" => \$job_per_file,
"l|last-parsed=s" => \$last_parsed,
"m|maxlength=i" => \$maxlength,
"N|appname=s" => \@dbappname,
my @tempfiles = ();
foreach my $logfile ( @given_log_files ) {
+ last if ($abort);
if ($queue_size > 1) {
# Create multiple process to parse one log file by chunks of data
my @chunks = &split_logfile($logfile);
for (my $i = 0; $i < $#chunks; $i++) {
+ last if ($abort);
push(@tempfiles, [ tempfile('tmp_pgbadgerXXXX', SUFFIX => '.bin', TMPDIR => 1, UNLINK => 1 ) ]);
spawn sub {
&process_file($logfile, $tempfiles[-1]->[0], $chunks[$i], $chunks[$i+1]);
};
}
}
+
# Wait for all child dies
1 while wait != -1;
&logmsg('DEBUG', "Starting to parse log file: $logfile");
+ my $terminate = 0;
+ local $SIG{INT} = sub { $terminate = 1 };
+ local $SIG{TERM} = sub { $terminate = 1 };
+
my $curdate = localtime(time);
# Syslog does not have year information, so take care of year overlapping
my $getout = 0;
while (my $row = $csv->getline($lfile)) {
+ # We received a signal
+ last if ($terminate);
+
# Set progress statistics
$cursize += length(join(',', @$row));
$nlines++;
$cursize += $start_offset;
}
while (my $line = <$lfile>) {
+
+ # We received a signal
+ last if ($terminate);
+
$cursize += length($line);
chomp($line);
$line =~ s/\r//;
&dump_as_binary($tmpoutfile);
$tmpoutfile->close();
}
+
+ return 0;
}
# Save last line parsed