#!/bin/sh
###############################################################################
#   afick_cron 
#      it's a part of the afick project
#
#    Copyright (C) 2002, 2003 by Eric Gerbier
#    Bug reports to: gerbier@users.sourceforge.net
#    $Id: afick_cron 791 2006-02-16 12:43:07Z gerbier $
#
#    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.
#
###############################################################################
# script for cron job 
# this script use the "macro" lines of afick configuration file
# the goals are :
# - set the nice priority
# - truncate too long reports to avoid big mails
# - avoid mails if no changes detected
# - sent report to the specicified email adress
# - write reports to /var/log/afick

AFICK="/usr/bin/afick.pl"
PATH="/bin:/usr/bin"
LOGDIR="/var/log/afick"
LOGFILE="$LOGDIR/afick.log"
ERRORLOG="$LOGDIR/error.log"
CONFFILE="/etc/afick.conf"

###############################################################################
treat_log() {
	if [ -n "$VERBOSE_AFICK" ]
	then
		echo "# This is an automated report generated by Another File Integrity Checker on $FQDN ${DATE}."
	fi

	# "normal" afick output : changes result
	if [ -s $LOGFILE ]; then
		loglines=`wc -l $LOGFILE | awk '{ print $1 }'`
		if [ ${loglines:=0} -gt $LINES ]; then
			echo "# TRUNCATED (!) output of the daily afick run:"
			echo "# Output is $loglines lines, truncated to $LINES."
			head -$LINES $LOGFILE
			echo "# The full output can be found in $LOGFILE."
		else
			echo "# Output of the daily afick run:"
			cat $LOGFILE
		fi
	elif [ -n "$VERBOSE_AFICK" ]
	then
		echo "# afick detected no changes."
	fi

	# afick errors
	if [ -s $ERRORLOG ]; then
		errorlines=`wc -l $ERRORLOG | awk '{ print $1 }'`
		if [ ${errorlines:=0} -gt $LINES ]; then
			echo "# TRUNCATED (!) output of errors produced:"
			echo "# Error output is $errorlines lines, truncated to $LINES."
			head -$LINES $ERRORLOG
			echo "# The full output can be found in $ERRORLOG."
		else
			echo "# Errors produced:"
			cat $ERRORLOG
		fi
	elif [ -n "$VERBOSE_AFICK" ]
	then
		echo "# afick produced no errors."
	fi
}
###############################################################################
macro () {
	key=$1
	grep "^@@define $key" $CONFFILE | head -1 | awk '{ print $3 }'
}
###############################################################################
send_mail() {
	echo "$OUTPUT" | mail -s "[AFICK] Daily report for $FQDN" $MAILTO
}
###############################################################################
# MAIN
###############################################################################

[ -x $AFICK ] || exit 0

FQDN=`hostname -f 2>/dev/null`
DATE=`date +"at %X on %x"`
MAILTO=`macro MAILTO`
LINES=`macro LINES`
VERBOSE=`macro VERBOSE`
REPORT=`macro REPORT`
NICE=`macro NICE`
BATCH=`macro BATCH`

# default values
[ -z "$FQDN" ] && FQDN=`hostname`
[ -z "$MAILTO" ] && MAILTO="root"
[ -z "$LINES" ] && LINES="1000"
[ -z "$VERBOSE" ] && VERBOSE=0
[ -z "$REPORT" ] && REPORT=1
[ -z "$NICE" ] && NICE=15
[ -z "$BATCH" ] && BATCH=1

#echo "MAILTO=$MAILTO LINES=$LINES VERBOSE=$VERBOSE NICE=$NICE BATCH=$BATCH"

if [ "$BATCH" == "0" ]
then
	exit 0
fi

if [ "$VERBOSE" == "1" ]
then
	# verbose mail
	export VERBOSE_AFICK=1
fi

# launch command
nice -n $NICE $AFICK -c $CONFFILE -u >$LOGFILE 2>$ERRORLOG

if [ "$REPORT" == "0" ]
then
	# no report
	exit
fi

# filter output to send by mail
OUTPUT=$( treat_log  )
if [ "$VERBOSE" == "1" ]
then
	send_mail
else
	# skip comments and empty lines
	OUTPUT_FILTRE=$( echo "$OUTPUT" | grep -v "^#" | grep -v "^$"  )
	if [ -n "$OUTPUT_FILTRE" ]
	then
		send_mail
	fi
fi
