1 |
#!/usr/bin/perl -wT |
#!/usr/bin/perl -w |
2 |
|
|
3 |
# this script can be run periodicly on log mail.log file to report |
# this script can be run periodicly on log mail.log file to report |
4 |
# change since last run. |
# change since last run. |
5 |
# |
# |
6 |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
# Dobrica Pavlinusic <dpavlin@rot13.org> |
7 |
|
# https://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; |
use strict; |
23 |
|
|
24 |
my $log="/var/adm/syslog.dated/current/mail.log"; |
my $log="/var/log/mail.log"; |
25 |
|
|
26 |
# edit this to your configuration! |
# edit this to your configuration! |
27 |
|
|
28 |
my $domain='@pliva.hr'; |
my $domain='@pliva.hr'; |
29 |
my $delta="/var/tmp/"; |
my $delta="/var/tmp/"; |
30 |
|
|
31 |
my $debug=1; |
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 |
# counters |
41 |
my ($mail_int, $mail_ext) = (0,0,0,0); |
my ($mail_int, $mail_ext) = (0,0,0,0); |
42 |
# size |
# size |
43 |
my ($size_int, $size_ext) = (0,0,0,0); |
my ($size_int, $size_ext) = (0,0,0,0); |
44 |
|
|
45 |
open(LOG,$log) || die "can't open log"; |
open(LOG,$log) || die "can't open log '$log': $!"; |
46 |
|
|
47 |
my $tmp_log=$log; |
my $tmp_log=$log; |
48 |
$tmp_log=~s/\W/_/g; |
$tmp_log=~s/\W/_/g; |
49 |
$delta.=$tmp_log.".offset"; |
$delta.=$tmp_log.".offset"; |
50 |
|
|
51 |
if (-e $delta) { |
if (-e $delta) { |
52 |
open(D,$delta) || die "can't open delta file '$delta' for log '$log'"; |
open(D,$delta) || die "can't open delta file '$delta' for log '$log': $!"; |
53 |
my $offset=<D>; |
my $offset=<D>; |
54 |
chomp $offset; |
chomp $offset; |
55 |
close(D); |
close(D); |
56 |
my $log_size = -s $log; |
my $log_size = -s $log; |
57 |
print "log size: $log_size\n" if ($debug); |
print STDERR "log size: $log_size\n" if ($debug); |
58 |
if ($offset <= $log_size) { |
if ($offset <= $log_size) { |
59 |
seek(LOG,$offset,0); |
seek(LOG,$offset,0); |
60 |
print STDERR "skipping to position: $offset\n" if ($debug); |
print STDERR "skipping to position: $offset\n" if ($debug); |
74 |
$mail_ext++; |
$mail_ext++; |
75 |
$size_ext+=$1; |
$size_ext+=$1; |
76 |
} |
} |
77 |
|
foreach my $patt (@count_patt) { |
78 |
|
if (m/$patt/i) { |
79 |
|
$count{$patt}++; |
80 |
|
} |
81 |
|
} |
82 |
$lines++; |
$lines++; |
83 |
} |
} |
84 |
|
|
85 |
print STDERR "processed $lines lines...\n" if ($debug); |
print STDERR "processed $lines lines...\n" if ($debug); |
86 |
|
|
87 |
open(D,"> $delta") || die "can't open delta file '$delta' for log '$log'"; |
if (! $skip_delta) { |
88 |
print D tell(LOG); |
open(D,"> $delta") || die "can't open delta file '$delta' for log '$log': $!"; |
89 |
close(D); |
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); |
print STDERR "last position in log: ".tell(LOG)."\n" if ($debug); |
96 |
|
|
97 |
print "$mail_int |
print "$mail_int\n$size_int\n$mail_ext\n$size_ext\n"; |
98 |
$size_int |
|
99 |
$mail_ext |
foreach my $patt (@count_patt) { |
100 |
$size_ext |
print $count{$patt} || 0,"\n"; |
101 |
"; |
} |