#!/bin/bash
#
# Troutman Mar 11, 2004
# http://www.troutman.org/
#
# mailstats produces a web page of mail statistics images
#
# Copy this file to /usr/bin/mailstats and chmod 500 as a user able to read /var/log/
# Run from cron hourly, edit crontab and add "59 * * * * /usr/bin/mailstats"
# This script assumes you are running Spamhaus DNS rules which log a Spamhaus message every time
# a spam message is killed in 'maillog'. If this is not the case, you will need to adjust.
# You must have a functioning copy of gnuplot installed in /usr/local/bin/gnuplot, or adjust.
# Location variables
org="Organization X" # Organization name
head="$org Mail Statistics" # Title of Web Page
wdir="/var/www/html" # Working Directory - no trailing /
log="/var/log/maillog" # Syslog Mail log
wpage="$wdir/spam.html" # Page destination
statlog="$wdir/mailstats.log" # Culled statistics log
# Get stats
date=`date +"%D %R"`; # Date in standard format
maildate=`date +"%b %e %H:"`; # Date in maillog format
csmtp=`grep -c "relay=" $log`; # Number of cumulative smtp transactions
cspam=`grep -c Spamhaus $log`; # Number of cumulative logged spams
smtp=`grep "$maildate" $log | grep -c "relay="` # Number of smtp transactions last hour
spam=`grep "$maildate" $log | grep -c Spamhaus`; # Number of logged spams last hour
relayd=`grep "$maildate" $log | grep -c "Relaying denied"` # Relaying Denied Last Hour
pop3=`grep "$maildate" $log | grep -c "pop3 service init"` # POP3 Checks This Hour
imap=`grep "$maildate" $log | grep -c "imap service init"` # IMAP Checks This Hour
# Set up main log with mail statistics
# Column Guide
# 7 - Cumulative SMTP for the week
# 8 - Cumulative Spam for the week
# 9 - SMTP Transactions This Hour
# 10 - Open Relay Denied This Hour
# 11 - POP3 Checks This Hour
# 12 - IMAP Checks This Hour
# 13 - SPAM Transactions This Hour
date +"%m %d %H %M %S %Y $csmtp $cspam $smtp $relayd $pop3 $imap $spam" >> $statlog
# Tail the last day, week, month and year of data for analysis
/usr/bin/tail -24 $statlog > $wdir/spamt.log # 24 Hours
/usr/bin/tail -168 $statlog > $wdir/spamw.log # 7 Days
/usr/bin/tail -720 $statlog > $wdir/spamm.log # 30 Days
/usr/bin/tail -8760 $statlog > $wdir/spamy.log # 365 Days
# Use gnuplot to generate graphs
# 1:7 indicates the columns you are plotting. linespoints are lines connecting points
# impulses are vertical bars. See gnuplot documentation to customize your own results.
#
# Sample smtph.cfg gnuplot config follows
# ----------------
# set term png color
# set output "/var/www/html/smtpt.png"
# set key below
# set grid
# set size 1.5,0.75
# set pointsize .25
# set xdata time
# set autoscale
# set ylabel "SMTP vs SPAM Messages Handled"
# set yrange [0:]
# set bmargin 4
# set tmargin 4
# set timefmt "%m %d %H %M %S %Y"
# plot "/var/www/html/spamt.log" using 1:8 title 'SPAM' w linespoints, \
# "/var/www/html/spamt.log" using 1:7 title 'SMTP' w linespoints
# ----------------
gp="/usr/local/bin/gnuplot /etc/gnuplot";
$gp/spamr.cfg # Ratio of spam trapped to smtp total
$gp/smtph.cfg # Number of smtp transactions per hour
$gp/spamh.cfg # Number of spam transactions per hour
$gp/relaydh.cfg # Number of open relay blocks
$gp/pop3h.cfg # Number of pop3 checks
$gp/imaph.cfg # Number of imap checks
$gp/spamt.cfg # Today's spam vs smtp
$gp/spamw.cfg # This week's spam vs. smtp
$gp/spamm.cfg # This month's spam vs. smtp
$gp/spamy.cfg # This year's spam vs. smtp
# Set up web page heading
echo "
$head$head
This week, as of $date" > $wpage
echo "
SMTP Messages handled : $csmtp" >> $wpage
echo "
Spamhaus blocks : $cspam" >> $wpage
# Image tables
echo "
| Percentage of known spam blocked by Spamhaus |
 |
" >> $wpage
echo "| Hourly SMTP message rate : $smtp |
 |
" >> $wpage
echo "| Hourly SPAM message rate : $spam |
 |
" >> $wpage
echo "| Hourly Relay Denied rate : $relayd |
 |
" >> $wpage
echo "| Hourly POP3 Check rate : $pop3 |
 |
" >> $wpage
echo "| Hourly IMAP Check rate : $imap |
 |
" >> $wpage
echo "| Today SMTP Connections vs. Blocked SPAM |
 |
" >> $wpage
echo "| This Week SMTP Connections vs. Blocked SPAM |
 |
" >> $wpage
echo "| This Month SMTP Connections vs. Blocked SPAM |
 |
" >> $wpage
echo "| This Year SMTP Connections vs. Blocked SPAM |
 |
" >> $wpage
echo "
$org Proprietary and Confidential" >> $wpage
echo "" >> $wpage
# EOF