Quota Warnings: Difference between revisions

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