#!/bin/sh
# $Header: check_coverage.sh,v 1.6.16.1 2004/01/15 15:39:09 $
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# check_coverage [-coverage] [-hardware] [-hosts [local_hosts_file]] [-client <name>] [-mailid <id>]
#
# This script can produce three reports which gather various types of 
# information about the clients being backed up by the server.  The reports
# are:
#        File System Backup Coverage Report
#        Policy Hardware/OS Report
#        Hosts Report
# 
# By default all three reports are generated.
#
# The File System Backup Coverage Report detects file systems that are not
# backed up by any policy and file systems that are backed up by more than
# 1 policy.  Currently, this report is supported for UNIX, Windows
# NT/2000/XP/NET/95/98/Me, and PC-IA64 Windows XP/NET clients.
#
# The Policy Hardware/OS Report displays Hardware and Operating System for
# clients.
#
# The Hosts Report indicates which hosts are in a NetBackup policy on the
# server.  By default, 'ypcat hosts' is used to get a hosts list, however
# a hosts file (e.g., /etc/hosts) can be specified.
#
# To specify a specific report use the -coverage, -hardware, or -hosts options.
# The -client option can be used to generate a report for a specific client.
# Note that the -client option only applies to the coverage and hardware
# reports.  The -mailid option can be used to mail the reports to a specific
# user.
#
# This script may be used in a crontab entry to have the reports generated and
# mailed to you once per day.
#
# caveat:
#   -not tried on all platforms, some may require modifications.
#   

USAGE="usage: $0 [-coverage] [-hardware] [-hosts [local_hosts_file]] [-client <name>] [-mailid <id>]"
BP_DIR=/usr/openv/netbackup
BP_ADMINCMD=${BP_DIR}/bin/admincmd
COVERAGE_RPT=0
HARDWARE_RPT=0
HOSTS_RPT=0
CLIENTNAME="NoNe"
MAILID="NoNe"
LOCALHOSTSFILE="NoNe"

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

# Process options
while true
do
	if [ "$#" -eq 0 ] ; then
		break
	fi

	case "$1"
	in
		"-coverage")
			COVERAGE_RPT=1
			;;
		"-hardware")
			HARDWARE_RPT=1
			;;
		"-hosts")
			HOSTS_RPT=1
			if [ "$#" -gt 1 ] ; then
				case "$2" in
					-*)
						;;
					*)
						LOCALHOSTSFILE=$2
						shift
						;;
				esac
			fi
			;;
		"-client")
			if [ "$#" -eq 1 ] ; then
				echo "client name not specified"
				echo ""
				echo $USAGE
				echo ""
				exit 1
			fi
			CLIENTNAME=$2
			shift
			;;
		"-mailid")
			if [ "$#" -eq 1 ] ; then
				echo "mailid not specified"
				echo ""
				echo $USAGE
				echo ""
				exit 1
			fi
			MAILID=$2
			shift
			;;
		*)
			echo "unknown option: $1"
			echo ""
			echo $USAGE
			echo ""
			exit 1
			;;
	esac

	# get next option
	shift
done

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

# If none of the report options are specified, all reports are generated.
if [ $COVERAGE_RPT -eq 0 -a $HARDWARE_RPT -eq 0 -a $HOSTS_RPT -eq 0 ] ; then
	COVERAGE_RPT=1
	HARDWARE_RPT=1
	HOSTS_RPT=1
fi

TR=/usr/bin/tr

case "$HARDWARE" in
	SUN4)
		TR=/bin/tr
		;;
	RS6000)
		TR=/bin/tr
		;;
	SOLARIS)
		TR=/bin/tr
		;;
esac

TAB=`/bin/echo ' ' | $TR ' ' '\011'`
SPACE=' '

# Call bpcoverage to create report(s) for client

