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