Quota Warnings: Difference between revisions

No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Archive}}{{Unsupported}}
{{Article Infobox|{{admin}}|||{{ZCS 4.5}}}}Also see http://wiki.zimbra.com/index.php?title=Quota_Warning_Script
v5 will have quota warnings built-in
=== Quota Limit ===
=== Quota Limit ===


Hi there !
''Target'' :


I did my own little script that allow users to be alerted when users are too close to their quota limit.
I did my own little script that allow users to be alerted when users are too close to their quota limit.
Line 9: Line 13:




''examples'' :
 
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'' :


<code>./quota_zimbra.pl -warning 87 -critical 90 -mail yes</code>
<code>./quota_zimbra.pl -warning 87 -critical 90 -mail yes</code>
Line 21: Line 31:
Please excuse my english level and my wiki presentation ;-)
Please excuse my english level and my wiki presentation ;-)


I will update this script as soon as possible (with exclude option...)
I want to thank Philippe Lizon to correct a minor error :-)


  #!/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
The script '''quota_zimbra.pl''' v0.2
#
 
# v0.1 2007-04-24 : first version by Erwan Ben Souiden : erwan<at>weborama.fr
 
use strict;
 
use Getopt::Long;
  <pre>
##############################
#!/usr/bin/perl
# DON T FORGET TO MODIFY LIKE YOU WANT
# quota_zimbra.pl : this perl script allows the user to know if his current mail quota is a warning state...
# mailto value and mailfrom value
# the state is determinated by the thresholds (in percent) you set
my $mailfrom = "you\@domain.com";
#
# message_critical value and message_warning value
# link : http://wiki.zimbra.com/index.php?title=Quota_Warnings
my $message_critical = "Your mailbox is in a critical state... please make some space or you will regret it ! mouhahahaaaaa.\nYour dear funky administrator";
#
my $message_warning = "Your mailbox is in a warning state...please make some space.\nYour dear funky administrator";
# History :
# debug value
# v0.2 2007-08-08 : update of the code by Rick van der Zwet : rick.van.der.zwet<at>joost.com
my $debug = 1;
# v0.1 2007-04-24 : first version by Erwan Ben Souiden : erwan<at>aleikoum.net
#############################
 
# the commands you need !
use strict;
my $command = '/opt/zimbra/bin/zmprov gqu localhost';
use Getopt::Long;
my ($mail_warn, $mail_crit);
 
my ($mail, $warning, $critical);
#############################
my ($nom, $quota, $used);
# DON T FORGET TO MODIFY LIKE YOU WANT
my $c_all = 0;
 
my $c_warn = 0;
#use this quota instead of the zimbra quota, setting it to 0 will use the zimbra quota
my $c_crit = 0;
my $softquota = "1073741824";
my @result;
#my $softquota = "0";
# help
#commands you need
if ($ARGV[0] =~ /^-h|^--help|^-H/){
my $command = '/opt/zimbra/bin/zmprov gqu';
  print " *** quota_zimbra.pl *** \n";
my $sendmail = '/opt/zimbra/postfix/sbin/sendmail';
  print_usage();
 
}
my $mailfrom = "admins\@example.com";
# options
my $verbose = 1;
GetOptions ('mail=s' => \$mail,
my @servers = ('localhost');
  'warning=s' => \$warning,
 
  'critical=s' => \$critical
my $message_template = sprintf<<EOF;
  );
To: <###NAME###> ###NAME###
# default values
Reply-To: <$mailfrom> $mailfrom
$warning =  "85" unless $warning;
Subject: Zimbra Quota ###STATE### - used ###USED###% - trigger ###STATEMAX###%
$critical =  "90" unless $critical;
X-Generated-By: $0
$mail =  "no" unless $mail;
X-Mailbox-State: ###STATE###
# conditions
NOTE: The quota will be set active on 5th of September 2007, this message is a warning only
if ($mail !~ /^(no|yes)$/) {
 
  print "ERROR : the mail value must be 'yes' or 'no'\n";
Hi ###NAME###,
  print_usage();
 
}
Sorry to bother you, but your mailbox is in a ###STATE### state, which means
if ( ($critical < $warning)  || ($critical < 0) || ($warning < 0) || ($warning > 100) || ($critical > 100) ) {
more than ###STATEMAX### percent is used.
  print "ERROR : the critical threshold must be higher than the warning threshold. And both value must be higher than 0 and smaller than 100\n";
Please make some space or you will not be able to receive email anymore.
  print_usage();
 
}
You are using ###SPACE### and the quota is set to ###QUOTA###
# the script
 
