: # -*- perl -*-
eval "exec perl -S $0 $*"
if $running_under_some_shell;

#=====================================================================
# errreporter - mails interesting entries from the AIX error log
#=====================================================================

use strict;
use File::Basename;
use Getopt::Std;

# declare vars in the conf file ...

my ($email, @ignore);

# declare some other vars used below ...

my ($identifier, $timestamp, $resource, $description, @error);

my $me = basename($0);
chomp (my $hostname = `/usr/bin/hostname`);

my @usage_string = <<ENDOFUSAGESTRING;
Usage: $me [-hul] [-f file]
Run "$me -h" for additional information.
ENDOFUSAGESTRING

my %opts;

getopts('hulf:', \%opts) or die @usage_string;

if ($opts{h} || $opts{u})  { exec "perldoc $0" };

if ($opts{l})              { exec '/usr/bin/errpt', '-t'
                               or die "$me: couldn't exec 'errpt -t'\n" };  

my $conf = $opts{f} || "/etc/errreporter.conf";

eval `/usr/bin/cat $conf 2> /dev/null` 
  or die "$me: couldn't read specified conf file $conf\n";

unless (defined ($email)) { die "$me: email not defined " }

# add the errpt header line to the ignore list ...

push (@ignore, "IDENTIFIER");

# get the current time, in the form used by the errpt command ...

my $starttime = sprintf '%02d%02d%02d%02d%02d',
  sub { $_[4]+1, $_[3], $_[2], $_[1], $_[5] % 100 } -> (localtime);

print STDOUT "$me: starting errreporter at $starttime, using conf file $conf.\n";

# open an errpt process, reading the summary of errors posted
# after the start time of this script ...

open (ERRPT, "/usr/bin/errpt -c -s $starttime |") or
  die "$me: couldn't open errpt for reading!\n";

# while the above process is open ...

while (<ERRPT>) {

    chomp;

    # split the errpt entry into fields ...

    ($identifier, $timestamp, undef, undef, $resource, $description) =
      split (/\s+/, $_, 6);

    my $ignore = 'no';

    foreach (@ignore) {

        # check to see if the entry is on the ignore list ...

        if ($identifier =~ /$_/) { $ignore = 'yes' ; last }

    };

    unless  ($ignore eq 'yes') {

        # store the full entry in the array @error ...

        @error = `/usr/bin/errpt -a -s $timestamp`;

        # add a subject line to the @error array ...

        unshift (@error, "Subject: $hostname -- $resource -- $description\n");

        #  and send the e-mail ...

        open (SENDMAIL, "|/usr/sbin/sendmail $email") or
          die "$me: couldn't open sendmail: $!";

        print SENDMAIL @error;

        close (SENDMAIL);

    };

};

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

=pod

=head1 NAME

errreporter - e-mails interesting entries from the AIX error log

=head1 SYNOPSIS

B<errreporter> [B<-h | -u>] 

B<errreporter> [B<-l>]

B<errreporter> B<-f> conffile

=head1 DESCRIPTION

I<errreporter> monitors the AIX error log, checks if new error entries 
are listed in a configurable "ignore" list, and e-mails the error in
detailed format to a specified address.

-head1 OPTIONS

=over 5

=item B<-h | -u>

Execs "perldoc" on the I<errreporter> program, displaying this documentation.

=item B<-l>

Execs "errpt -t", which displays the error template summaries.  This is useful
when creating the "ignore" list of errors that you don't want to receive e-mail for.

=item B<-f conffile>

Uses the specified file as the configuration file.  If this flag is not specified,
the default of I<"/etc/errreporter.conf"> is used.

=back

=head1 CONFIGURATION

I<errreporter> uses a configuration file for the setting of two options: the e-mail
address to mail errors to, and the list of errors that should be ignored.  Because
the configuration file is "sourced" by the I<errreporter> program, it must follow
proper perl syntax.  The two options are:

=over 5

=item B<$email = "you\@your.address";>

Set the variable $email to the the email address that will receive the error log
entries.  Don't forget to backslash the at-sign, and end the line with a semi-colon.

=item B<@ignore = ("IDENTIFIER1", "IDENTIFIER2", ...);>

Set the array variable @ignore to a comma-separated quoted list of error identifiers
that I<errreporter> should not send mail for. 

=back

=head1 AUTHOR

Sandor W. Sklar
ssklar@stanford.edu

=cut
