> 3. It would be nice to have a configuration option
>     (mon.cf) to set the "From:" and "Reply-To:" headers
>     to some helpful value for administrative e-mail
>     communication.

I usually do this on the mail system for all mails from root or whoever
mon is running under...

Attached is (according to my tastes) a slighly clearer version of the mail
alert.

-- 
Mark Lawrence
[EMAIL PROTECTED]


#!/usr/bin/perl
#
# mail2.alert - Improved Mail alert for mon
#
# The first line from STDIN is summary information, adequate to send
# to a pager or email subject line.
#
# Mark Lawrence, [EMAIL PROTECTED] - based on the original from:
# Jim Trocki, [EMAIL PROTECTED]
#
# $Id: mail.alert 1.1 Sat, 26 Aug 2000 15:22:34 -0400 trockij $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
$RCSID='$Id: mail.alert 1.1 Sat, 26 Aug 2000 15:22:34 -0400 trockij $';


### Extra Perl packages ###

use Getopt::Std;
use Text::Wrap;
use Text::Tabs;


### A function to pretty-print statements of the form "Field: value" ###

$Text::Wrap::columns = 72;
$colwidth = 18;
$fillspec = "\%-${colwidth}s\ ";
$fill     = sprintf($fillspec, " ");

sub wprint {
        my($description, $string) = @_;
        $description = $description . ":";
        return expand wrap ("", $fill, sprintf("${fillspec}\%s",
                                $description, $string)), "\n";
}


### Evaluate command-line options and environment values ###

getopts ("S:s:g:h:t:l:u");

$summary=<STDIN>;
chomp $summary;
$summary = $opt_S if (defined $opt_S);
$mailaddrs = join (',', @ARGV);

$ALERT = $ENV{"MON_ALERTTYPE"};

$firstfailt = $ENV{"MON_FIRST_FAILURE"};
$firstfail  = localtime($firstfailt);

my $sec    = $opt_t - $firstfailt;

my $days   = int($sec/(24*60*60)); 
$days      = $days > 0 ? ($days . " day" . ($days > 1 ? "s " : " ")) : "";

my $hours  = ($sec/(60*60))%24;
$hours     = $hours > 0 ? ($hours . " hour" . ($hours > 1 ? "s " : " ")) : "";

my $mins   = ($sec/60)%60;
$mins      = $mins > 0 ? ($mins . " min" . ($mins > 1 ? "s " : " ")) : "";

my $secs   = $sec%60;
$secs      = $secs > 0 ? ($secs . " sec" . ($secs > 1 ? "s " : " ")) : "";

my $length = $days . $hours . $mins . $secs;

if ($sec == 0) {
        $length = "0";
}


$t = localtime($opt_t);
($wday,$mon,$day,$tm) = split (/\s+/, $t);

$subject = uc($ALERT) . ": $opt_g $opt_s ($day $mon $tm): $summary";


### Create the email ###

open (MAIL, "| /usr/sbin/sendmail -oi -t") ||
    die "could not open pipe to mail: $!\n";

print MAIL <<EOF;
To: $mailaddrs
Subject: $subject
X-Mailer: $0

EOF

# This is missing somewhere
# Next alert in : $opt_l

print MAIL wprint("Notice Summary", $summary);
print MAIL wprint("Notice Date", $t);

if ($opt_g ne $opt_h) {
        print MAIL wprint("Group", $opt_g);
} else {
        print MAIL wprint("Device", $opt_g);
}

if ($ENV{MON_DESCRIPTION} ne "") {
        print MAIL wprint("Description", $ENV{MON_DESCRIPTION});
}

print MAIL wprint("Service", $opt_s);

if ($opt_g ne $opt_h) {
        print MAIL wprint("Affected Members", $opt_h);
}

print MAIL wprint("Failure detected", $firstfail);

if ($opt_u) {
        print MAIL wprint("Restore detected", $t);
        print MAIL wprint("Length of Failure", $length);
} else {
        print MAIL wprint("Current downtime", $length);
}

print MAIL "\n(Detailed output follows, if any)\n\n";

#
# The remaining lines normally contain more detailed information,
# but this is monitor-dependent.
#
while (<STDIN>) {
    print MAIL;
}

close (MAIL);

_______________________________________________
mon mailing list
[EMAIL PROTECTED]
http://linux.kernel.org/mailman/listinfo/mon

Reply via email to