Quota Warning Script

From Zimbra :: Wiki

Jump to: navigation, search
   Article-alert.png  - This article is a community contribution and may include unsupported customizations for network edition customers.


Admin Article

Article Information

This article applies to the following ZCS versions.
  ZCS 4.5 Article  ZCS 4.5

Also see http://wiki.zimbra.com/index.php?title=Quota_Warnings v5 will have quota warnings built-in

This bash script will send emails to users who are nearing their maximum quota. Can be added to the zimbra user's crontab

#!/bin/bash

warn_percentage=85

tmpFile=`mktemp`

zmprov gqu localhost > $tmpFile

while read line ; do

    userReport=$line
    user=`echo $userReport | cut -f1 -d\ `
    max_quota=`echo $userReport | cut -f2 -d\ `
    used_quota=`echo $userReport | cut -f3 -d\ `

    if [ $max_quota -ne 0 ] ; then
        quota_percentage=`echo "scale=1; (($used_quota * 100)/ $max_quota)" | bc`

        if [ `echo "$quota_percentage >= $warn_percentage" | bc` -eq 1 ] ; then
            max_quota_megs=`echo "scale=1; ($max_quota / 1048576)" | bc`
            echo "You are currently using $quota_percentage% of your mailbox quota of $max_quota_megs megabytes." | mail -s \"Quota Warning - $quota_percentage%\" $user
        fi    
  
    fi

done < $tmpFile

rm -f tmpFile


And of, course, the php way. Feel free to use whatever you want. PHP script has to be scheduled daily in zimbra user cron via 'crontab -e'

0 0 * * * /usr/bin/php /opt/zimbra/quotawarn/quotawarn.php

Customize script to match your own language and not send quota warns to wiki (documents), spam, ham, and admin accounts. By default, it sends warn message when quota reach 90% at 12:00PM everyday and when reach 75% every Sunday night.

<?php

$users=array();
$user=array();
$return = exec ("zmprov gqu localhost",$users_quota, $status);

for ($i = 1; $i < count($users_quota); $i++) 
{
$user['email']=strtok ($users_quota[$i]," ");
$user['max']= strtok (" ");
$user['act']= strtok (" ");

switch ($user['email']) {
                case "kuns1kesoy@vectorsf.com":
                        break;
                case "ud2qtlgeld@vectorsf.com":
                        break;
                case "beveg2wn@vectorsf.com":
                        break;
                case "wiki@vectorsf.com":
                        break;
                default:
                                if ($user['act'] > $user['max']*0.9) {
                                        echo "Storage for user ".$user['email']." over 90%\n";
                                        sendwarning ($user['email'],$user['act'],$user['max'],2);
                                }
                                else if ($user['act'] > $user['max']*0.75 && $user['act'] <  $user['max']*0.90 ) {
                                        echo "Storage for user ".$user['email']." over 75%\n";
                                        sendwarning ($user['email'],$user['act'],$user['max'],1);
                                }

        }

}

function sendwarning ($email,$usage,$maxquota,$warntype)
{
        $dow     = date ("D");
        $to      = $email;
        $subject = 'Aviso de utilizacion de cuota de mail';
        $headers = 'From: postmaster@vectorsf.com' . "\r\n" .
        'Reply-To: postmaster@vectorsf.com' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
        if ($warntype == 1 && $dow == "Sun")
                $message = "Tu cuota de correo ha superado el 75% de su asignacion.\nTe recomendamos vayas eliminando o archivando los mensajes mas antiguos para evitar problemas de almacenamiento futuros.\nEn estos momentos estas utilizando ".round($usage/1024/1024)." MB de un total de ".round($maxquota/1024/1024)." MB.\n";
        else if ($warntype == 2)
                $message = "Tu cuota de correo ha superado el 90% de su asignacion.\nTe recomendamos vayas eliminando o archivando los mensajes mas antiguos para evitar problemas de almacenamiento futuros.\nEn estos momentos estas utilizando ".round($usage/1024/1024)." MB de un total de ".round($maxquota/1024/1024)." MB.\n";
        else return;
    
    mail($to, $subject, $message, $headers);
}
?> 


And in English (Note: This is not really a translation of the above, just what I *think* it is. :-) ):

<?php

$users=array();
$user=array();
$return = exec ("zmprov gqu localhost",$users_quota, $status);

for ($i = 1; $i < count($users_quota); $i++)
{
$user['email']=strtok ($users_quota[$i]," ");
$user['max']= strtok (" ");
$user['act']= strtok (" ");

switch ($user['email']) {
                case "kjhkjh@zimbra1.yjsinc.com":
                break;
                case "jkhkjh@zimbra1.yjsinc.com":
                break;
                default:

if ($user['act'] > $user['max']*0.9) {
                      echo "Storage for user ".$user['email']." over 90%\n";
                      sendwarning ($user['email'],$user['act'],$user['max'],2);
} else if ($user['act'] > $user['max']*0.75 && $user['act'] <  $user['max']*0.90 ) {
     echo "Storage for user ".$user['email']." over 75%\n";
     sendwarning ($user['email'],$user['act'],$user['max'],1);
}
   }

        }

        function sendwarning ($email,$usage,$maxquota,$warntype)
                {
        $dow     = date ("D");
        $to      = $email;
        $subject = 'YellowJacket Mailbox Quota Warning!';
            $headers = 'From: postmaster@yjenergy.com' . "\r\n" . 'Reply-To: postmaster@yjenergy.com' . "\r\n" . 'X-Mailer: PHP/' . phpversion();
            if ($warntype == 1 && $dow == "Sun")
            $message = "Your mailbox is over 75% of your quota limit.\nWe recommend deleting or archiving your old emails to free up space.\nOnce quota is reached, emails will get bounced.\nUsage: ".round($us
age/1024/1024)." MB from a total of ".round($maxquota/1024/1024)." MB.\n";
                else if ($warntype == 2)
             $message = "Your mailbox is over 90% of your quota limit.\nWe recommend deleting or archiving your old emails to free up space.\nOnce quota is reached, emails will get bounced.\nUsage: ".round($u
sage/1024/1024)." MB from a total of ".round($maxquota/1024/1024)." MB.\n";
                 else return;

                 mail($to, $subject, $message, $headers);
                }
?>

If you're setting this up on RedHat, be sure to up2date --install php, and if you're on CentOS yum install php (or php-cli might be better if it's there).

Put the file above in /opt/zimbra/bin (as the root user).

Instead of adding this directly to the cron, as the 'root' user do the following:

# cd /etc/cron.daily
# vi zimbraquotawarn

Put this code in that file:

/sbin/runuser zimbra -l -s /bin/bash -c "/usr/bin/php -f /opt/zimbra/bin/quotawarn.php"
Then:
# chmod +x zimbraquotawarn

And you're all set.

Verified Against: unknown Date Created: 3/20/2007
Article ID: http://wiki.zimbra.com/index.php?title=Quota_Warning_Script Date Modified: 04/16/2010
Personal tools