Quota Warnings: Difference between revisions

Line 1: Line 1:
=== 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 9:




''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 27:
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>weborama.fr
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;
#send email to user to inform about the problem
# help
my $mailinform = 1;
if ($ARGV[0] =~ /^-h|^--help|^-H/){
#commands you need
  print " *** quota_zimbra.pl *** \n";
my $command = '/opt/zimbra/bin/zmprov gqu';
  print_usage();
my $sendmail = '/opt/zimbra/postfix/sbin/sendmail';
}
 
# options
my $mailfrom = "admins\@example.com";
GetOptions ('mail=s' => \$mail,
my $verbose = 1;
  'warning=s' => \$warning,
my @servers = ('localhost');
  'critical=s' => \$critical
 
  );
my $message_template = sprintf<<EOF;
# default values
To: <###NAME###> ###NAME###
$warning =  "85" unless $warning;
Reply-To: <$mailfrom> $mailfrom
$critical =  "90" unless $critical;
Subject: Zimbra Quota ###STATE### - used ###USED###% - trigger ###STATEMAX###%
$mail =  "no" unless $mail;
X-Generated-By: $0
# conditions
X-Mailbox-State: ###STATE###
if ($mail !~ /^(no|yes)$/) {
NOTE: The quota will be set active on 5th of September 2007, this message is a warning only
  print "ERROR : the mail value must be 'yes' or 'no'\n";
 
  print_usage();
Hi ###NAME###,
}
 
if ( ($critical < $warning) || ($critical < 0) || ($warning < 0) || ($warning > 100) || ($critical > 100) ) {
Sorry to bother you, but your mailbox is in a ###STATE### state, which means
  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";
more than ###STATEMAX### percent is used.
  print_usage();
Please make some space or you will not be able to receive email anymore.
}
 
# the script
You are using ###SPACE### and the quota is set to ###QUOTA###
@result = `$command`;
 
foreach (@result) {
Some tips of reducing your mailboxsize:
  ($nom, $quota, $used) = split(/ /,$_);
* Empty your trash folder
  $c_all ++;
* Make a local backup of some mesages
  if ($quota eq "0") {
 
      print "INFO : $nom has no quota restriction\n" if ($debug);
Best regards,
      next;
Your quota reminder
  }
Department System administation
  $used = ($used / $quota) *100;
EOF
  if ( ($used >= $warning) && ($used < $critical) ) {
#############################
      $c_warn ++;
 
      print "WARNING : $nom is in a warning state\n";
my ($warning,$critical, $mailinform);
      $mail_warn  = '/usr/bin/printf "%b" "To:.$nom. \nSubject: Zimbra Quota \n'.$message_warning.'" | /usr/sbin/sendmail -t -F'.$mailfrom;
my ($mail_warn, $mail_crit);
      `$mail_warn` if ($mail eq "yes");
my ($nom, $quota, $used);
  }
my $c_all = 0;
  elsif ($used >= $critical) {
my $c_warn = 0;
      $c_crit ++;
my $c_crit = 0;
      print "CRITICAL : $nom is in a critical state\n";
my @result;
      $mail_crit = '/usr/bin/printf "%b" "To:.$nom. \nSubject: Zimbra Quota \n'.$message_critical.'" | /usr/sbin/sendmail -t -F'.$mailfrom;
 
      `$mail_crit` if ($mail eq "yes");
if ($ARGV[0] =~ /^-h|^--help|^-H/){
    }
  print " *** quota_zimbra.pl *** \n";
    else {
  print_usage();
      print "INFO : $nom is ok !\n" if ($debug);
}
    }
GetOptions ('mail=s' => \$mailinform,
}
  'warning=s' => \$warning,
if ($debug) {
  'critical=s' => \$critical
  print "\n*******\nINFO : 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";
  if ($mailinform !~ /^(0|1)$/) {
  print "INFO : There are $c_all zimbra users\n";
  print "ERROR : the mail value must be '1'  or '0' to disable mail notification\n";
}
  print_usage();
exit(0);
}
# function 1 :  display the help
 
sub print_usage()
# default values
{
$warning = "85" unless $warning;
  print "Utilisation: ./quota_zimbra.pl -mail [yes | no] -warning 85 -critical 90\n";
$critical = "90" unless $critical;
  print "Options:\n";
$mailinform =  "0" unless $mailinform;
  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 "-- Quota Warning v2 --\n";
  print "\t-warning INTEGER\n";
print "Options :\n\tmailinform : $mailinform\n\twarning : $warning%\n\tcritical : $critical%\n\n";
  print "\t\tallow you to set up the warning threshold. The default value is 85\n";
 
  print "\t-critical INTEGER\n";
foreach my $server (@servers) {
  print "\t\tallow you to set up the critical threshold. The default value is 90\n";
print "INFO : Reporting server $server\n" if ($verbose);
  exit(0);
@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);
}
 
  </pre>

Revision as of 09:54, 9 August 2007

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