#!/usr/bin/perl
#
# MBProbe_warning.pl
#
# AUTHOR:
#   Dan Harkless                                                    <opensource@
#   http://harkless.org/dan/software/                              harkless.org>
#
# COPYRIGHT:
#   This file is Copyright (C) 2002 by Dan Harkless, and is released under the
#   GNU General Public License <http://www.gnu.org/copyleft/gpl.html>.
#
# DESCRIPTION:
#   A Perl script to be called from MBProbe <http://mbprobe.livewiredev.com/>
#   when measurements go into the Warning zone.  Emails you the first and last
#   few lines from MBProbe's Event and History Logs.
#
#   Requires Perl (I have only tested with the version of Perl that comes with 
#   Cygwin <http://sources.redhat.com/cygwin/>, but it should work fine with 
#   ActiveState Perl as well) and the Net::SMTP module (available from CPAN).  
#   You must associate the .pl extension with the perl.exe executable.
#
#   You also must customize the mail server and email addresses below.  If you
#   don't use a real domain name on the From address, the SMTP server may refuse
#   your mail.  If you aren't getting your mail as expected, try setting 
#   $debug_SMTP_session to 1 to have the script show the SMTP transaction and
#   pause for user input before its console window goes away.
#
# DATE        MODIFICATION
# ==========  ==================================================================
# 2002-12-14  With current Cygwin, get "domainname: command not found" from 
#             Net::Domain.  Set the name to use on the HELO ourselves to prevent
#             this, and because the automatically-determined name will be bogus
#             if we're a non-Internet-accessible machine behind a firewall.
# 2002-08-19  Original.


## Variables requiring customization ###########################################
$smtp_server = 'mailserver.yourdomain.tld';
$to_address = 'to@yourdomain.tld';
$from_address = 'from@yourdomain.tld';


## Optionally customizable variables ###########################################
$debug_SMTP_session = 0;

$event_head_lines = 2;
$event_tail_lines = 10;
$history_head_lines = 4;
$history_tail_lines = 10;

# This is what we'll bill ourselves as in the HELO/EHLO command.  Ideally it
# should be the real DNS name of the IP address that the mail server will see,
# whether that's this PC's IP address or that of an intervening firewall.  Some
# mail servers might be set to be paranoid and reject mail if the HELO name 
# doesn't check out.
$hello = 'localhost';


## Modules used ################################################################
use Net::SMTP;


## Subroutines #################################################################
sub head_and_tail {
    my $log_filename = shift;
    my $head_lines = shift;
    my $tail_lines = shift;

    open(LOG, $log_filename);

    my $number_of_lines = 0;

    while (<LOG>) {
        $number_of_lines++;
    }

    seek(LOG, 0, 0);
    my $current_line = 1;

    while (<LOG>) {
        if ($current_line <= $head_lines or
            $number_of_lines - $current_line < $tail_lines) {
	    $smtp->datasend($_);
        }
        if ($current_line == $head_lines and
            $number_of_lines > $head_lines + $tail_lines) {
    	    $smtp->datasend("(...)\n");
        }
        $current_line++;
    }
}


## Main ########################################################################
$smtp = Net::SMTP->new($smtp_server, Debug=>$debug_SMTP_session, Hello=>$hello);

$smtp->mail($from_address);
$smtp->to($to_address);
$smtp->data();
$smtp->datasend("To: $to_address\n");
$smtp->datasend("From: $from_address\n");
$smtp->datasend("Subject: MBProbe Warning\n");
$smtp->datasend("\n");

head_and_tail("MBProbeEventLog.txt", $event_head_lines, $event_tail_lines);
$smtp->datasend("\n\n");
head_and_tail("MBProbeHistoryLog.txt", $history_head_lines, 
              $history_tail_lines);

$smtp->dataend();
$smtp->quit;

if ($debug_SMTP_session) {
    print "\nHit Enter to close this window.";
    $dummy = <STDIN>;
}