call_bpcoverage() {
	CLIENT=${1}

	if [ $COVERAGE_RPT -eq 1 -a $HARDWARE_RPT -eq 1 ] ; then

		/bin/echo "Generating Coverage & Hardware/OS Report for client $CLIENT"

		${BP_ADMINCMD}/bpcoverage -c ${CLIENT} ${COV_HEADER_OPTION} -coverage ${COV_RPT_FILE} ${HW_HEADER_OPTION} -hardware ${HW_RPT_FILE} 1>>${COV_RPT_FILE} 2>&1
		if [ $? != 0 ] ; then
			/bin/echo "Reports for $CLIENT failed"
			/bin/echo " "
			return
		fi
		/bin/echo "Completed reports for $CLIENT"
		/bin/echo " "
		COV_HEADER_OPTION="-no_cov_header"
		HW_HEADER_OPTION="-no_hw_header"

   elif [ $COVERAGE_RPT -eq 1 ] ; then

		/bin/echo "Generating Coverage Report for client $CLIENT"

		${BP_ADMINCMD}/bpcoverage -c ${CLIENT} ${COV_HEADER_OPTION} -coverage ${COV_RPT_FILE} 1>>${COV_RPT_FILE} 2>&1
		if [ $? != 0 ] ; then
			/bin/echo "Report for $CLIENT failed"
			/bin/echo " "
			return;
		fi
		/bin/echo "Completed report for $CLIENT"
		/bin/echo " "
		COV_HEADER_OPTION="-no_cov_header"

   elif [ $HARDWARE_RPT -eq 1 ] ; then

		/bin/echo "Generating Hardware/OS Report for client $CLIENT"

		${BP_ADMINCMD}/bpcoverage -c ${CLIENT} ${HW_HEADER_OPTION} -hardware ${HW_RPT_FILE} ${QUERY_OPTION} 1>>${HW_RPT_FILE} 2>&1
		if [ $? != 0 ] ; then
			/bin/echo "Report for $CLIENT failed"
			/bin/echo " "
			return
		fi
		/bin/echo "Completed report for $CLIENT"
		/bin/echo " "
		HW_HEADER_OPTION="-no_hw_header"

	fi
}

# 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"
}
		


# Create a files for output of reports
DATE_TIME=`/bin/date +%m-%d@%H:%M`
if [ $COVERAGE_RPT -eq 1 ] ; then
	COV_RPT_FILE=/tmp/coverage_rpt.${DATE_TIME}
	touch ${COV_RPT_FILE}
fi
if [ $HARDWARE_RPT -eq 1 ] ; then
	HW_RPT_FILE=/tmp/hardware_rpt.${DATE_TIME}
	touch ${HW_RPT_FILE}
fi
if [ $HOSTS_RPT -eq 1 ] ; then
	HOSTS_RPT_FILE=/tmp/hosts_rpt.${DATE_TIME}
	touch ${HOSTS_RPT_FILE}
	HOSTS_FILE=/tmp/hosts.${DATE_TIME}
	touch ${HOSTS_FILE}
	YES_HOSTS_FILE=/tmp/yes_hosts.${DATE_TIME}
	touch ${YES_HOSTS_FILE}
	NO_HOSTS_FILE=/tmp/no_hosts.${DATE_TIME}
	touch ${NO_HOSTS_FILE}
fi
NON_UNIX_FOUND=/tmp/non-UNIX_clients.${DATE_TIME}
touch $NON_UNIX_FOUND
COV_HEADER_OPTION=" "
HW_HEADER_OPTION=" "
QUERY_OPTION=" "

if [ $COVERAGE_RPT -eq 1 -o $HARDWARE_RPT -eq 1 ] ; then
	if [ "$CLIENTNAME" != "NoNe" ] ; then
		call_bpcoverage ${CLIENTNAME}
	else
		/usr/openv/netbackup/bin/admincmd/bpplclients -allunique -noheader 2>/dev/null |
		while read policy_hw policy_os client
		do
			if [ \( "$policy_hw" = "PC" -a  "$policy_os" != "WindowsNT" -a "$policy_os" != "Windows95" -a "$policy_os" != "Windows98" -a "$policy_os" != "WindowsMe" -a "$policy_os" != "Windows2000" -a "$policy_os" != "WindowsXP" -a "$policy_os" != "WindowsNET" \) -o \( "$policy_hw" = "PC-IA64" -a "$policy_os" != "WindowsXP" -a "$policy_os" != "WindowsNET" \)  -o "$policy_hw" = "MACINTOSH" -o "$policy_hw" = "Novell" -o "$policy_hw" = "NDMP" ] ; then

				QUERY_OPTION="-no_client_query"
