/[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.7 - (hide annotations)
Fri Jul 26 10:10:02 2002 UTC (16 years, 8 months ago) by dpavlin
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +1 -0 lines
File MIME type: text/plain
better instruction on top of file

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

  ViewVC Help
Powered by ViewVC 1.1.26