-
Notifications
You must be signed in to change notification settings - Fork 0
/
spamstats
executable file
·196 lines (170 loc) · 5.46 KB
/
spamstats
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/usr/bin/perl
#
# spamstats - Examine mail logs for spam/virus filter stats.
#
# Copyright (C) 2003-2006 Steven Pritchard <[email protected]>
# This program is free software; you may redistrute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# The full text of the GNU General Public License can be found at
#
# http://www.gnu.org/copyleft/gpl.html
#
# You may also request a copy by writing to
#
# Free Software Foundation, Inc.
# 59 Temple Place, Suite 330
# Boston, MA 02111-1307 USA
#
# $Id: spamstats,v 1.11 2006/11/20 16:51:25 steve Exp $
use strict;
use warnings;
use FileHandle;
use List::Util qw(sum);
my $start="";
my %ids;
my $clean=0;
my %spam=('green' => 0,
'blue' => 0,
'yellow' => 0,
'orange' => 0,
'red' => 0);
my %dropped=('orange' => 0, 'red' => 0);
my $executable=0;
my $zip=0;
my $virus=0;
my %dnsbl;
my %blocked;
my %virii;
my $total=0;
my @files;
if (@ARGV) {
@files=@ARGV;
} else {
@files=reverse
grep { -f } ("/var/log/maillog", map { "/var/log/maillog.$_" } (1..4));
}
for my $file (@files) {
my $fh=new FileHandle "<$file" or warn "Can't open $file: $!\n";
next if (!$fh);
while (<$fh>) {
if (!$start) {
$start=substr $_, 0, 15;
}
my ($process, $id)=(split)[4,5];
if ($process=~/^postfix\/([-\w]+)\[\d+\]:$/) {
my $service=$1;
if ($service eq "smtp") {
if (/\(250 I got it\. Seems virus-free\.\)$/) {
$clean++;
} elsif (/\(250 I WILL DROP THIS MESSAGE\.\)$/) {
$virus++;
} elsif (/\(25\d 2\.7\.\d Ok, discarded, (?:id=[-\d]* - )?(SPAM|UBE|.*VIRUS: .*\))/) {
my $reason=$1;
if ($reason=~/VIRUS:\s+(.*)\)$/) {
$virii{$1}++;
$virus++;
} else {
$blocked{$reason}++;
}
}
} elsif ($service eq "smtpd") {
if (/ blocked using ([-\w\.]+)[,;]/) {
$dnsbl{$1}++;
} elsif (/: client=[-\w\.]+(\[[\d\.]+\])$/) {
$ids{$id}++ if ($1 ne "[127.0.0.1]");
}
} elsif ($service eq "cleanup") {
if (/ ([a-z]+) spam dropped$/) {
$dropped{$1}++;
} elsif (/No executable attachments allowed here\.$/) {
$executable++;
} elsif (/Zip attachments not allowed here\.$/) {
$zip++;
} elsif (/reject: header (?i)(X-Spam-Flag): YES/) {
$blocked{$1}++;
} elsif (/Rejected - (?:see|U[BC]E)/) {
$blocked{'Local rules'}++;
} elsif (/We didn't send you a virus so don't send us your spam\.$/) {
$blocked{'virus filter spam'}++;
}
}
} elsif ($process=~/^assassind\[/) {
if (/Message has spam score of \d+\.\d+ \(([a-z]+)\)\s*$/) {
$spam{$1}++;
}
}
}
}
my $messages=scalar(keys(%ids));
$messages+=sum(values %dnsbl);
if ($messages == 0) {
print STDERR "No messages.\n";
exit 0;
}
print "Stats beginning $start\n", "-" x 31, "\n";
if (grep { $spam{$_} > 0 } keys(%spam)) {
print <<END;
Spam scores
-----------
END
for my $color (qw(green blue yellow orange red)) {
printf "%7s: %9d (%2.f%%)\n",
$color, $spam{$color}, ($spam{$color}/$messages)*100;
}
}
print <<END;
Spam dropped
------------
END
#printf "\nDropped %d orange spam and %d red spam (%d total)\n",
# $dropped{'orange'}, $dropped{'red'}, $dropped{'orange'}+$dropped{'red'};
for my $site (sort { $dnsbl{$b} <=> $dnsbl{$a} } keys %dnsbl) {
printf "%-20s %6d (%2.f%%)\n",
$site, $dnsbl{$site}, ($dnsbl{$site}/$messages)*100;
$total+=$dnsbl{$site};
}
for my $reason (sort { $blocked{$b} <=> $blocked{$a} } keys %blocked) {
printf "%-20s %6d (%2.f%%)\n",
$reason, $blocked{$reason}, ($blocked{$reason}/$messages)*100;
$total+=$blocked{$reason};
}
for my $color (qw(orange red)) {
if ($dropped{$color}) {
printf "%-20s %6d (%2.f%%)\n",
$color." score", $dropped{$color}, ($dropped{$color}/$messages)*100;
$total+=$dropped{$color};
}
}
printf "%-20s %6d (%2.f%%)\n",
"executable content", $executable, ($executable/$messages)*100;
$total+=$executable;
if ($zip) {
printf "%-20s %6d (%2.f%%)\n",
"zip attachments", $zip, ($zip/$messages)*100;
$total+=$zip;
}
printf "%-20s %6d (%2.f%%)\n",
"virus infected", $virus, ($virus/$messages)*100;
$total+=$virus;
print "-" x 28, "\n";
printf "%-20s %6d (%2.f%%)\n", "Total", $total, ($total/$messages)*100;
printf "%-20s %6d\n", "Total messages", $messages;
printf "\n%2.f%% of all mail blocked.\n", ($total/$messages)*100;
if (keys(%virii)) {
print <<END;
VIRUS SUMMARY
-------------
END
for my $name (sort { $virii{$b} <=> $virii{$a} } keys(%virii)) {
printf "%-30s %6d\n", $name, $virii{$name};
}
}
# vi: set ai et: