5 use constant
{ STAT_MTIME
=> 9, };
9 for my $monthname (qw
/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/)
11 $month{$monthname} = $n;
17 "after=s" => \
$datetime_after,
18 "before=s" => \
$datetime_before,
20 or die "Usage: $0 [--after YYYY-mm-dd_HH:MM:SS] [--before YYYY-mm-dd_HH:MM:SS]\n";
24 $infile = $ARGV[0] || "/var/log/asterisk/messages";
25 $maxfiles = `ulimit -n` || 100;
26 $mtime = (stat $infile)[STAT_MTIME
];
27 $year = 1900 + (localtime $mtime)[5];
29 for my $var (qw
/datetime_after datetime_before/)
33 if($$var =~ /^(\d{4})-(\d\d)-(\d\d)(?:[T_\s](\d\d):(\d\d)(?::(\d\d))?)?$/)
35 $$var = mktime
($6 || 0, $5 || 0, $4 || 0, $3, $2 - 1, $1 - 1900);
37 elsif($$var !~ /^\d+$/)
39 die "Wrong datetime format: $$var\n";
45 open my $fh, '<', $infile;
48 if(/^\[(\S+)\s+(\d+)\s+(\d+):(\d+):(\d+)\].*?\[(\d+)\]/)
50 my ($monthname, $day, $hour, $min, $sec, $pid) = ($1, $2, $3, $4, $5, $6);
51 my $timestamp = mktime
($sec, $min, $hour, $day, $month{$monthname} - 1, $year - 1900);
52 if((!defined $datetime_before or $timestamp <= $datetime_before) and (!defined $datetime_after or $timestamp >= $datetime_after))
54 if(not exists $datetime{$pid})
56 $datetime{$pid} = $timestamp;
59 if(not defined $outfiles{$pid})
61 if(scalar(keys %outfiles) > $maxfiles - 3)
63 for my $k (keys %outfiles)
65 if(not close $outfiles{$k})
74 my $fn = "$pid.astlog";
75 my $mode = $datetime{$pid} ?
'>>' : '>';
76 if(not open $outfiles{$pid}, $mode, $fn)
82 print {$outfiles{$pid}} $_ or warn "$!\n";
88 for my $pid (keys %datetime)
90 close $outfiles{$pid};
91 my $time = $datetime{$pid};
92 my $fn = "$pid.astlog";
93 utime $time, $time, $fn;
97 # find -type f -size -200c -delete
106 asterisk-log-separator - Split up Asterisk PBX log file into multiple files based on which process wrote each part