@result = `$command`;
Some tips of reducing your mailboxsize:
foreach (@result) {
* Empty your trash folder
  ($nom, $quota, $used) = split(/ /,$_);
* Make a local backup of some mesages
  $c_all ++;
 
  if ($quota eq "0") {
Best regards,
      print "INFO : $nom has no quota restriction\n" if ($debug);
Your quota reminder
      next;
Department System administation
  }
EOF
  $used = ($used / $quota) *100;
#############################
  if ( ($used >= $warning) && ($used < $critical) ) {
 
      $c_warn ++;
my ($warning,$critical, $mailinform);
      print "WARNING : $nom is in a warning state\n";
my ($mail_warn, $mail_crit);
      $mail_warn  = '/usr/bin/printf "%b" "To:.$nom. \nSubject: Zimbra Quota \n'.$message_warning.'" | /usr/sbin/sendmail -t -F'.$mailfrom;
my ($nom, $quota, $used);
      `$mail_warn` if ($mail eq "yes");
my $c_all = 0;
  }
my $c_warn = 0;
  elsif ($used >= $critical) {
my $c_crit = 0;
      $c_crit ++;
my @result;
      print "CRITICAL : $nom is in a critical state\n";
 
      $mail_crit = '/usr/bin/printf "%b" "To:.$nom. \nSubject: Zimbra Quota \n'.$message_critical.'" | /usr/sbin/sendmail -t -F'.$mailfrom;
if ($ARGV[0] =~ /^-h|^--help|^-H/){
      `$mail_crit` if ($mail eq "yes");
  print " *** quota_zimbra.pl *** \n";
    }
  print_usage();
    else {
}
      print "INFO : $nom is ok !\n" if ($debug);
GetOptions ('mail=s' => \$mailinform,
    }
  'warning=s' => \$warning,
}
  'critical=s' => \$critical
if ($debug) {
  );
  print "\n*******\nINFO : Stats from quota_zimbra.pl\n";
 
  print "INFO : $c_crit users in a critical state\n";
  if ($mailinform !~ /^(0|1)$/) {
  print "INFO : $c_warn users in a critical state\n";
  print "ERROR : the mail value must be '1'  or '0' to disable mail notification\n";
  print "INFO : There are $c_all zimbra users\n";
  print_usage();
}
}
exit(0);
 
# function 1 :  display the help
# default values
sub print_usage()
$warning = "85" unless $warning;
{
$critical =  "90" unless $critical;
  print "Utilisation: ./quota_zimbra.pl -mail [yes | no] -warning 85 -critical 90\n";
$mailinform =  "0" unless $mailinform;
  print "Options:\n";
 
  print "\t-mail [yes | no]\n";
print "-- Quota Warning v2 --\n";
  print "\t\tif you want to disable the mail report to the user please. The default value is 'no'\n";
print "Options :\n\tmailinform : $mailinform\n\twarning : $warning%\n\tcritical : $critical%\n\n";
  print "\t-warning INTEGER\n";
 
  print "\t\tallow you to set up the warning threshold. The default value is 85\n";
foreach my $server (@servers) {
  print "\t-critical INTEGER\n";
print "INFO : Reporting server $server\n" if ($verbose);
  print "\t\tallow you to set up the critical threshold. The default value is 90\n";
@result = `$command $server`;
  exit(0);
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 [1 | 0] -warning 85 -critical 90\n";
  print "Options:\n";
  print "\t-mail [1 | 0]\n";
  print "\t\The mail value must be '1'  or '0' to disable mail notification. The default value is '0'\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);
}
 
</pre>
 
{{Article Footer|unknown|4/25/2007}}
 
[[Category:Administration]]
[[Category:ZCS 4.5]]

Latest revision as of 17:23, 24 March 2015

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_Warning_Script v5 will have quota warnings built-in

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>aleikoum.net

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";
#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 [1 | 0] -warning 85 -critical 90\n";
  print "Options:\n";
  print "\t-mail [1 | 0]\n";
  print "\t\The mail value must be '1'  or '0' to disable mail notification. The default value is '0'\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);
}

 
Verified Against: unknown Date Created: 4/25/2007
Article ID: https://wiki.zimbra.com/index.php?title=Quota_Warnings Date Modified: 2015-03-24



Try Zimbra

Try Zimbra Collaboration with a 60-day free trial.
Get it now »

Want to get involved?

You can contribute in the Community, Wiki, Code, or development of Zimlets.
Find out more. »

Looking for a Video?

Visit our YouTube channel to get the latest webinars, technology news, product overviews, and so much more.
Go to the YouTube channel »

Jump to: navigation, search