/[cricket]/parse_maillog.pl
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /parse_maillog.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.5 - (hide annotations)
Fri Jul 19 13:28:07 2002 UTC (19 years, 3 months ago) by dpavlin
Branch: MAIN
Changes since 1.4: +39 -12 lines
File MIME type: text/plain
You can now add optional regexps to count in mail.log (eq. pop,imap and
you will get numeber of imap and pop connections)

1 dpavlin 1.4 #!/usr/bin/perl -w
2 dpavlin 1.1
3     # this script can be run periodicly on log mail.log file to report
4     # change since last run.
5     #
6     # Dobrica Pavlinusic <dpavlin@rot13.org>
7 dpavlin 1.5 #
8     # Usage: parse_maillog.pl /path/to/mail.log [count_regex,count_regex,...]
9     #
10     # Results for optional count regexps will be dumped to stdout after data
11     # for number of internal mails, size of internal mails, number of
12     # external mails and size of external mails
13     #
14     # So, if you want also to count number of pop and imap accesses from your
15     # mail.log you can use:
16     #
17     # parse_maillog.pl /var/log/mail.log popd,imapd
18     #
19     # and modify your target file accordingly
20 dpavlin 1.1
21     use strict;
22    
23 dpavlin 1.2 my $log="/var/log/mail.log";
24 dpavlin 1.1
25     # edit this to your configuration!
26    
27     my $domain='@pliva.hr';
28     my $delta="/var/tmp/";
29    
30 dpavlin 1.5 my $debug=1;
31     my $skip_delta=0;
32 dpavlin 1.1
33 dpavlin 1.5 $log = shift @ARGV if ($ARGV[0] && -r $ARGV[0]);
34    
35     # take patterns to count (separated by ,)
36     my @count_patt = split(/,/,shift @ARGV) if (@ARGV);
37     my %count;
38 dpavlin 1.3
39 dpavlin 1.1 # counters
40     my ($mail_int, $mail_ext) = (0,0,0,0);
41     # size
42     my ($size_int, $size_ext) = (0,0,0,0);
43    
44 dpavlin 1.5 open(LOG,$log) || die "can't open log '$log': $!";
45 dpavlin 1.1
46     my $tmp_log=$log;
47     $tmp_log=~s/\W/_/g;
48     $delta.=$tmp_log.".offset";
49    
50     if (-e $delta) {
51 dpavlin 1.2 open(D,$delta) || die "can't open delta file '$delta' for log '$log': $!";
52 dpavlin 1.1 my $offset=<D>;
53     chomp $offset;
54     close(D);
55     my $log_size = -s $log;
56 dpavlin 1.5 print STDERR "log size: $log_size\n" if ($debug);
57 dpavlin 1.1 if ($offset <= $log_size) {
58     seek(LOG,$offset,0);
59     print STDERR "skipping to position: $offset\n" if ($debug);
60     } else {
61     print STDERR "reset position to begin\n" if ($debug);
62     }
63     }
64    
65     my $lines=0;
66    
67     while(<LOG>) {
68     chomp;
69     if (m/from=[^\@]+\@[^\@]+$domain[>,]+\s+.*size=(\d+)/ || m/from=[^\@]+[,\s]+.*size=(\d+)/ ) {
70     $mail_int++;
71     $size_int+=$1;
72     } elsif (m/from=.*size=(\d+)/) {
73     $mail_ext++;
74     $size_ext+=$1;
75     }
76 dpavlin 1.5 foreach my $patt (@count_patt) {
77     if (m/$patt/i) {
78     $count{$patt}++;
79     }
80     }
81 dpavlin 1.1 $lines++;
82     }
83    
84     print STDERR "processed $lines lines...\n" if ($debug);
85    
86 dpavlin 1.5 if (! $skip_delta) {
87     open(D,"> $delta") || die "can't open delta file '$delta' for log '$log': $!";
88     print D tell(LOG);
89     close(D);
90     } else {
91     print STDERR "new delta not written to file!\n";
92     }
93 dpavlin 1.1
94     print STDERR "last position in log: ".tell(LOG)."\n" if ($debug);
95    
96 dpavlin 1.5 print "$mail_int\n$size_int\n$mail_ext\n$size_ext\n";
97    
98     foreach my $patt (@count_patt) {
99     print $count{$patt} || 0,"\n";
100     }

  ViewVC Help
Powered by ViewVC 1.1.26