#! /usr/bin/perl -wT

# check_mailq - check to see how many messages are in the smtp queue awating
#   transmittal.
#
# Initial version support sendmail's mailq command
#  Support for mutiple sendmail queues (Carlos Canau)
#  Support for qmail (Benjamin Schmid)

# License Information:
# 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
# (at your option) 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
############################################################################

require 5.004;
use POSIX;
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw(:DEFAULT :thresholds);

use vars qw($opt_v $PROGNAME $opt_w $opt_c $opt_t
	    $opt_M $status $state $msg $msg_q $msg_p $opt_W $opt_C $mailq @lines
	    %srcdomains %dstdomains);

use utils qw(%ERRORS &usage );

$w_opt->description("Min. number of messages in queue to generate warning");
$c_opt->description("Min. number of messages in queu to generate critical alert ( w < c )");
my $W_opt = new Plugin::Parameter(-name => "Warning", -flags => [ 'W', 'Warning' ],
				  -optional => "yes", -valueoptional => "no", -type => "INTEGER",
				  -description => "Min. number of messages for same domain in queue to generate warning");
my $C_opt = new Plugin::Parameter(-name => "Critical", -flags => [ 'C', 'Critical' ],
				  -optional => "yes", -valueoptional => "no", -type => "INTEGER",
				  -description => "Min. number of messages for same domain in queue to generate critical alert ( W < C )");
my $M_opt = new Plugin::Parameter(-name => "mailserver", -flags => [ 'M', 'mailserver' ],
				  -optional => "yes", -valueoptional => "no", -type => "PROGRAM",
				  -default => "sendmail",
				  -checker => sub { my ($opt, $parameter, $plugin) = @_;
						    if ($$opt !~ m/^(sendmail|qmail|postfix|exim)$/) {
						      $plugin->usage();
						      usage("$PROGNAME UNKNOWN: mailserver (-M) $$opt not supported\n");
						    }
						  },
				  -description => "[ sendmail | qmail | postfix | exim ]");
my $plugin = new Plugin(-revision => '$Revision: 1.4 $',
			-copyright => "2002 Subhendu Ghosh/Carlos Canau/Benjamin Schmid, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Checks the number of messages in the mail queue (supports multiple sendmail queues, qmail) Feedback/patches to support non-sendmail mailqueue welcome",
			-longcomment => "Note: -w and -c are required arguments.  -W and -C are optional. -W and -C are applied to domains listed on the queues - both FROM and TO. (sendmail) -W and -C are applied message not yet preproccessed. (qmail) This plugin uses the system mailq command (sendmail) or qmail-stat (qmail) to look at the queues. Mailq can usually only be accessed by root or a TrustedUser. You will have to set appropriate permissions for the plugin to work.",
			-checker => sub { my ($plugin) = @_;
					  if ($opt_w >= $opt_c) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: warning (-w) must be less than critical (-c)!\n");
					  }
					  if ((defined $opt_W && ! defined $opt_C)
					      || (!defined $opt_W && defined $opt_C)) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: Warning (-W) must be specified with Critical (-C)!\n");
					  }
					  if (defined $opt_W && $opt_W >= $opt_C) {
					    $plugin->usage();
					    usage("$PROGNAME UNKNOWN: Warning (-W) must be less than Critical (-C)!\n");
					  }
					},
			-parameterlists => [ [ $w_opt, $c_opt, $W_opt, $C_opt, $t_opt, $M_opt, $v_opt ], $h_opts, $V_opts ]);


$ENV{'PATH'}='';
$ENV{'BASH_ENV'}=''; 
$ENV{'ENV'}='';

$plugin->init();

$PROGNAME = "check_mailq";
$mailq = 'sendmail';	# default
$msg_q = 0 ;
$msg_p = 0 ;
$state = $ERRORS{'UNKNOWN'};

$plugin->start_timeout($opt_t, "ERROR: timed out waiting for $utils::PATH_TO_MAILQ");

# switch based on MTA

