# -*- perl -*-
#
# $Revision: 1.1 $
package mailq;

require Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(&print_usage &print_version &service_test);
use lib $main::runtimedir;
use csl_utils qw($TIMEOUT %ERRORS);
use strict;

sub print_usage ();
sub print_version ();
sub service_test (%);

sub print_usage () {

    print "
Usage:  Check mailq service.
Options:
    -m <messages warn, messages crit>               # checks mailq command
    -f <files warn, files crit, directory>          # checks # files in dir
    -q <qf files warn, qf files crit, directory>    # checks qf* files in dir
    --help
    --version
    --usage
";
    return $ERRORS{'OK'};
}

sub print_version () {
    my $version = '$Revision: 1.1 $';
    $version =~ s/^\$Revision: //;
    $version =~ s/ \$\s*$//;

    print "mailq.pm - $version\n";
    return $ERRORS{'OK'};
}

sub service_test (%) {
    my %options = @_;
    my $files; 

    if ($options{"verbose"}) {
        foreach my $opt (sort keys(%options)) {
            if ($options{$opt}) {
                print "$opt - $options{$opt}\n";
            }
        }
    }

    my $status = "OK";
    my $MAILQ = "/usr/bin/mailq -OMaxQueueRunSize=1";
    my $return;

    if (@{$options{"opt_q"}}) {
        print "qf files\n" if ($options{verbose});

        foreach my $arg (@{$options{"opt_q"}}) {
            my ($min, $max, $directory) = split(",", $arg);

            procCheck($min, $max);
            print_usage unless (-d $directory);
            print "Proc: $min - $max, $directory\n" if ($options{"verbose"});

#            opendir(DIR, $directory) || die "can't opendir $directory: $!";
#            my $files = scalar (grep { /^qf/ } readdir(DIR));
#            closedir DIR;  

            open (IN, "$MAILQ -oQ$directory |") or die "cannot run $MAILQ -oQ$directory: $!\n";
            while (<IN>) {
            	if (/Total.requests:/) {
		    chomp;
                    s/.*Total.requests:\s*//;
		    $files = $_;
            	}
            }
            close IN;

            if ($files >= $max) {
                $status = "CRITICAL";
                $return .= "$files (allowed $min-$max) qf messages in $directory # $return";
            } elsif (($status eq "WARNING") && ($files >= $min)) {
                $status = "WARNING";
                $return = "$files (allowed $min-$max) qf messages in $directory # $return";
            } else {
                $return .= "$files (allowed $min-$max) qf messages in $directory # ";
            }
        }
    } 

    if (@{$options{"opt_f"}}) {
        print "files\n" if ($options{verbose});

        foreach my $arg (@{$options{"opt_f"}}) {
            my ($min, $max, $directory) = split(",", $arg);

            procCheck($min, $max);
            print_usage unless (-d $directory);
            print "Proc: $min - $max, $directory\n" if ($options{"verbose"});

            opendir(DIR, $directory) || die "can't opendir $directory: $!";
            my $files = scalar (grep { !/^\./ } readdir(DIR));
            closedir DIR;  

            if ($files >= $max) {
                $status = "CRITICAL";
                $return = "$files (allowed $min-$max) files in $directory # $return";
            } elsif (($status eq "WARNING") && ($files >= $min)) {
                $status = "WARNING";
                $return = "$files (allowed $min-$max) files in $directory # $return";
            } else {
                $return .= "$files (allowed $min-$max) files in $directory # ";
            }
        }
    }

    if ( ( !@{$options{opt_f}} && !@{$options{opt_f}} ) || 
                                                @{$options{opt_m}} ) {
        # probably -m, default
        print "mailq\n" if ($options{verbose});

        my $min = 1500;
        my $max = 3000;

        ($min, $max) = split(",", ${$options{"opt_m"}}[0]) if (@{$options{"opt_m"}});

        procCheck($min, $max);
        print "Proc: $min - $max\n" if ($options{"verbose"});

        open (IN, "$MAILQ |") or die "cannot run $MAILQ: $!\n";
        while (<IN>) {
            if (/Total.requests:/) {
	        chomp;
                s/.*Total.requests:\s*//;
		$files = $_;
            }
        }
        close IN;

        if ($files >= $max) {
            $status = "CRITICAL";
            $return = "$files (allowed $min-$max) messages through mailq # $return";
        } elsif ($files >= $min) {
            $status = "WARNING";
            $return = "$files (allowed $min-$max) messages through mailq # $return";
        } else {
            $return .= "$files (allowed $min-$max) messages through mailq # ";
        }
    }
    
    print "$status: $return\n";
    print "Exit status: $status\n" if $options{"verbose"};

    exit $ERRORS{$status};
}

sub procCheck {
    my $min = shift;
    my $max = shift;

    if ((!$min) || (!$max) || ($min > $max)) {
        print "Bad Process listing - $min, $max\n";
        print_usage;
        exit;
    }

}

################################
# 
# Documentation Stuff
#
# -stefan
#
################################

=head1 NAME

csl_mailq

=head1 SYNOPSIS

test stuff

=head1 DESCRIPTION

Ditto!!

=head1 OPTIONS

=item B<-U>, B<--usage>

=head1 OTHER STUFF

Who cares!!

