make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / asterisk-log-separator
blob833a559ff14b7ce47cc4526f86ddc77d57004ae0
1 #!/usr/bin/env perl
3 use Data::Dumper;
4 use POSIX qw/mktime/;
5 use constant { STAT_MTIME => 9, };
6 use Getopt::Long;
8 $n = 1;
9 for my $monthname (qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/)
11 $month{$monthname} = $n;
12 $n++;
16 GetOptions(
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/)
31 if(defined $$var)
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;
46 while(<$fh>)
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})
67 die "$!\n";
69 delete $outfiles{$k};
70 last;
74 my $fn = "$pid.astlog";
75 my $mode = $datetime{$pid} ? '>>' : '>';
76 if(not open $outfiles{$pid}, $mode, $fn)
78 die "$fn: $!\n";
80 print "$pid, ";
82 print {$outfiles{$pid}} $_ or warn "$!\n";
86 close $fh;
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;
96 # delete small files:
97 # find -type f -size -200c -delete
100 __END__
102 =pod
104 =head1 NAME
106 asterisk-log-separator - Split up Asterisk PBX log file into multiple files based on which process wrote each part
108 =cut