if ($opt_M eq "sendmail") {
  ## open mailq
  if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
    if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
      print "$PROGNAME UNKNOWN: ERROR: could not open $utils::PATH_TO_MAILQ \n";
      exit $ERRORS{'UNKNOWN'};
    }
  }elsif( defined $utils::PATH_TO_MAILQ){
    unless (-x $utils::PATH_TO_MAILQ) {
      print "$PROGNAME UNKNOWN: ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
      exit $ERRORS{'UNKNOWN'};
    }
  } else {
    print "$PROGNAME UNKNOWN: ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
    exit $ERRORS{'UNKNOWN'};
  }
	
  while (<MAILQ>) {
    # match email addr on queue listing
    if ( (/<.*@.*\.(\w+\.\w+)>/) || (/<.*@(\w+\.\w+)>/) ) {
      my $domain = $1;
      if (/^\w+/) {
	print "$utils::PATH_TO_MAILQ = srcdomain = $domain \n" if $opt_v ;
	$srcdomains{$domain} ++;
      }
      next;
    }
	
    #
    # ...
    # sendmail considers a message with more than one destiny, say N, to the same MX 
    # to have N messages in queue.
    # we will only consider one in this code
    if (( /\s\(reply:\sread\serror\sfrom\s.*\.(\w+\.\w+)\.$/ ) || ( /\s\(reply:\sread\serror\sfrom\s(\w+\.\w+)\.$/ ) ||
	( /\s\(timeout\swriting\smessage\sto\s.*\.(\w+\.\w+)\.:/ ) || ( /\s\(timeout\swriting\smessage\sto\s(\w+\.\w+)\.:/ ) ||
	( /\s\(host\smap:\slookup\s\(.*\.(\w+\.\w+)\):/ ) || ( /\s\(host\smap:\slookup\s\((\w+\.\w+)\):/ ) || 
	( /\s\(Deferred:\s.*\s.*\.(\w+\.\w+)\.\)/ ) || ( /\s\(Deferred:\s.*\s(\w+\.\w+)\.\)/ ) ) {
      print "$utils::PATH_TO_MAILQ = dstdomain = $1 \n" if $opt_v ;
      $dstdomains{$1} ++;
    }
	
    if (/\s+\(I\/O\serror\)/) {
      print "$utils::PATH_TO_MAILQ = dstdomain = UNKNOWN \n" if $opt_v ;
      $dstdomains{'UNKNOWN'} ++;
    }

    # Finally look at the overall queue length
    #
    if (/mqueue/) {
      print "$utils::PATH_TO_MAILQ = $_ "if $opt_v ;
      if (/ \((\d+) request/) {
	#
	# single queue: first line
	# multi queue: one for each queue. overwrite on multi queue below
	$msg_q = $1 ;
      }
    } elsif (/^\s+Total\sRequests:\s(\d+)$/) {
      print "$utils::PATH_TO_MAILQ = $_ \n" if $opt_v ;
      #
      # multi queue: last line
      $msg_q = $1 ;
    }
  }
	
  ## close mailq
  close (MAILQ); 
  # declare an error if we also get a non-zero return code from mailq
  # unless already set to critical
  if ( $? ) {
    $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
    print "STDERR $?: $!\n" if $opt_v;
    $msg = "$state: (stderr)\n";
  }

  $plugin->stop_timeout();

  ## now check the queue length(s)

  if ($msg_q == 0) {
    $msg = "OK: mailq is empty";
    $state = $ERRORS{'OK'};
  } else {
    print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $opt_v;
    # overall queue length
    if ($msg_q < $opt_w) {
      $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
      $state = $ERRORS{'OK'};
    }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
      $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
      $state = $ERRORS{'WARNING'};
    }else {
      $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
      $state = $ERRORS{'CRITICAL'};
    }

    # check for domain specific queue lengths if requested
    if (defined $opt_W) {
      # Apply threshold to queue lengths FROM domain
      my @srckeys = sort { $srcdomains{$b} <=> $srcdomains{$a} } keys %srcdomains;
      my $srcmaxkey = $srckeys[0];
      print "src max is $srcmaxkey with $srcdomains{$srcmaxkey} messages\n" if $opt_v;
		
      if ($srcdomains{$srcmaxkey} >= $opt_W && $srcdomains{$srcmaxkey} < $opt_C) {
	if ($state == $ERRORS{'OK'}) {
	  $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'WARNING'};
	} elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
	  $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
	} else {
	  $msg = "WARNING: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'WARNING'};
	}
      } elsif ($srcdomains{$srcmaxkey} >= $opt_C) {
	if ($state == $ERRORS{'OK'}) {
	  $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C)";
	  $state = $ERRORS{'CRITICAL'};
	} elsif ($state == $ERRORS{'WARNING'}) {
	  $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold C = $opt_C) -and- " . $msg;
	  $msg =~ s/WARNING: //;
	} elsif ($state == $ERRORS{'CRITICAL'}) {
	  $msg .= " -and- $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
	} else {
	  $msg = "CRITICAL: $srcdomains{$srcmaxkey} messages in queue FROM $srcmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'CRITICAL'};
	}
      } else {
	if ($srcdomains{$srcmaxkey} > 0) {
	  $msg .= " $srcdomains{$srcmaxkey} msgs. FROM $srcmaxkey is below threshold ($opt_W/$opt_C)";
	}
      }
      # Apply threshold to queue lengths TO domain
      my @dstkeys = sort { $dstdomains{$b} <=> $dstdomains{$a} } keys %dstdomains;
      my $dstmaxkey = $dstkeys[0];
      print "dst max is $dstmaxkey with $dstdomains{$dstmaxkey} messages\n" if $opt_v;
      if ($dstdomains{$dstmaxkey} >= $opt_W && $dstdomains{$dstmaxkey} < $opt_C) {
	if ($state == $ERRORS{'OK'}) {
	  $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'WARNING'};
	} elsif (($state == $ERRORS{'WARNING'}) || ($state == $ERRORS{'CRITICAL'})){
	  $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
	} else {
	  $msg = "WARNING: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'WARNING'};
	}
      } elsif ($dstdomains{$dstmaxkey} >= $opt_C) {
	if ($state == $ERRORS{'OK'}) {
	  $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C)";
	  $state = $ERRORS{'CRITICAL'};
	} elsif ($state == $ERRORS{'WARNING'}) {
	  $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold C = $opt_C) -and- " . $msg;
	  $msg =~ s/WARNING: //;
	} elsif ($state == $ERRORS{'CRITICAL'}) {
	  $msg .= " -and- $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
	} else {
	  $msg = "CRITICAL: $dstdomains{$dstmaxkey} messages in queue TO $dstmaxkey (threshold W = $opt_W)";
	  $state = $ERRORS{'CRITICAL'};
	}
      } else {
	if ($dstdomains{$dstmaxkey} > 0) {
	  $msg .= " $dstdomains{$dstmaxkey} msgs. TO $dstmaxkey is below threshold ($opt_W/$opt_C)";
	}
      }
    } # End of queue length thresholds
  }
  # end of ($mailq eq "sendmail")
} elsif ( $opt_M eq "postfix" ) {
  ## open mailq
  if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
    if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
      print "$PROGNAME UNKNOWN: ERROR: could not open $utils::PATH_TO_MAILQ \n";
      exit $ERRORS{'UNKNOWN'};
    }
  }elsif( defined $utils::PATH_TO_MAILQ){
    unless (-x $utils::PATH_TO_MAILQ) {
      print "$PROGNAME UNKNOWN: ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
      exit $ERRORS{'UNKNOWN'};
    }
  } else {
    print "$PROGNAME UNKNOWN: ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
    exit $ERRORS{'UNKNOWN'};
  }

  @lines = reverse <MAILQ>;

  # close qmail-qstat
  close MAILQ;
  # declare an error if we also get a non-zero return code from mailq
  # unless already set to critical
  if ( $? ) {
    $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
    print "STDERR $?: $!\n" if $opt_v;
    $msg = "$state: (stderr)\n";
  }

  $plugin->stop_timeout();

  # check queue length
  if ($lines[0]=~/Kbytes in (\d+)/) {
    $msg_q = $1 ;
  }elsif ($lines[0]=~/Mail queue is empty/) {
    $msg_q = 0;
  }else{
    print "$PROGNAME UNKNOWN: Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
    exit   $ERRORS{'UNKNOWN'};
  }

  # check queue length(s)
  if ($msg_q == 0){
    $msg = "OK: mailq reports queue is empty";
    $state = $ERRORS{'OK'};
  } else {
    print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $opt_v;
    # overall queue length
    if ($msg_q < $opt_w) {
      $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
      $state = $ERRORS{'OK'};
    }elsif  ($msg_q >= $opt_w  && $msg_q < $opt_c) {
      $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
      $state = $ERRORS{'WARNING'};
    }else {
      $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
      $state = $ERRORS{'CRITICAL'};
    }
  }
  # end of ($mailq eq "postfixl")
} elsif ( $opt_M eq "qmail" ) {
  # open qmail-qstat 
  if ( defined $utils::PATH_TO_QMAIL_QSTAT && -x $utils::PATH_TO_QMAIL_QSTAT ) {
    if (! open (MAILQ, "$utils::PATH_TO_QMAIL_QSTAT | " ) ) {
      print "$PROGNAME UNKNOWN: ERROR: could not open $utils::PATH_TO_QMAIL_QSTAT \n";
      exit $ERRORS{'UNKNOWN'};
    }
  }elsif( defined $utils::PATH_TO_QMAIL_QSTAT){
    unless (-x $utils::PATH_TO_QMAIL_QSTAT) {
      print "$PROGNAME UNKNOWN: ERROR: $utils::PATH_TO_QMAIL_QSTAT is not executable by (uid $>:gid($)))\n";
      exit $ERRORS{'UNKNOWN'};
    }
  } else {
    print "$PROGNAME UNKNOWN: ERROR: \$utils::PATH_TO_QMAIL_QSTAT is not defined\n";
    exit $ERRORS{'UNKNOWN'};
  }

  @lines = <MAILQ>;

  # close qmail-qstat
  close MAILQ;
  # declare an error if we also get a non-zero return code from mailq
  # unless already set to critical
  if ( $? ) {
    $state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
    print "STDERR $?: $!\n" if $opt_v;
    $msg = "$state: (stderr)\n";
  }

  $plugin->stop_timeout();

  # check queue length
  if ($lines[0]=~/^messages in queue: (\d+)/) {
    $msg_q = $1 ;
  }else{
    print "$PROGNAME UNKNOWN: Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
    exit   $ERRORS{'UNKNOWN'};
  }

  # check messages not processed
  if ($lines[1]=~/^messages in queue but not yet preprocessed: (\d+)/) {
    my $msg_p = $1;
  }else{
    print "$PROGNAME UNKNOWN: Couldn't match $utils::PATH_TO_QMAIL_QSTAT output\n";
    exit  $ERRORS{'UNKNOWN'};
  }

  # check queue length(s)
  if ($msg_q == 0){
    $msg = "OK: qmail-qstat reports queue is empty";
    $state = $ERRORS{'OK'};
  } else {
    print "msg_q = $msg_q warn=$opt_w crit=$opt_c\n" if $opt_v;
    # overall queue length
    if ($msg_q < $opt_w) {
      $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
      $state = $ERRORS{'OK'};
    }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
      $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
      $state = $ERRORS{'WARNING'};
    }else {
      $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
      $state = $ERRORS{'CRITICAL'};
    }
    # check messages not yet preprocessed (only compare is $opt_W and $opt_C
    # are defined)
    if (defined $opt_W) {
      $msg .= "[Preprocessed = $msg_p]";
      if ($msg_p >= $opt_W && $msg_p < $opt_C ) {
	$state = $state == $ERRORS{"CRITICAL"} ? $ERRORS{"CRITICAL"} : $ERRORS{"WARNING"}  ;
      }elsif ($msg_p >= $opt_C ) {
	$state = $ERRORS{"CRITICAL"} ;
      }
    }
  }				
  # end of ($mailq eq "qmail")
} elsif ( $opt_M eq "exim" ) {
  ## open mailq 
  if ( defined $utils::PATH_TO_MAILQ && -x $utils::PATH_TO_MAILQ ) {
    if (! open (MAILQ, "$utils::PATH_TO_MAILQ | " ) ) {
      print "$PROGNAME UNKNOWN: ERROR: could not open $utils::PATH_TO_MAILQ \n";
      exit $ERRORS{'UNKNOWN'};
    }
  }elsif( defined $utils::PATH_TO_MAILQ){
    unless (-x $utils::PATH_TO_MAILQ) {
      print "$PROGNAME UNKNOWN: ERROR: $utils::PATH_TO_MAILQ is not executable by (uid $>:gid($)))\n";
      exit $ERRORS{'UNKNOWN'};
    }
  } else {
    print "$PROGNAME UNKNOWN: ERROR: \$utils::PATH_TO_MAILQ is not defined\n";
    exit $ERRORS{'UNKNOWN'};
  }

  while (<MAILQ>) {
    #22m  1.7K 19aEEr-0007hx-Dy <> *** frozen ***
    #root@exlixams.glups.fr

    if (/\s[\w\d]{6}-[\w\d]{6}-[\w\d]{2}\s/) { # message id 19aEEr-0007hx-Dy
      $msg_q++ ;
    }
  }
  close(MAILQ) ;

  $plugin->stop_timeout();

  if ($msg_q < $opt_w) {
    $msg = "OK: mailq ($msg_q) is below threshold ($opt_w/$opt_c)";
    $state = $ERRORS{'OK'};
  }elsif ($msg_q >= $opt_w  && $msg_q < $opt_c) {
    $msg = "WARNING: mailq is $msg_q (threshold w = $opt_w)";
    $state = $ERRORS{'WARNING'};
  }else {
    $msg = "CRITICAL: mailq is $msg_q (threshold c = $opt_c)";
    $state = $ERRORS{'CRITICAL'};
  }
 # end of ($mailq eq "exim")
}

# Perfdata support
print "$PROGNAME $msg|unsent=$msg_q;$opt_w;$opt_c;0\n";
exit $state;
