#    Big Sister sendmail queue size monitor
#
#    Copyright (C) 2001 ToolCASE LLC
#    Contact: Jacob Gore <jacob.gore@toolcase.com>, +1-303-771-5436.
#
#    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.
#
#    Syntax for entering it into adm/uxmon-net:
#
#    host   frequency=Number mailq_command=String \
#             messages_yellow=Number messages_red=Number \
#             sendmail
#
#    Defaults:  mailq_command = /usr/bin/mailq
#               messages_yellow = 300
#               messages_red = 500
#               frequency = (default set as usually)
#
#    NB: This program has only been really tested on Solaris, though
#    it seems to work on Linux too.  The OS dependency is the format
#    produced by the "mailq" command.

#=============================================================================

my $mail;

print "SENDMAIL: Starting config.\n" if $BigSister::common::dl>3;

# check to see if module is already in memory; if it isn't, load it
unless ($mail = $memory{"sendmail_$args{frequency}"}) {

    print "SENDMAIL: need to load module...\n" if $BigSister::common::dl>4;

    &uxmon::load_module('sendmail');

    print "SENDMAIL: ...done.  Creating mail monitor...\n" if $BigSister::common::dl>4;

    $mail = Monitor::sendmail->new($args{mailq_command});

    print "SENDMAIL: ...done.  Registering 'sendmail_$args{frequency}'...\n"
        if $BigSister::common::dl>4;

    $memory{"sendmail_$args{frequency}"} = $mail;

    print "SENDMAIL: ...done.  Running checker...\n" if $BigSister::common::dl>4;
    
    &uxmon::checker($mail);

    print "SENDMAIL: ...done.  Scheduling frequency $args{frequency}...\n"
        if $BigSister::common::dl>4;

    $mail->sched_frequency($args{frequency});

    print "SENDMAIL: ...done.\n" if $BigSister::common::dl>4;

} else {
    
    print "SENDMAIL: DON'T need to load module.\n" if $BigSister::common::dl>4;
  
}
  

# send the monitor program either the passed-in limits,
# or our pre-defined limits for queue size warnings
print "SENDMAIL: adding checks...\n" if $BigSister::common::dl>4;
$mail->add_check(
    $args{messages_yellow} ? $args{messages_yellow} : 300,
    $args{messages_red} ? $args{messages_red} : 500,
    "$args{alias}." . ($args{check}?$args{check}:'mail'));
print "SENDMAIL: ...done.\n" if $BigSister::common::dl>4;

print "SENDMAIL: Config done.\n" if $BigSister::common::dl>3;