# Coverage reports are not currently supported for some non-UNIX clients.
# WindowsNT/2000/XP/NET, Windows95/98/Me and PC-IA64 Windows XP/NET clients are supported.
				if [ $COVERAGE_RPT -eq 1 ] ; then
					echo "  $policy_os  ${TAB}$client" >> ${NON_UNIX_FOUND}
					if [ $HARDWARE_RPT -eq 1 ] ; then
						COVERAGE_RPT=0;
						call_bpcoverage ${client}
						COVERAGE_RPT=1;
					fi
				else
					call_bpcoverage ${client}
				fi
				QUERY_OPTION=" "
			else
				call_bpcoverage ${client}
			fi
		done
	fi
fi

# If you do not want ClearCase mvfs file systems in the Coverage Report then
# uncomment the next 7 lines.  Use egrep to remove lines containing mvfs=
#if [ $COVERAGE_RPT -eq 1 ] ; then
#	RM_MVFS_FILE=/tmp/rm_mvfs.${DATE_TIME}
#	touch ${RM_MVFS_FILE}
#	cat $COV_RPT_FILE | egrep -v "mvfs=" > $RM_MVFS_FILE
#	mv $RM_MVFS_FILE $COV_RPT_FILE
#	rm -f $RM_MVFS_FILE
#fi

# Generate Hardware/OS reports for Non-UNIX clients
# if [ "$CLIENTNAME" = "NoNe" -a $HARDWARE_RPT -eq 1 ] ; then
#
# If the hardware report file is not zero length, then the header must have
# been displayed already.  NOTE: The DISPLAY_HEADER variable cannot be used,
# the while loop causes a subprocess to be spawned and any changes to 
# DISPLAY_HEADER are not available to the parent process once the subprocess
# for the while completes.
#
#	if [ -s $HW_RPT_FILE ] ; then
#		HEADER_OPTION="-nh"
#	else
#		HEADER_OPTION=" "
#	fi
#
#	/usr/openv/netbackup/bin/admincmd/bpplclients -allunique -noheader 2>/dev/null |
#	while read policy_hw policy_os client
#	do
#		if [ "$policy_hw" = "PC" -o "$policy_hw" = "PC-IA64" -o "$policy_hw" = "MACINTOSH" -o "$policy_hw" = "Novell" -o "$policy_hw" = "NDMP" ] ; then
#
#			/bin/echo "Generating Hardware/OS Report for client $client"
#
#			${BP_ADMINCMD}/bpcoverage -c ${client} ${HEADER_OPTION} -no_client_query -hardware ${HW_RPT_FILE} 1>>${HW_RPT_FILE} 2>&1
#			if [ $? != 0 ] ; then
#				/bin/echo "Report for $client failed"
#				/bin/echo " "
#			else
#				/bin/echo "Completed report for $client"
#				/bin/echo " "
#			fi
#
#			HEADER_OPTION="-nh"
#		fi
#	done
#fi

