sort correctly by date number, ignore some lines
[mx01] / klin / from-by-time.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use Data::Dump qw(dump);
6 my $stat;
7 my $l = 0;
8
9 while(<>) {
10         chomp;
11         my ( $mon, $dd, $time, $host, $daemon, $msg ) = split(/\s+/,$_,6);
12
13         next unless $msg =~ m/from=<([^>]+)>/;
14         next if $msg =~ m/(NOQUEUE|FILTER)/;
15         my $from = $1;
16
17         my $t = $time;
18         $t =~ s/\d:\d\d$/_/; # group by 10 min
19
20         $stat->{$mon}->{$dd}->{$t}->{$from}++;
21
22         $l++;
23         print STDERR "$l " if $l % 10000 == 0;
24 }
25
26 #warn "# stat = ", dump( $stat );
27
28 foreach my $mon ( sort keys %$stat ) {
29         foreach my $dd ( sort { $a <=> $b } keys %{$stat->{$mon}} ) {
30                 foreach my $t ( sort keys %{ $stat->{$mon}->{$dd} } ) {
31                         my $n = 0;
32                         my $e = $stat->{$mon}->{$dd}->{$t};
33                         my $fmt = "%3s %2s %5s %4d %s\n";
34                         foreach my $email ( sort { $e->{$b} <=> $e->{$a} } keys %$e ) {
35                                 if ( $n == 0 ) {
36                                         printf $fmt, $mon, $dd, $t, $e->{$email}, $email;
37                                 } elsif ( $n == 5 ) { # XXX top
38                                         last;
39                                 } else {
40                                         printf $fmt, '', '', '', $e->{$email}, $email;
41                                 }
42                                 $stat->{_from}->{$email} += $e->{$email};
43                                 $n++;
44                         }
45                 }
46         }
47 }
48 #print "_from = ",dump( $stat->{_from} );
49 print "# XXX count,email\n";
50 foreach my $email ( sort { $stat->{_from}->{$b} <=> $stat->{_from}->{$a} } keys %{ $stat->{_from} } ) {
51         printf "%4d %s\n", $stat->{_from}->{$email}, $email;
52 }