AFDB logo
mn :: comp :: tools :: Monitoring spam

Monitoring spam

Some scripts for monitoring your SpamAssassin install with MRTG. There are a ton of others, this is just mine. Example (live) output can be viewed here. Compare Postfix stats with SpamAssassin stats -- not all mail is sent to SpamAssassin because SpamAssassin is configured on a per-user basis.

The first one is spam-stats. This is a bash script that greps the log file. It is utter junk, so if you need high performance, ask me to make the effort to fix it. It works for my needs, but it will be a bottleneck if you handle large amounts of mail. Download this and stick it in /usr/local/sbin.

The second one is spamstats.pl. This is a perl script that parses the first scripts output and gives it to MRTG. It needs to use some storage space, I put mine in /var/lib/pfixmrtg. (I based my modifications to these scripts on pfixmrtg, which is the tool I'm using to generate the Postfix statistics.)

Edit your mrtg.cfg. For instance:

Target[spam]: `/usr/local/sbin/spamstats.pl localhost spam` 
Directory[spam]: felix
MaxBytes[spam]: 100000 
Options[spam]: gauge, nopercent, growright
Title[spam]: Spam Statistics 
PageTop[spam]: SA-identified spam, SA-processed email
WithPeak[spam]: dwmy 
YLegend[spam]: No. of messages 
ShortLegend[spam]: messages 
LegendI[spam]:  spam: 
LegendO[spam]:  total: 

# show percentage of spam messages 
Target[spam_percent_spam]: `/usr/local/sbin/spamstats.pl localhost spam` 
Directory[spam_percent_spam]: felix
MaxBytes[spam_percent_spam]: 100000 
Options[spam_percent_spam]: growright,gauge,nopercent,dorelpercent,noi,noo 
Title[spam_percent_spam]: Spam Statistics 
PageTop[spam_percent_spam]: Percent of e-mails identified as spam
WithPeak[spam_percent_spam]: dwmy 
YLegend[spam_percent_spam]: percent of messages 
ShortLegend[spam_percent_spam]: messages 
LegendI[spam_percent_spam]:  Spam: 
LegendO[spam_percent_spam]:  Clean: 

Then run indexmaker to pick up the changes. That's all there is to it.

How it works

The first script is just a tool which goes through the mail log file and figures out how many clean mails, spammy mails, and total mails handled are in it. The second script calls that tool, then compares the numbers found against the numbers in a temp file which was saved after the last run. The differences are passed to MRTG, and the current values are saved in the temp file for comparison with the next measurement. An example temp file:

[jack@felix jack]$ cat /var/lib/pfixmrtg/spamstat.old 
clean=3216
spam=299
total=3515

Because the script doesn't care about the actual numbers, just the differences between them, log rotation has a minimal effect (one measurement is effectively zeroed out after the rotation).

Last modified: Oct 24, 2008 2:28 pm.
Contact me.

Powered by Zope