-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-mailstats.pl
executable file
·71 lines (60 loc) · 1.4 KB
/
update-mailstats.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl
#
# $jwk: update-mailstats.pl,v 1.6 2007/05/30 01:28:33 jwk Exp $
#
# Copyright 2006-2007 Joel Knight
# Copyright Craig Sanders 1999
#
# this script is licensed under the terms of the GNU GPL.
#
#
# [2006.11.12]
use DB_File;
use File::Tail;
$debug = 0;
$mail_log = '/var/log/messages';
$stats_file = '/tmp/postfix-stats.db';
$db = tie(%stats, "DB_File", "$stats_file", O_CREAT|O_RDWR, 0644, $DB_HASH)
|| die ("Cannot open $stats_file");
my $logref = tie(*LOG, "File::Tail", ( name=>$mail_log, debug=>$debug ));
# taken from perlmonks.com - http://www.perlmonks.org/index.pl?node_id=131513
close STDIN;
close STDOUT;
close STDERR;
if (open(DEVTTY, "/dev/tty")) {
ioctl DEVTTY, 0x20007471, 0;
close DEVTTY;
}
open STDIN, "</dev/null";
open STDOUT, ">/dev/null";
open STDERR, ">&STDOUT";
fork && exit;
foreach (keys %stats) {
$stats{$_} = 0;
}
$db->sync;
while (<LOG>) {
if (/status=sent/) {
next unless (/ postfix\//);
if (/relay=([^,]+)/o) {
$relay = $1;
}
if ($relay !~ /\[/o ) {
$stats{"sent:$relay"} += 1;
} else {
$stats{"sent:smtp"} += 1;
}
} elsif (/status=bounced.+said: (\d)\d\d/) {
$stats{"smtp:$1xx"} += 1;
} elsif (/smtpd.*client=/) {
$stats{"recv:smtp"} += 1;
} elsif (/pickup.*(sender|uid)=/) {
$stats{"recv:local"} += 1;
} elsif (/NOQUEUE: reject.+\]: (\d)\d\d/) {
$stats{"smtpd:$1xx"} += 1;
}
$db->sync;
}
$db->sync;
untie $logref;
untie %stats;