#!/usr/bin/perl -w

use POSIX;
use strict;

$ENV{'PATH'}='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';
my $PROGNAME = "check_mailq";
my $mailq = '/usr/bin/mailq';
my $msg_q = 0 ;

if (! open (MAILQ, "$mailq 2>&1 | ")) {
	die "ERROR: $mailq returned an error\n";
}

my @lines = reverse <MAILQ>;
close(MAILQ);

if ( $? ) {
	die "CRITICAL: Error code ".($?>>8)." returned from $mailq",$/;
}

## shut off the alarm

# check queue length
if ($lines[0]=~/Kbytes in (\d+)/) {
	$msg_q = $1 ;
}elsif ($lines[0]=~/Mail queue is empty/) {
	$msg_q = 0;
}else{
        die "Couldn't match $mailq output\n";
}

print "$msg_q";


