1 |
#!/usr/bin/perl -w |
2 |
|
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 |
# |
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 |
|
21 |
use strict; |
22 |
|
23 |
my $log="/var/log/mail.log"; |
24 |
|
25 |
# edit this to your configuration! |
26 |
|
27 |
my $domain='@pliva.hr'; |
28 |
my $delta="/var/tmp/"; |
29 |
|
30 |
my $debug=1; |
31 |
my $skip_delta=0; |
32 |
|
33 |
$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 |
|
39 |
# 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 |
open(LOG,$log) || die "can't open log '$log': $!"; |
45 |
|
46 |
my $tmp_log=$log; |
47 |
$tmp_log=~s/\W/_/g; |
48 |
$delta.=$tmp_log.".offset"; |
49 |
|
50 |
if (-e $delta) { |
51 |
open(D,$delta) || die "can't open delta file '$delta' for log '$log': $!"; |
52 |
my $offset=<D>; |
53 |
chomp $offset; |
54 |
close(D); |
55 |
my $log_size = -s $log; |
56 |
print STDERR "log size: $log_size\n" if ($debug); |
57 |
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 |
foreach my $patt (@count_patt) { |
77 |
if (m/$patt/i) { |
78 |
$count{$patt}++; |
79 |
} |
80 |
} |
81 |
$lines++; |
82 |
} |
83 |
|
84 |
print STDERR "processed $lines lines...\n" if ($debug); |
85 |
|
86 |
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 |
|
94 |
print STDERR "last position in log: ".tell(LOG)."\n" if ($debug); |
95 |
|
96 |
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 |
} |