Quota Warnings

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Quota Limit

Target :

I did my own little script that allow users to be alerted when users are too close to their quota limit. You can set-up thresholds to determine when a user is in a warning, critical or ok state.

This script is, I think, ideal for a cronjob.


Thanks to Rick van der Zwet, there is a new version !!

Besides, I want to thank Philippe Lizon to correct a minor error :-) (in the v0.1)


Examples :

./quota_zimbra.pl -warning 87 -critical 90 -mail yes

more information :

./quota_zimbra.pl --help

I hope it could be usefull for some administrators :-D

Please excuse my english level and my wiki presentation ;-)



The script quota_zimbra.pl v0.2


#!/usr/bin/perl
# quota_zimbra.pl : 	this perl script allows the user to know if his current mail quota is a warning state...
#			the state is determinated by the thresholds (in percent) you set
#
# link : http://wiki.zimbra.com/index.php?title=Quota_Warnings
#
# History :
# v0.2	2007-08-08		: update of the code by Rick van der Zwet : rick.van.der.zwet<at>joost.com
# v0.1		2007-04-24		: first version by Erwan Ben Souiden : erwan<at>weborama.fr

use strict;
use Getopt::Long;

#############################
# DON T FORGET TO MODIFY LIKE YOU WANT

#use this quota instead of the zimbra quota, setting it to 0 will use the zimbra quota
my $softquota = "1073741824";
#my $softquota = "0";
#send email to user to inform about the problem
my $mailinform = 1;
#commands you need
my $command = '/opt/zimbra/bin/zmprov gqu';
my $sendmail = '/opt/zimbra/postfix/sbin/sendmail';

my $mailfrom = "admins\@example.com";
my $verbose = 1;
my @servers = ('localhost');

my $message_template = sprintf<<EOF;
To: <###NAME###> ###NAME###
Reply-To: <$mailfrom> $mailfrom
Subject: Zimbra Quota ###STATE### - used ###USED###% - trigger ###STATEMAX###%
X-Generated-By: $0
X-Mailbox-State: ###STATE###
NOTE: The quota will be set active on 5th of September 2007, this message is a warning only

Hi ###NAME###,

Sorry to bother you, but your mailbox is in a ###STATE### state, which means 
more than ###STATEMAX### percent is used.
Please make some space or you will not be able to receive email anymore.

You are using ###SPACE### and the quota is set to ###QUOTA###

Some tips of reducing your mailboxsize:
* Empty your trash folder
* Make a local backup of some mesages

Best regards,
Your quota reminder
Department System administation
EOF
#############################

my ($warning,$critical, $mailinform);
my ($mail_warn, $mail_crit);
my ($nom, $quota, $used);
my $c_all = 0;
my $c_warn = 0;
my $c_crit = 0;
my @result;

if ($ARGV[0] =~ /^-h|^--help|^-H/){
  print " *** quota_zimbra.pl *** \n";
  print_usage();
}
GetOptions ('mail=s' => \$mailinform,
  'warning=s' => \$warning,
  'critical=s' => \$critical
  );

  if ($mailinform !~ /^(0|1)$/) {
  print "ERROR : the mail value must be '1'  or '0' to disable mail notification\n";
  print_usage();
}
  
# default values
$warning =  "85" unless $warning;
$critical =  "90" unless $critical;
$mailinform =  "0" unless $mailinform;

print "-- Quota Warning v2 --\n";
print "Options :\n\tmailinform : $mailinform\n\twarning : $warning%\n\tcritical : $critical%\n\n";

foreach my $server (@servers) {
	print "INFO : Reporting server $server\n" if ($verbose);
	@result = `$command $server`;
	foreach (@result) {
		($nom, $quota, $used) = split(/ /,$_);
		#use softquota if defined
		if ($softquota ) { 
			$quota = $softquota;
		}
		$c_all ++;
		if ($quota eq "0") {
			print "INFO : $nom has no quota restriction\n" if ($verbose);
			next;
		}
        my $usedMB = sprintf "%.2f", $used / (1024 * 1024);
        my $quotaMB = sprintf "%.2f", $quota / (1024 * 1024);
        #print "$quota $quotaMB";
		my $message = $message_template;
		$used = ($used / $quota) * 100;
        $used = sprintf "%.2f", $used;

		$message =~ s/###QUOTA###/${quotaMB}MB/g;
        $message =~ s/###SPACE###/${usedMB}MB/g;
		$message =~ s/###NAME###/$nom/g;
        $message =~ s/###USED###/$used/g;
		
		if ( ($used >= $warning) && ($used < $critical) ) {
			$c_warn ++;
			print "WARNING : $nom used $used\% - trigger $warning\% \n";
			my $subject = "Zimbra Quota warning - used $used\% - trigger $warning\%";
            $message =~ s/###STATE###/warning/g;
            $message =~ s/###STATEMAX###/$warning/g;
			if ($mailinform) {
				open( MAIL, "| $sendmail -F$mailfrom $nom");
				print MAIL $message;
                close(MAIL);
            }
		}
		elsif ($used >= $critical) {
			$c_crit ++;
			print "CRITICAL : $nom used $used\% - trigger $critical\% \n";
			my $subject = "Zimbra Quota warning - used $used\% - trigger $critical\%";
            $message =~ s/###STATE###/critical/g;
            $message =~ s/###STATEMAX###/$critical/g;
			if ($mailinform) {
				open( MAIL, "| $sendmail -F$mailfrom $nom");
				print MAIL $message;
                close(MAIL);
            }
	   }
	   else {
			print "INFO : $nom is ok !\n" if ($verbose);
	   }
	}
}

if ($verbose) {
  print "\n*******\n";
  print "INFO : Softquota in use $softquota bytes\n";
  print "INFO : Stats from quota_zimbra.pl\n";
  print "INFO : $c_crit users in a critical state\n";
  print "INFO : $c_warn users in a warning state\n";
  print "INFO : There are $c_all zimbra users\n";
}

#make sure to exit with non-zero when some mailbox is in warning of critical state
exit($c_crit + $c_warn);

# function 1 :  display the help 
sub print_usage()
{
  print "Utilisation: ./quota_zimbra.pl -mail [yes | no] -warning 85 -critical 90\n";
  print "Options:\n";
  print "\t-mail [yes | no]\n";
  print "\t\tif you want to disable the mail report to the user please. The default value is 'no'\n";
  print "\t-warning INTEGER\n";
  print "\t\tallow you to set up the warning threshold. The default value is 85\n";
  print "\t-critical INTEGER\n";
  print "\t\tallow you to set up the critical threshold. The default value is 90\n";
  exit(0);
}

 
Jump to: navigation, search