#! /bin/sh
#
#	$Id: amverify.sh.in,v 1.7.2.13 2000/11/01 21:09:22 jrjackson Exp $
#
# (C) 1996 by ICEM Systems GmbH
# Author: Axel Zinser (fifi@icem.de)
#
# amverify: check amanda tapes and report errors
#

prefix=/usr/local
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
libexecdir=/usr/local/libexec/amanda

PATH=$sbindir:$libexecdir:/usr/bin:/bin:/usr/sbin:/sbin:/usr/ucb
export PATH

USE_VERSION_SUFFIXES="no"
if [ "$USE_VERSION_SUFFIXES" = "yes" ]; then
	SUF="-2.4.2p2"
else
	SUF=
fi

# If the shell/system echo support \r and \c, use them to write some
# status messages over the top of each other as things progress, otherwise
# use a normal echo and let them go on new lines.  Define $Echoe to be
# an echo that goes to stderr.  In the code, $Echoe is used and it may
# be either echoe or echone, the latter being code that knows about echon.

t=`echo "abc\r\c" | wc -c`
if [ $t -eq 4 ]; then
	Echon=echon
else
	Echon=echo
fi
Echoe=echoe
elen=0
echoe() {
	echo "$@" >&2
	Echoe=echoe
}
echon() {
        newelen=`expr "$1" : '.*'`
	blanks=
        while [ $newelen -lt $elen ]; do
		blanks="$blanks "
                elen=`expr $elen - 1`
        done
        echo "$1""$blanks\r\c"
        elen=$newelen
	Echoe=echone
}
echone() {
	echon
	echoe "$@"
	Echoe=echoe
}

report() {
	$Echoe "$@"
	echo "$@" >> $REPORT
}

getparm() {
	$AMGETCONF $CONFIG $1 2>/dev/null | grep -v BUGGY
}

sendreport() {
	if [ -f $REPORT -a X"$REPORTTO" != X"" ]; then
		(
		echo "Tapes: $TAPELIST"
		if [ -s $DEFECTS ]; then
			echo "Errors found: "
			cat $DEFECTS
		else
			echo "No errors found!"
		fi
		echo

		[ -s $REPORT ] \
			&& cat $REPORT
		) | $MAIL -s "$ORG AMANDA VERIFY REPORT FOR$TAPELIST" $REPORTTO
	fi
}

###
# This function is called to process one dump image.  Standard input is
# the dump image.  We parse the header and decide if it is a GNU tar
# dump or a system dump.  Then we do a catalog operation to /dev/null
# and finally a "cat" to /dev/null to soak up whatever data is still in
# the pipeline.
#
# In the case of a system restore catalogue, this does not fully check
# the integrity of the dump image because system restore programs stop
# as soon as they are done with the directories, which are all at the
# beginning.  But the trailing cat will at least make sure the whole
# image is readable.
###