# Generate Hosts Report
if [ $HOSTS_RPT -eq 1 ] ; then

	/bin/echo "Generating Hosts Report"

	if [ "$LOCALHOSTSFILE" = "NoNe" ] ; then
		/usr/bin/ypcat hosts > $HOSTS_FILE 2> /dev/null
	else
		# Using egrep to remove lines that start with the # (comment) character
		# and blank lines
		cat $LOCALHOSTSFILE | egrep -v "^#" > $NO_HOSTS_FILE
		cat $NO_HOSTS_FILE | egrep -v "^$" > $HOSTS_FILE
	fi

	/usr/openv/netbackup/bin/admincmd/bpplclients -allunique -noheader 2>/dev/null |
	while read policy_hw policy_os client
	do
		cat $HOSTS_FILE | egrep "($SPACE|$TAB)${client}($|$SPACE|$TAB)" >> $YES_HOSTS_FILE
		cat $HOSTS_FILE | egrep -v "($SPACE|$TAB)${client}($|$SPACE|$TAB)" > $NO_HOSTS_FILE
		mv $NO_HOSTS_FILE $HOSTS_FILE
	done

	cat $YES_HOSTS_FILE  |
	while read ipaddress aliases
	do
		if [ -x /bin/printf ] ; then
			FORMATTED=`/bin/printf "YES  %-15s" $ipaddress`
			echo "$FORMATTED  $aliases" >> $NO_HOSTS_FILE
		else
			echo "YES  $ipaddress  ${TAB}$aliases" >> $NO_HOSTS_FILE
		fi
	done

	cat $HOSTS_FILE  |
	while read ipaddress aliases
	do
		if [ -x /bin/printf ] ; then
			FORMATTED=`/bin/printf "NO   %-15s" $ipaddress`
			echo "$FORMATTED  $aliases" >> $NO_HOSTS_FILE
		else
			echo "NO   $ipaddress  ${TAB}$aliases" >> $NO_HOSTS_FILE
		fi
	done

	# Sort by IP address
	sort -u -t. +0.5n -1 +1n -2 +2n -3 +3n -4 $NO_HOSTS_FILE > $HOSTS_FILE

	echo "                       Hosts Report" > $HOSTS_RPT_FILE
	echo "                       ------------" >> $HOSTS_RPT_FILE
	echo " " >> $HOSTS_RPT_FILE
	echo "      Key:  YES      - In a NetBackup policy" >> $HOSTS_RPT_FILE
	echo "            NO       - Not in a NetBackup policy" >> $HOSTS_RPT_FILE
	echo " " >> $HOSTS_RPT_FILE
	echo " " >> $HOSTS_RPT_FILE
	cat $HOSTS_FILE >> $HOSTS_RPT_FILE

	rm -f $HOSTS_FILE
	rm -f $NO_HOSTS_FILE
	rm -f $YES_HOSTS_FILE

	/bin/echo "Completed report
"
fi

if [ -s $NON_UNIX_FOUND ] ; then
	echo "
No Coverage reports generated for the following non-UNIX clients.
  OS Type       Client name
  -------       -----------"
	Entries=`wc -l $NON_UNIX_FOUND | sed s/' '/''/g | cut -d '/' -f1`
	if [ $Entries -gt 9 ] ; then
		head -9 $NON_UNIX_FOUND
		echo "  ...
There are $Entries clients in the list

The complete non-UNIX client list is in $NON_UNIX_FOUND "
	else
		cat $NON_UNIX_FOUND
	fi
else
	rm -f $NON_UNIX_FOUND
fi

echo
if [ $COVERAGE_RPT -eq 1 ] ; then
	echo "The Coverage Report can be found in $COV_RPT_FILE"
fi

if [ $HARDWARE_RPT -eq 1 ] ; then
	echo "The Hardware/OS Report can be found in $HW_RPT_FILE"
fi

if [ $HOSTS_RPT -eq 1 ] ; then
	echo "The Hosts Report can be found in $HOSTS_RPT_FILE"
fi

if [ "$MAILID" != "NoNe" ] ; then
	echo "
Mailing Report(s) to $MAILID"
	if [ $COVERAGE_RPT -eq 1 ] ; then
		cat $COV_RPT_FILE | mail_cmd "Coverage Report" $MAILID
	fi
	if [ $HARDWARE_RPT -eq 1 ] ; then
		cat $HW_RPT_FILE | mail_cmd "Hardware/OS Report" $MAILID
	fi
	if [ $HOSTS_RPT -eq 1 ] ; then
		cat $HOSTS_RPT_FILE | mail_cmd "Hosts Report" $MAILID
	fi
fi
