Exim Total Mail

#!/bin/bash

## This script is intented to be run late at night to total the days mail for the hosted

## domains and store it to $WORKDIR

EXIMVIRTUALDIR=/etc/exim/virtual

EXIMLOGFILE=/var/log/exim/mainlog

#EXIMLOGFILE=$1

EXIMPREFIXFILE=/etc/exim/eximuserprefix

PASSWDFILE=/etc/passwd

WORKDIR=/usr/local/share/mailtotal

TODAY=`date +%F`

#TODAY=$2

YEARMONTH=`echo $TODAY | cut -c-7`

MONTHSTATFILE=$WORKDIR/mailtotal$YEARMONTH

MAILGROUP=12

## Calculate the totals for all the hosted domains

for domainname in `ls $EXIMVIRTUALDIR` ; do

## Get the domain prefix

userprefix=`grep $domainname $EXIMPREFIXFILE | cut -d: -f2`_

for mailuser in `grep :$MAILGROUP:: $PASSWDFILE | grep $userprefix | cut -d: -f1`; do

## Get the previous total

tmptotal=`grep $mailuser $MONTHSTATFILE | cut -d: -f2`

if [ "$tmptotal" = "" ]; then

tmptotal="0"

fi

## Build a list of the mail sizes

for messagesize in `exigrep $mailuser $EXIMLOGFILE | grep $TODAY | grep S= | cut -d' ' -f9 | grep -v P= | cut -d= -f2` ; do

counter=$tmptotal\ +\ $messagesize

tmptotal=$counter

done

if [ "$tmptotal" = "" ] ;then

tmptotal="0"

fi

## Add the list of mail sizes up

totalbandwidth=`expr $tmptotal`

## Write it to a temp file

echo $mailuser:$totalbandwidth >> $MONTHSTATFILE.tmp

#echo $mailuser:$tmptotal

done

done

## Overwrite the old file with the new one

mv -f $MONTHSTATFILE.tmp $MONTHSTATFILE