#!/bin/sh
# $Header: mail_bp_reports.sh,v 1.2.24.1 2004/01/15 15:39:10 $
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# mail_bp_reports
#
# This script can be used by an adminstrator as a convenient way to mail
# the various NetBackup reports.  It may be used in a crontab entry to 
# have status mailed to you once per day.
#
# some caveats:
#   -assumes binaries, scripts are located in the right places
#	bperror
#  	cleanstats
#  	mail
#   -not tried on all servers, some platforms may need slight modifications

# mail_cmd - find a mail binary and send a message
#
# mail_cmd subject user

mail_cmd () {
	mail_subject="$1"
	mail_user="$2"
	mail_command=''

	# Searching in this order appears to find a command that supports
	# the "-s" argument on all of our UNIX server platforms.

	for mail_base in mailx Mail mail
	do
		for mail_dir in /usr/bin /usr/ucb /usr/sbin /bin /sbin
		do
			if [ -x "$mail_dir/$mail_base" ]
			then
				mail_command="$mail_dir/$mail_base"
				break
			fi
		done
		if [ "$mail_command" != '' ]
		then
			break
		fi
	done
	if [ "$mail_command" = '' ]
	then
		echo "No mail command on this platform" >&2
		return 1
	fi
	"$mail_command" -s "$mail_subject" "$mail_user"
}
		

# This is the command used to get the backup status and problem reports.
BPERROR=/usr/openv/netbackup/bin/admincmd/bperror

BPIMAGELIST=/usr/openv/netbackup/bin/admincmd/bpimagelist

if [ -f /usr/openv/netbackup/version ] ; then
	HARDWARE=`head -1 /usr/openv/netbackup/version | cut -f2 -d" "`
else
	echo "/usr/openv/netbackup/version not found"
	exit 1
fi

/usr/openv/netbackup/bin/admincmd/bpauthorize -test_admin >/dev/null
status=$?
if [ ${status} -ne 0 ] ; then
	echo ""
	echo "You do not have the proper authorization to perform"
	echo "this task.  Please resolve and try again."
	exit ${status}
fi

# HOURS is the number of hours ago the reports should begin.
HOURS=24

OUTFILE=/tmp/bpstat
OUTFILE1=/tmp/bpstat.1
OUTFILE2=/tmp/bpstat.2

/bin/rm -rf $OUTFILE $OUTFILE1 $OUTFILE2
touch  $OUTFILE $OUTFILE1 $OUTFILE2

# -------------------------------------
echo "
" >> $OUTFILE
$BPERROR -U -backstat -hoursago $HOURS 1>>$OUTFILE 2>&1
echo "
 Recently Used Media:
" >> $OUTFILE
$BPIMAGELIST -A -media -hoursago $HOURS 1>>$OUTFILE 2>&1

cat $OUTFILE | mail_cmd "NetBackup backup status" root

# -------------------------------------
echo "
" >> $OUTFILE1
$BPERROR -U -problems -hoursago $HOURS 1>>$OUTFILE1 2>&1

cat $OUTFILE1 | mail_cmd "NetBackup problems log" root

# -------------------------------------
echo "
" >> $OUTFILE2
/usr/openv/netbackup/bin/goodies/cleanstats 1>>$OUTFILE2 2>&1

cat $OUTFILE2 | mail_cmd "NetBackup cleaning stats" root

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

/bin/rm -rf $OUTFILE $OUTFILE1 $OUTFILE2