doonefile() {

	###
	# The goal here is to collect the first 32 KBytes and save the
	# first line.  But the pipe size coming in to us from amrestore
	# is highly system dependent and "dd" does not do reblocking.
	# So we pick a block size that is likely to always be available in
	# the pipe and a count to take it up to 32 KBytes.  Worst case,
	# this could be changed to "bs=1 count=32k".  We also have to
	# soak up the rest of the output after the "head" so an EPIPE
	# does not go back and terminate the "dd" early.
	###

	HEADER=`$DD bs=512 count=64 | ( sed 1q ; cat > /dev/null )`
	CMD=
	result=1
	if [ X"$HEADER" = X"" ]; then
		echo "** No header" > $TEMP/errors
	else
		set X $HEADER
		shift
		shift 9
		if [ X"$1" = X"program" -a X"$2" != X"" ]; then
			if [ X"$TAR" != X"" -a X"$2" = X"$TAR" ]; then
				CMD=$TAR
				ARGS="tf -"
			elif [ X"$TAR" != X"" \
			       -a X"$SAMBA_CLIENT" != X"" \
			       -a X"$2" = X"$SAMBA_CLIENT" ]; then
				CMD=$TAR
				ARGS="tf -"
			elif [ X"$TAR" != X"" \
			       -a X`basename $2` = X`basename $TAR` ]; then
				CMD=$TAR
				ARGS="tf -"
			elif [ X"$DUMP" != X"" -a X"$2" = X"$DUMP" ]; then
				CMD=$RESTORE
				if [ $IS_AIX -eq 1 ]; then
					ARGS=-tB
				else
					ARGS="tbf 2 -"
				fi
			elif [ X"$VDUMP" != X"" -a X"$2" = X"$VDUMP" ]; then
				CMD=$VRESTORE
				ARGS="tbf 2 -"
			elif [ X"$VXDUMP" != X"" -a X"$2" = X"$VXDUMP" ]; then
				CMD=$VXRESTORE
				ARGS="tbf 2 -"
			elif [ X"$XFSDUMP" != X"" -a X"$2" = X"$XFSDUMP" ]; then
				CMD=$XFSRESTORE
				ARGS="-t -v silent -"
			else
				echo "** Cannot do $2 dumps" > $TEMP/errors
				result=999	# flag as not really an error
			fi
		else
			echo "** Cannot find dump type" > $TEMP/errors
		fi
	fi
	if [ X"$CMD" != X"" ]; then
		if [ -x $CMD ]; then
			$CMD $ARGS > /dev/null 2> $TEMP/errors
			result=$?
		else
			echo "** Cannot execute $CMD" > $TEMP/errors
		fi
	fi
	cat >/dev/null				# soak up the rest of the image
	echo $result
}

#
# some paths
#
#	CONFIG_DIR	directory in which the config file resides
#	AMRESTORE	full path name of amrestore
#	AMGETCONF	full path name of amgetconf
#	AMTAPE		full path name of amtape
#	TAR		ditto for GNU-tar
#	SAMBA_CLIENT	ditto for smbclient
#	DUMP		ditto for the system dump program
#	RESTORE		ditto for the system restore program
#	VDUMP		ditto for the system dump program
#	VRESTORE	ditto for the system restore program
#	VXDUMP		ditto for the system dump program
#	VXRESTORE	ditto for the system restore program
#	XFSDUMP		ditto for the system dump program
#	XFSRESTORE	ditto for the system restore program
#	DD		ditto for dd
#	MT		ditto for mt
#	MTOPT		flag given to MT to specify tape device: -f or -t
#	MAIL		mail program
#	IS_AIX		true if this is an AIX system

CONFIG_DIR=/usr/local/etc/amanda
libexecdir=$libexecdir
sbindir=$sbindir
AMRESTORE=$sbindir/amrestore$SUF
AMGETCONF=$sbindir/amgetconf$SUF
AMTAPE=$sbindir/amtape$SUF
TAR=/usr/local/bin/gtar
SAMBA_CLIENT=
DUMP=/sbin/dump
RESTORE=/sbin/restore
VDUMP=
VRESTORE=
VXDUMP=
VXRESTORE=
XFSDUMP=
XFSRESTORE=
DD=/bin/dd
MT=mt
MTOPT=-f
MAIL=/usr/local/bin/mailx
if [ X"`/bin/uname -s 2>/dev/null`" = X"AIX" ]; then
	IS_AIX=1

	# The AIX "mt stat" function does not really do anything w.r.t.
	# checking the drive for ready, and in fact, will fail under
	# some conditions (e.g. if the tape "file" is a symlink to the
	# real device).  We let the rewind do the equivalent since all
	# we use this for is to wait for device ready.

	DEVICE_READY=:
else
	IS_AIX=0
	DEVICE_READY='$MT $MTOPT $DEVICE stat'
fi

#
# config file
#
SLOT=0
CONFIG=$1
[ X"$CONFIG" = X"" ] \
	&& $Echoe "usage: amverify$SUF <config> [slot]" \
	&& exit 1

AMCONFIG=$CONFIG_DIR/$CONFIG/amanda.conf
[ ! -f $AMCONFIG ] \
	&& $Echoe "Cannot find config file $AMCONFIG" \
	&& exit 1

cd $CONFIG_DIR/$CONFIG

