#!/bin/bash
# Shell Script utility to read the maildirsize file.
# If the mailbox of some user inside the MAILROOT-Directory is over the defined MAXPERCENTAGE
# a mail is sent to this user
# -------------------------------------------------------------------------
# User define Function (UDF)
processLine(){
line="$@" # get all args
if [ $COUNTER -eq 0 ]
then
HARDQUOTA=$(echo $line | cut -d "S" -f 1)
else
ACTUALSIZE=$(echo $line | awk '{ print $1 }')
MBOXSPACE=$(( $MBOXSPACE + $ACTUALSIZE ))
fi
}
### Main script starts here ###
###
# Counter
COUNTER=0
# Maximum Percentage of Quota usage
MAXPERCENTAGE=80
###
#
MAILROOT=/srv/email/mailnames
cd $MAILROOT > /dev/null
for DIR in *.*; do
cd $MAILROOT/$DIR > /dev/null
for MAILBOX in *; do
if [ -d $MAILROOT/$DIR/$MAILBOX ]; then
FILE=$MAILROOT/$DIR/$MAILBOX/Maildir/maildirsize
fi
if [ -f $FILE ]; then
# Set loop separator to end of line
while read -r line
do
# use $line variable to process line in processLine() function
processLine $line
(( COUNTER ++ ))
done < $FILE
#reset COUNTER to 0 for the next file
COUNTER=0
if [ $HARDQUOTA -gt 0 ]; then
SOFTQUOTA=$(( $MAXPERCENTAGE*$HARDQUOTA/100 ))
MBOXPERCENT=$(( $MBOXSPACE*100/$HARDQUOTA ))
fi
#echo "Quota is $HARDQUOTA, used $MBOXSPACE, softquota is $SOFTQUOTA, in Prozent $MBOXPERCENT"
if [ $HARDQUOTA -gt 0 -a $MBOXSPACE -gt $SOFTQUOTA ]; then
echo "$MAILBOX@$DIR belegt $MBOXSPACE bytes, Soft/Hardquota = $SOFTQUOTA bytes/$HARDQUOTA bytes, in Prozent $MBOXPERCENT"
#mail -r do-not-reply@$DIR -s "Mailbox Quota Warnung"
[email protected] << EOF
mail -r do-not-reply@$DIR -s "Mailbox Quota Warung" $MAILBOX@$DIR << EOF
Liebe(r) $MAILBOX
Deine Mailbox nutzt im Moment $MBOXSPACE b ($MBOXPERCENT %) auf dem Mailserver. Wir wuerden dich gerne erinnern
dass deine Mailbox nur $HARDQUOTA b beinhalten darf.
Um Probleme zu vermeiden, entfernen Sie bitte Mails in Ihrem Posteingang, Gesendeten Objekten und allen Unterordnern
Liebe Gruesse,
mailbox-robot
EOF
fi
#reset all variables to 0 in order to process the next file
HARDQUOTA=0
MBOXSPACE=0
SOFTQUOTA=0
fi
done
done
exit 0