-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsa-stats
executable file
·342 lines (294 loc) · 8.63 KB
/
sa-stats
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/perl
# -------------------------------------------------------------
# file: sa-stats.pl (SARE release)
# created: 2005-01-31
# updated: 2007-01-30
# version: 1.03
# author: Dallas Engelken <[email protected]>
# desc: Generates Top Spam/Ham Rules fired for SA 3.1.x installations.
#
# IMPORTANT NOTES
#
# SA 3.0.x log files do not have user=<user> in
# the report: log entries, so this does not work with 3.0.
# See http://www.rulesemporium.com/programs/sa-stats.txt for
# a SA 3.0.x version ( no per-domain / per-user support )
#
# If your top 5 does not contain URIBL_BLACK, see
# http://www.uribl.com/usage.shtml
# -------------------------------------------------------------
# Per User and Per Domain Statistics...
# -------------------------------------------------------------
#
# ./sa-stats -r postmaster
# - this would give all stats for postmaster users,
# regardless of which domain it was for. handy if you
# have alot of domain aliases
#
# ./sa-stats -r @domain
# - this would give all stats for the domain specified.
# make sure you include the '@' sign before the
# domain or the script will assume you wanted a user
# name instead.
#
# ./sa-stats -r [email protected]
# - this would give all stats for a specific email address.
# this assumes you pass 'spamc -u <fullemail>' vs.
# 'spamc -u <userpart>'. If you do the latter, you simply
# want to call -r <userpart> instead.
#
# -------------------------------------------------------------
use Getopt::Long;
use Pod::Usage;
my ($LOG_DIR,$FILE,$TOPRULES,$PRINT_TO_WEB,$HELP,$RECIP);
GetOptions (
'logdir|l=s' => \$LOG_DIR,
'filename|f=s' => \$FILE,
'recip|r=s' => \$RECIP,
'num|n=i' => \$TOPRULES,
'web|w' => \$PRINT_TO_WEB,
'help|h' => \$HELP
);
if ($HELP) {
print "usage: $0 [-l <dir>] [-f <file>] [-n <num>] [-w]\n";
print "\t--logdir|-l <dir>\tDirectory containing spamd logs\n";
print "\t--filename|-f <file>\tFile names or regex to look for in the logdir\n";
print "\t--num|-n <num>\tNumber of top rules to display\n";
print "\t--web|-w\tMake it web friendly output\n";
print "\t--help|-h\tPrints this help\n";
exit;
}
if (!defined $TOPRULES) { $TOPRULES=20 }
if (!defined $LOG_DIR) { $LOG_DIR="/var/log" }
if (!defined $FILE) { $FILE='^maillog$' } # regex
# LEAVE THE REST ALONE UNLESS YOU KNOW WHAT YOU ARE DOING...
################################################################
my $NUM_EMAIL=0; my $NUM_SPAM=0; my $NUM_HAM=0;
my $EMAIL_HITS=0; my $SPAM_HITS=0; my $HAM_HITS=0;
my %SPAM_RULES=(); my %HAM_RULES=();
my $TOTAL_SPAM_RULES=0; my $TOTAL_HAM_RULES=0;
my $ALSPAM=0; my $ALHAM=0; my $ALNO=0;
my $HAM_SEC=0; my $SPAM_SEC=0; my $EMAIL_SEC=0;
my $footer = '</div><div id="footer"><p>CGI by <a href="mailto:[email protected]">Dallas Engelken</a></p></div>';
opendir (DIR,"$LOG_DIR");
my @logs = grep /$FILE/i, readdir DIR;
closedir DIR;
foreach my $log (@logs) {
&calcstats($LOG_DIR."/".$log);
}
&summarize();
exit;
#############################
sub calcstats {
my $log=shift;
if (!-e $log || -d $log) {
print "$log not found..\n";
return;
}
open(F,"$log");
while(<F>) {
my ($result,$score,$rules,$time,$size,$learn,$recip);
my $spam=0;
# for user=, it may be %domain or $GLOBAL or @GLOBAL or user@domain..
if (/.*result:\s+(\w|\.)\s+(\-?\d+)\s+\-\s+(.*)\s+scantime\=([\d\.]+)\,size\=(\d+).*user=([^\,]+).*autolearn=(\w+)/) {
$result=$1;
$score=$2;
$rules=$3;
$time=$4;
$size=$5;
$recip=$6;
$learn=$7;
}
else {
next;
}
my ($user,$domain);
if ($recip =~ m/^[\%\@](.+)/) {
$user = undef;
$domain = '@'.$1;
}
if ($recip =~ m/(.+)\@(.+)/) {
$user=$1;
$domain='@'.$2;
}
else {
$user=$recip;
$domain='@localhost';
}
my $email = $user.$domain;
next if ($RECIP && $RECIP !~ m/\@/ && $RECIP ne $user);
next if ($RECIP =~ m/^[\%\@](.+)/ && $RECIP ne $domain);
next if ($RECIP =~ m/(.+)\@(.+)/ && $RECIP ne $email);
if ($result eq "Y") {
$SPAM_SEC+=$time;
}
else {
$HAM_SEC+=$time;
}
$EMAIL_SEC+=$time;
$spam=1 if ($result =~ m/Y/);
if ($learn =~ /ham/) {
$ALHAM++;
}
elsif ($learn =~ /spam/) {
$ALSPAM++;
}
else {
$ALNO++;
}
my @tmprules=split(/\,/,$rules);
foreach my $r (@tmprules) {
if ($spam) {
$TOTAL_SPAM_RULES++;
if (defined $SPAM_RULES{$r}) {
$SPAM_RULES{$r}++;
}
else {
$SPAM_RULES{$r}=1;
}
}
else {
$TOTAL_HAM_RULES++;
if (defined $HAM_RULES{$r}) {
$HAM_RULES{$r}++;
}
else {
$HAM_RULES{$r}=1;
}
}
}
if ($spam) {
$NUM_SPAM++;
$SPAM_HITS += $score;
}
else {
$NUM_HAM++;
$HAM_HITS += $score;
}
$NUM_EMAIL++;
$EMAIL_HITS += $score;
}
close(F);
}
sub summarize {
my ($avgspamhits,$avghamhits,$avgemailhits);
print "Content-type: text/html\n\n" if ($PRINT_TO_WEB);
print "<pre>" if ($PRINT_TO_WEB);
if ($NUM_SPAM > 0) {
$avgspamhits= sprintf("%.2f",$SPAM_HITS/$NUM_SPAM);
$avgspamtime= sprintf("%.2f",$SPAM_SEC/$NUM_SPAM);
}
else {
$avgspamhits=0;
$avgspamtime=0;
}
if ($NUM_HAM > 0) {
$avghamhits= sprintf("%.2f",$HAM_HITS/$NUM_HAM);
$avghamtime= sprintf("%.2f",$HAM_SEC/$NUM_HAM);
}
else {
$avghamhits=0;
$avghamtime=0;
}
if ($NUM_EMAIL > 0) {
$avgemailhits= sprintf("%.2f",$EMAIL_HITS/$NUM_EMAIL);
$avgemailtime= sprintf("%.2f",$EMAIL_SEC/$NUM_EMAIL);
}
else {
$avgemailhits=0;
$avgemailtime=0;
}
print "\n\n";
if ($RECIP) {
print "SPAM STATS FOR $RECIP\n";
print "-" x 60 . "\n";
}
my $ALTOT=$ALSPAM+$ALHAM;
printf("Email: %8s Autolearn: %5s AvgScore: %6.2f AvgScanTime: %5.2f sec\n",$NUM_EMAIL,$ALTOT,$avgemailhits,$avgemailtime);
printf("Spam: %8s Autolearn: %5s AvgScore: %6.2f AvgScanTime: %5.2f sec\n",$NUM_SPAM,$ALSPAM,$avgspamhits,$avgspamtime);
printf("Ham: %8s Autolearn: %5s AvgScore: %6.2f AvgScanTime: %5.2f sec\n",$NUM_HAM,$ALHAM,$avghamhits,$avghamtime);
&br;
printf "Time Spent Running SA: %7.2f hours\n",$EMAIL_SEC/60/60;
printf "Time Spent Processing Spam: %7.2f hours\n",$SPAM_SEC/60/60;
printf "Time Spent Processing Ham: %7.2f hours\n",$HAM_SEC/60/60;
&br;
my $count=0;
print "TOP SPAM RULES FIRED";
print " FOR $RECIP" if ($RECIP);
print "\n";
&hr;
printf("%4s\t%-24s\t%5s %8s %7s %7s %7s\n","RANK","RULE NAME","COUNT","\%OFMAIL","\%OFSPAM","\%OFHAM");
&hr;
foreach my $key (sort { $SPAM_RULES{$b} <=> $SPAM_RULES{$a} } keys %SPAM_RULES) {
my $perc1=sprintf("%.2f", $NUM_EMAIL);
my $perc2=sprintf("%.2f", $NUM_SPAM);
my $perc3=sprintf("%.2f", $NUM_HAM);
if ($NUM_EMAIL > 0) {
#$perc1=sprintf("%.2f",($SPAM_RULES{$key}/$NUM_EMAIL)*100);
$perc1=sprintf("%.2f",(($SPAM_RULES{$key}+$HAM_RULES{$key})/$NUM_EMAIL)*100);
}
if ($NUM_SPAM > 0) {
$perc2=sprintf("%.2f",($SPAM_RULES{$key}/$NUM_SPAM)*100);
}
if ($NUM_HAM > 0) {
$perc3=sprintf("%.2f",($HAM_RULES{$key}/$NUM_HAM)*100);
}
printf("%4d\t%-24s\t%5s\t%6.2f\t%6.2f\t%6.2f\n",$count+1,$key,$SPAM_RULES{$key},$perc1,$perc2,$perc3);
$count++;
if ($count >= $TOPRULES && $TOPRULES > 0) {
last;
}
}
&hr;
&br;
$count=0; # thanks mike.
print "TOP HAM RULES FIRED";
print " FOR $RECIP" if ($RECIP);
print "\n";
&hr;
printf("%4s\t%-24s\t%5s %8s %7s %7s %7s\n","RANK","RULE NAME","COUNT","\%OFMAIL","\%OFSPAM","\%OFHAM");
&hr;
foreach my $key (sort { $HAM_RULES{$b} <=> $HAM_RULES{$a} } keys %HAM_RULES) {
my $perc1=sprintf("%.2f", $NUM_EMAIL);
my $perc2=sprintf("%.2f", $NUM_SPAM);
my $perc3=sprintf("%.2f", $NUM_HAM);
if ($NUM_EMAIL > 0) {
# $perc1=sprintf("%.2f",($HAM_RULES{$key}/$NUM_EMAIL)*100);
$perc1=sprintf("%.2f",(($SPAM_RULES{$key}+$HAM_RULES{$key})/$NUM_EMAIL)*100);
}
if ($NUM_SPAM > 0) {
$perc2=sprintf("%.2f",($SPAM_RULES{$key}/$NUM_SPAM)*100);
}
if ($NUM_HAM > 0) {
$perc3=sprintf("%.2f",($HAM_RULES{$key}/$NUM_HAM)*100);
}
printf("%4d\t%-24s\t%5s\t%6.2f\t%6.2f\t%6.2f\n",$count+1,$key,$HAM_RULES{$key},$perc1,$perc2,$perc3);
$count++;
if ($count >= $TOPRULES && $TOPRULES > 0) {
last;
}
}
&hr;
&br;
print "</pre>\n" if ($PRINT_TO_WEB);
print $footer if ($PRINT_TO_WEB && $footer ne "");
print "\n";
}
#######################
sub hr {
if ($PRINT_TO_WEB) {
print "<hr size=1 width=50% align=left>";
}
else {
print "-" x 70 ."\n";
}
}
#######################
sub br {
if ($PRINT_TO_WEB) {
print "<br>";
}
else {
print "\n";
}
}