#!/bin/sh

# ClonePanel - Manages duplicate accounts on two or more webservers,
# including snapshot backups, monitoring and failover dns.
# Copyright (C)2006 Chris Cheers, Internet Lynx.
# Contact chris[at]clonepanel[dot]com.
# Internet Lynx, PO Box 7117, Mannering Park, NSW 2259, Australia
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Version=0.33
# log_error -h for built-in documentation

set -u
# Be strict about variable declaration

unset PATH
#avoid use of $PATH - limit script to system commands we choose

PROGRAM_DIR=$(/bin/echo $0 |/bin/sed -e "s/\/[a-zA-Z0-9_]*$//")
# Extract the working directory from command line
# NB - if your system doesn't have echo and sed in these locations
# then this line will need to be changed in all shell scripts

# Standard include files:
. $PROGRAM_DIR/includes


# Built-in documentation:
function showdocs {
	$CAT <<-EODOC
		Part of monitoring system - not intended to be run interactively
		
		Usage:
		./log_error file score hostname
		file = Filename (in system/errors) to append error report onto
		score = Severity of error 1 - 1000
		hostname may be prefixed with m_h_ (classname)
EODOC
	exit $DOC_REQUEST
}

if [ "$1" == "-h" ]; then
	showdocs
fi

# Start main program

MAIL=/bin/mail
# Not included with other commands - don't want this enabled by default



file=$1
# Filename to write error report to
score=$2
# Score (1-1000) that triggered this error
plist=($*)
unset plist[0]
# Full list of input params (excluding file a above)

if [ -s "$ERROR_IGNORE_FILE" ]; then
# Ignore errors if they match criteria in error ignore file
	while read erig_host erig_mode erig_score
	do
		if [ ! "$erig_score" ]; then
			continue
		fi
		match=`$ECHO ${plist[*]} |$GREP "m_h_$erig_host" |$GREP "m_$erig_mode"`
# Does the error match the given criteria?
		if [ ! "$match" ]; then
			continue
		fi
		if [ $score -le $erig_score ]; then
# Criteria match, score less than permitted max - quit this script here...
			$ECHO "Warning: Ignoring error on $erig_host - $erig_mode up to $erig_score"
			exit 0
		fi
		break
# If we're here, the error is too severe to ignore - don't bother checking
# other criteria
	done < $ERROR_IGNORE_FILE	
fi

# Unless in ignore list, always log error to file
if [ ! -d $ERROR_LOG_DIR ]; then
	create_dir $ERROR_LOG_DIR
fi

$ECHO "${plist[*]}" >>$ERROR_LOG_DIR/$file

host=`$ECHO $3 |$SED -e "s/m_h_//"`
config -H $host

emessage="Error report: ${plist[*]}"
written=''

if [ "$WRITE_STOPS_EMAIL_ON_ERROR" == "y" ]; then
	write_stops=y
else
	write_stops=''
fi

# Should we write an alert?
if [ "$WRITE_ON_ERROR" == "y" ]; then
	me=`$WHOAMI`
	if [ "`$WHO |$GREP $me`" ]; then
# I am online!
		$ECHO $emessage |$WRITE $me
		written=y
	fi
fi

# Do we have an e-mail address for error reports?
if [ "$ERROR_REPORT_EMAIL" ]; then
# And does host setup require it?
	if [ "$REPORT_ERRORS_BY_EMAIL" == "y" ]; then
# And (finally) do we suppress it if already written as a console alert?
		if [ "$written" -a "$write_stops" ]; then
			cecho -c $warning "Email suppressed - alert written to console"
		else
			$CAT |$MAIL -s "CPerror $2 $host $5" $ERROR_REPORT_EMAIL <<END_OF_MESSAGE
$emessage
Logged to file $file
END_OF_MESSAGE
		fi
	fi
fi

# Finally, run error manager to handle prolonged issues
$PROGRAM_DIR/$ERROR_MANAGER_SCRIPT