TPCHANGER=`getparm tpchanger`
if [ X"$TPCHANGER" = X"" ]; then
	$Echoe "No tape changer..."
	DEVICE=`getparm tapedev`
	[ X"$DEVICE" = X"" ] \
		&& $Echoe "No tape device..." \
		&& exit 1
	[ ! -c $DEVICE ] \
		&& $Echoe "Not a character special device: $DEVICE" \
		&& exit 1
	$Echoe "Tape device is $DEVICE..."
	SLOTS=1
else
	CHANGER_SLOT=${2:-current}
	$Echoe "Tape changer is $TPCHANGER..."
	SLOTS=`getparm runtapes`
	[ X"$SLOTS" = X"" ] && SLOTS=1
	if [ $SLOTS -eq 1 ]; then
		p=""
	else
		p=s
	fi
	$Echoe "$SLOTS slot${p}..."
	MAXRETRIES=2
fi

#
# check the accessability
#
[ X"$TAR" != X"" -a ! -x "$TAR" ] \
	&& $Echoe "GNU tar not found: $TAR"
[ X"$DUMP" != X"" -a \( X"$RESTORE" = X"" -o ! -x "$RESTORE" \) ] \
	&& $Echoe "System restore program not found: $RESTORE"
[ X"$VDUMP" != X"" -a \( X"$VRESTORE" = X"" -o ! -x "$VRESTORE" \) ] \
	&& $Echoe "System restore program not found: $VRESTORE"
[ X"$VXDUMP" != X"" -a \( X"$VXRESTORE" = X"" -o ! -x "$VXRESTORE" \) ] \
	&& $Echoe "System restore program not found: $VXRESTORE"
[ X"$XFSDUMP" != X"" -a \( X"$XFSRESTORE" = X"" -o ! -x "$XFSRESTORE" \) ] \
	&& $Echoe "System restore program not found: $XFSRESTORE"
[ ! -x $AMRESTORE ] \
	&& $Echoe "amrestore not found: $AMRESTORE" \
	&& exit 1

REPORTTO=`getparm mailto`
if [ X"$REPORTTO" = X"" ]; then
	$Echoe "No notification by mail!"
else
	$Echoe "Verify summary to $REPORTTO"
fi

ORG=`getparm org`
if [ X"$ORG" = X"" ]; then
	$Echoe "No org in amanda.conf -- using $CONFIG"
	ORG=$CONFIG
fi

#
# ok, let's do it
#
#	TEMP		directory for temporary tar archives and stderr
#	DEFECTS		defect list
#	REPORT		report for mail

TEMP=/tmp/amanda/amverify.$$
trap 'rm -fr $TEMP' 0
if ( umask 077 ; mkdir $TEMP ) ; then
	:
else
	$Echoe "Cannot create $TEMP"
	exit 1
fi
DEFECTS=$TEMP/defects; rm -f $DEFECTS
REPORT=$TEMP/report; rm -f $REPORT
TAPELIST=
EXITSTAT=$TEMP/amrecover.exit; rm -rf $EXITSTAT

trap 'report "aborted!"; echo "aborted!" >> $DEFECTS; sendreport; rm -fr $TEMP; exit 1' 1 2 3 4 5 6 7 8 10 12 13 14 15

$Echoe "Defects file is $DEFECTS"
report "amverify $CONFIG"
report "`date`"
report ""

# ----------------------------------------------------------------------------

