#! /bin/sh -e

# $Id: lr_check_errlines.in,v 1.3 2002/08/18 19:10:04 flacoste Exp $

#
# Copyright (C) 2002 Stichting LogReport Foundation LogReport@LogReport.org
# 
#     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 (see COPYING); if not, check with
#     http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
#     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
#

PROGRAM=lr_check_errlines

tag="${LR_SUPERSERVICE:-UNSET} ${LR_SERVICE:-UNSET} ${LR_ID:-UNSET} $PROGRAM"
echo >&2 "$tag info started"

if test -z "$LR_SERVICE"
then
    echo >&2 "$tag err LR_SERVICE is unset"
    exit 1
fi

LR_SUPERSERVICE=`lr_check_superservice $LR_SERVICE`
if test -z "$LR_SUPERSERVICE"
then
    echo >&2 "$tag err lr_check_superservice failed"
    exit 1
fi

if test -z "$LR_ID"
then
    echo >&2 "$tag err LR_ID is unset"
    exit 1
fi

#
# Check the percentage of lines that failed
# max value for error lines / log lines.
# When there is more then 5% error rate, we suspect there is a bogus log file
MAXERRORRATE="0.05"

tmpfile=$TMPDIR/$PROGRAM.$LR_SERVICE.$LR_ID.lines
if lr_db_fetch $LR_ID errorlines > $tmpfile
then
    ERRORLINES=`cat $tmpfile`
    rm $tmpfile
else
    echo >&2 "$tag crit lr_db_fetch $LR_ID errorlines failed, exiting"
    echo >&2 "$tag notice keeping $tmpfile for debug"
    exit 1
fi
if lr_db_fetch $LR_ID dlflines > $tmpfile
then
    DLFLINES=`cat $tmpfile`
    rm $tmpfile
else
    echo >&2 "$tag crit lr_db_fetch $LR_ID dlflines failed, exiting"
    echo >&2 "$tag notice keeping $tmpfile for debug"
    exit 1
fi
if lr_db_fetch $LR_ID loglines > $tmpfile
then
    LOGLINES=`cat $tmpfile`
    rm $tmpfile
else
    echo >&2 "$tag crit lr_db_fetch $LR_ID loglines failed, exiting"
    echo >&2 "$tag notice keeping $tmpfile for debug"
    exit 1
fi

if test -z "$LR_ARCHIVE"
then
    # we can purge loglines, dlflines and errorlines from the archive now
    if lr_db_purge $LR_ID dlflines $DLFLINES
    then
        :
    else
        echo >&2 "$tag crit lr_db_purge $LR_ID dlflines $DLFLINES failed, exiting"
        echo >&2 "$tag notice keeping $tmpfile for debug"
        exit 1
    fi
    if lr_db_purge $LR_ID errorlines $ERRORLINES
    then
        :
    else
        echo >&2 "$tag crit lr_db_purge $LR_ID errorlines $ERRORLINES failed, exiting"
        echo >&2 "$tag notice keeping $tmpfile for debug"
        exit 1
    fi
    if lr_db_purge $LR_ID loglines $LOGLINES
    then
        :
    else
        echo >&2 "$tag crit lr_db_purge $LR_ID loglines $LOGLINES failed, exiting"
        echo >&2 "$tag notice keeping $tmpfile for debug"
        exit 1
    fi
fi

if test 0 -lt "$LOGLINES"
then
    OK=`/usr/bin/perl -e "print \"1\\n\" if $ERRORLINES / $LOGLINES < $MAXERRORRATE"`
else
    # Empty log file, this is not an error 
    OK=1
fi

cat <<EOF
${PROGRAM}_ERRORLINES=$ERRORLINES
${PROGRAM}_DLFLINES=$DLFLINES
${PROGRAM}_LOGLINES=$LOGLINES
${PROGRAM}_OK=$OK
EOF

if test 1 != "$OK"
then
    echo >&2 "$tag warning more than 5% error rate"
fi

echo >&2 "$tag info stopped"

exit 0

POD=<<'EOFPOD'
=pod

=head1 NAME

lr_archive_log - internal script that handles archiving log and report files

=head1 SYNOPSIS

B<lr_archive_log> B<[-l> I<logfile>B<]> B<[-l> I<logfile>B<]> ... B<[-x> I<xmlreportfile>B<]> B<[-x> I<xmlreportfile>B<]> ...

=head1 DESCRIPTION

B<lr_archive_log> is an internal script used by lr_log2report(1) and
lr_log2mail(1). It should be called at the end of the job to archive (or not)
the generated XML report and log file.

This script will keep, remove or archive the log and XML report
files depending on the value of I<LR_KEEP> and I<LR_ARCHIVE>.

When I<LR_ARCHIVE> is set, a compressed copy of the log and XML report
file will be stored in Lire's archive. The XML report will be
saved under F<LR_ARCHIVE_DIR/report/xml/LR_SUPERSERVICE/LR_SERVICE/LR_EXTID>
and the report will be saved under
F<LR_ARCHIVE_DIR/log/raw/LR_SUPERSERVICE/LR_SERVICE/LR_EXTID>. The filenames
will be I<LR_TIME>.gz and I<LR_TIME>.xml.gz respectively.

The log and XML report files will be removed unless I<LR_KEEP> is set.

=head1 ENVIRONMENT

The following environment variables must set and exported prior to
calling this script: I<LR_SERVICE> and I<LR_ID>.
Also, the F<defaults> file should have been sourced.

=head1 VERSION

This manpage: $Id: lr_check_errlines.in,v 1.3 2002/08/18 19:10:04 flacoste Exp $

=head1 COPYRIGHT

Copyright (C) 2002 Stichting LogReport Foundation LogReport@LogReport.org
 
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 (see COPYING); if not, check with
http://www.gnu.org/copyleft/gpl.html or write to the Free Software 
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.

=head1 AUTHOR

Francis J. Lacoste <flacoste@logreport.org>

=cut

EOFPOD

