#!/usr/bin/perl
#
#############################################################################################
#
#Copyleft 2010 Thomas GINESTET (thomas dot ginestet at cryonux dot com)
#
#This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or any later version.
#
#This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# Please notice this plugin has to be used for nsca check on a Zimbra server only.
#
#############################################################################################

use strict;
use Getopt::Long;

##########
my $nsca_host = "my_nagios_server_ip"; 
my $config = "/etc/send_nsca.cfg";
my $send_nsca = "/usr/sbin/send_nsca -c $config -H $nsca_host";
my $hosts = "my_zimbra_server_ip";
my $service = "check_mailbox_quota";
my $command = '/opt/zimbra/bin/zmprov gqu localhost';
my $warning = 99.8; # define alert threshold in % for used quota
my $warntocrit = 10; # define alert threshold in integer for users being over quota
my $code;
my $value;
my ($account, $quota, $used);
my $count = 0;
my @result;
##########


@result = `$command`;

foreach (@result) {
	($account, $quota, $used) = split(/ /,$_);
	if ($quota == 0) { $quota = 536870912; } # hack to solve system accounts quota's problems like spam filter
	$used = ($used / $quota) *100;
	if ($used >= $warning) {
		$count ++;
	}
}
if ($count == 0) {
	$code = 0;
	$value = "OK: Aucun utilisateur hors quota de messagerie";
}
if (($count > 0) &&  ($count < $warntocrit) ) { 
	$code = 1;
	$value = "WARNING: $count utilisateur(s) hors quota de messagerie";
}  
if ($count >= $warntocrit) { 
	$code = 2;
	$value = "CRITICAL: $count utilisateur(s) hors quota de messagerie";
}

open(SEND,"|$send_nsca") || die "Could not run $send_nsca: $!\n";
	print SEND "$hosts\t$service\t$code\t$value\n";
close SEND;

exit(0);