while [ $SLOT -lt $SLOTS ]; do
	SLOT=`expr $SLOT + 1`
	#
	# Tape Changer: dial slot
	#
	if [ X"$TPCHANGER" != X"" ]; then
		report "Loading ${CHANGER_SLOT} slot..."
		$AMTAPE $CONFIG slot $CHANGER_SLOT > $TEMP/amtape.out 2>&1
		THIS_SLOT=$CHANGER_SLOT
		CHANGER_SLOT=next
		RESULT=`grep "changed to slot" $TEMP/amtape.out`
		[ X"$RESULT" = X"" ] \
			&& report "** Error loading slot $THIS_SLOT" \
			&& report "`cat $TEMP/amtape.out`" \
			&& cat $TEMP/amtape.out >> $DEFECTS \
			&& continue
		DEVICE=`$AMTAPE $CONFIG device`
	fi
	report "Using device $DEVICE"
	$Echon "Waiting for device to go ready..."
	until eval $DEVICE_READY >/dev/null 2>&1; do
		sleep 3
	done
	$Echon "Rewinding..."
	until $MT $MTOPT $DEVICE rewind; do
		sleep 3
	done
	$Echon "Processing label..."
	$DD if=$DEVICE count=1 bs=32k 2> $TEMP/errors > $TEMP/header
	[ ! -s $TEMP/header ] \
		&& report "** Error reading label on tape" \
		&& cat $TEMP/errors >> $DEFECTS \
		&& continue
	TAPENDATE=`sed 1q < $TEMP/header | grep '^AMANDA: TAPESTART'`
	[ X"$TAPENDATE" = X"" ] \
		&& report "** No amanda tape in slot" \
		&& continue
	set X $TAPENDATE
	shift
	shift			# "AMANDA:"
	shift			# "TAPESTART"
	VOLUME=$4
	DWRITTEN=$2
	report "Volume $VOLUME, Date $DWRITTEN"
	[ X"$DWRITTEN" = X"0" -o X"$DWRITTEN" = X"X" ] \
		&& report "Fresh tape. Skipping..." \
		&& continue
	TAPELIST="$TAPELIST $VOLUME"
	$Echon "Rewinding..."
	until $MT $MTOPT $DEVICE rewind; do
		sleep 3
	done
	ERG=0
	ERRORS=0
	while [ $ERG = 0 ]; do
		if [ $Echon = echon ]; then
			$Echon "Waiting for device to go ready..."
		fi
		until eval $DEVICE_READY >/dev/null 2>&1; do
			sleep 3
		done
		if [ $Echon = echon ]; then
			$Echon "Reading..."
		fi
		RESULT=`$AMRESTORE -h -p $DEVICE 2> $TEMP/amrestore.out \
			| doonefile 2> $TEMP/onefile.errors`
		FILE=`grep restoring $TEMP/amrestore.out \
			| sed 's/^.*restoring //'`
		EOF=`grep "reached end of tape" $TEMP/amrestore.out`
		# amrestore:   0: restoring sundae._mnt_sol1_usr.19961127.1
		if [ X"$FILE" != X"" -a X"$RESULT" = X"0" ]; then
			report "Checked $FILE"
		elif [ X"$FILE" != X"" -a X"$RESULT" = X"999" ]; then
			report "Skipped $FILE (`cat $TEMP/errors`)"
		elif [ -n "$EOF" ]; then
			report "End-of-Tape detected."
			break
		else
			report "** Error detected ($FILE)"
			echo "$VOLUME ($FILE):" >>$DEFECTS
			[ -s $TEMP/amrestore.out ] \
				&& report "`cat $TEMP/amrestore.out`" \
				&& cat $TEMP/amrestore.out >>$DEFECTS
			[ -s $TEMP/errors ] \
				&& report "`cat $TEMP/errors`" \
				&& cat $TEMP/errors >>$DEFECTS
			[ -s $TEMP/onefile.errors ] \
				&& report "`cat $TEMP/onefile.errors`" \
				&& cat $TEMP/onefile.errors >>$DEFECTS
			ERRORS=`expr $ERRORS + 1`
			[ $ERRORS -gt 5 ] \
				&& report "Too many errors." \
				&& break
		fi
	done
	$Echon "Rewinding..."
	until $MT $MTOPT $DEVICE rewind; do
		sleep 3
	done
	rm -f $TEMP/header \
	      $TEMP/amtape.out \
	      $TEMP/amrestore.out \
	      $TEMP/errors \
	      $TEMP/onefile.errors
done

if [ X"$TPCHANGER" != X"" ]; then
	report "Advancing past the last tape..."
	$AMTAPE $CONFIG slot advance 2> $TEMP/amtape.out
	RESULT=`grep "changed to slot" $TEMP/amtape.out`
	[ X"$RESULT" = X"" ] \
		&& report "** Error advancing after last slot" \
		&& report "`cat $TEMP/amtape.out`" \
		&& cat $TEMP/amtape.out >> $DEFECTS
fi

[ -s $DEFECTS ] \
	&& $Echoe "Errors found: " \
	&& cat $DEFECTS

sendreport

exit 0
