#!/bin/sh
# $Revision$
#
#***************************************************************************
# $Copyright: Copyright (c) 2022 Veritas Technologies LLC. All rights reserved $
#***************************************************************************
#
# netbackup.sh
#
# Start/Stop the NetBackup request daemon at system initiation/shutdown.
# Starts/Stops the Media Manager device daemon (ltid).
# Starts/Stops the Sysbase ASA server (NB_dbsrv)
#
# Note - when the Media Manager device daemon (ltid) is started,
# it automatically starts the Media Manager volume daemon
# (/usr/openv/volmgr/bin/vmd) and robotic daemons as required.
#
# The following two lines are used by RedHat Linux "chkconfig" command
# and are NOT comments.
# chkconfig: 235 77 01
# description: NetBackup
#
# The following INIT INFO block is used by SuSE insserv command and is NOT a comment.
### BEGIN INIT INFO
# Provides: netbackup
# Required-Start: $network vxpbx_exchanged
# Required-Stop: $network vxpbx_exchanged
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: NetBackup
### END INIT INFO
#

# Solaris's default awk doesn't support regex functions, but nawk does. Nawk
# isn't installed on all platforms, though. Let's use nawk when it's available,
# but awk otherwise. In order of preference:
#   /bin/nawk
#   /usr/bin/nawk
#   /usr/bin/awk
#
#  This logic is duplicated in nbcheck at
#  lib/NBCheck/NixPlatformCheck.pm.  Any change to the logic
#  here should be ported to nbcheck to keep the two components
#  synchronized.

AWK=/bin/nawk

if [ ! -f ${AWK} ] && [ -f /usr/bin/nawk ]; then
	AWK=/usr/bin/nawk
elif  [ ! -f ${AWK} ] && [ -f /usr/bin/awk ]; then
	AWK=/usr/bin/awk
fi

# vim: set ft=sh et sw=4 ts=4:

#----- $Id$ -----
#
#		This function is a case statement sets
#		the ECHO variable
#		with the appropriate path & flags.

#Define Echo to allow escape characters
case "`uname -s`" in
	Linux*)
		unset POSIXLY_CORRECT
		ECHO="/bin/echo -e"
		;;
	SunOS*)
		ECHO="/usr/bin/echo"
		;;
	*)
		ECHO="echo"
		;;
esac

# Read the configuration value(s) given by $2 from the file given in $1. The
# file should contain key=value pairs. Whitespace is ignored. Comments start
# with #. Whitespace surrounding the value is stripped before returning.
#
# Returns 0 if the key exists in the file. Prints the value to stdout. (If the
# key is present, but there's no value, then we print nothing, but still return
# 0.)
# Returns 1 if the key does not exist in the file, or if the file does not
# exist.
#
# Examples:
#
# Get name of master server
# read_first_config_value /usr/openv/netbackup/bp.conf SERVER
#
# Read answer file
# read_config_values /tmp/NBInstallAnswer.conf CA_CERTIFICATE_FINGERPRINT
#
# Requires fn.set_echo_var and fn.set_awk_var

__read_config_helper()
{
    config_file="${1}"
    key="${2}"
    filter="${3}"
    if [ ! -f "${config_file}" ]; then
        return 1
    fi
    result=`
        ${AWK} -v key="${key}" -F'[ \t]*=' '
        BEGIN { result = 227 }
        {
            sub("#.*", "", $0);
            if (match($1, "^[ \t]*" key "$")) {
                sub("^[^=]*=[ \t]*", "", $0);
                sub("[ \t]*$", "", $0);
                print;
                result = 0
            }
        }
        END { exit result }' "${config_file}"
    `
    if [ $? -ne 0 ]; then
        # no value specified
        return 1
    else
        # Filter
        ${ECHO} "${result}" | ${filter}
        return 0
    fi
}

read_config_values()
{
    __read_config_helper "$@" cat
}

read_first_config_value()
{
    __read_config_helper "$@" 'head -1'
}

read_last_config_value()
{
    __read_config_helper "$@" 'tail -1'
}

# vim: set ft=sh et sw=4 ts=4:

#----- $Id$
#
# Checks in configuration if NBMQBROKER service is enabled
# on this machine.
#
# Returns:
#	0 if NBMQBROKER is not enabled
#	1 if NBMQBROKER is enabled
#	2 if an error occurred
#
# These functions are expected to be included already:
#	fn.set_echo_var
#	fn.set_awk_var
#	fn.read_config_values

NBCL_CONF_FILE=/usr/openv/var/global/nbcl.conf
BP_CONF_FILE=/usr/openv/netbackup/bp.conf

check_for_nbmqbroker ()
{
	use_nbmqbroker=""
	if [ -f "${NBCL_CONF_FILE}" ] ; then
		use_nbmqbroker=`read_config_values ${NBCL_CONF_FILE} ENABLE_MQBROKER | tr '[:lower:]' '[:upper:]'`
	fi

	# If entry is not present in nbcl.conf then check in bp.conf file.
	if [ "${use_nbmqbroker}" = "" ] ; then
		if [ -f "${BP_CONF_FILE}" ] ; then
			use_nbmqbroker=`read_config_values ${BP_CONF_FILE} ENABLE_MQBROKER | tr '[:lower:]' '[:upper:]'`
		else
			# Return error if bp.conf is not present.
			return 2
		fi
	fi

	# Return 1 if ENABLE_MQBROKER flag is set, else return 0.
	if [ "${use_nbmqbroker}" = "TRUE" -o "${use_nbmqbroker}" = "YES" -o "${use_nbmqbroker}" = "1" ] ; then
		return 1
	else
		return 0
	fi
}

FTO_DNAT_SUPPORT=1
FTO_HARDENING_NVE60_PHASE2=0

# Set umask to 022 to make sure files and directories
# are not created with world writable permissions.
umask 022

NBPATH=/usr/openv/netbackup/bin
MQBROKERPATH=/usr/openv/mqbroker/bin
ERLPATH=/usr/openv/mqbroker/erlang/erts-12.0.4/bin
# setting message broker config file path that will 
# use to check whether service is configured or not
# before starting the service
MQBROKER_CONFIG_FILE=/usr/openv/var/global/mqbroker/mqbroker.config
PDDEPATH=/usr/openv/pdde
PDREG=/etc/pdregistry.cfg
SPWS_SERVICE=/etc/init.d/pdde-spws
VPFS_ENABLED=${PDDEPATH}/vpfs/etc/vpfs.mnt
VMPATH=/usr/openv/volmgr/bin
VMPS=/usr/openv/volmgr/bin/vmps
BPPS=/usr/openv/netbackup/bin/bpps
LTID=/usr/openv/volmgr/bin/ltid
TL8CD=/usr/openv/volmgr/bin/tl8cd
TLDCD=/usr/openv/volmgr/bin/tldcd
TLHCD=/usr/openv/volmgr/bin/tlhcd
BPRD=/usr/openv/netbackup/bin/initbprd
NB_DBSRV=/usr/openv/db/bin/NB_dbsrv
MAKE_SCSI_DEV=/usr/openv/volmgr/bin/make_scsi_dev
BPCONF=/usr/openv/netbackup/bp.conf
BPRDREQ=/usr/openv/netbackup/bin/admincmd/bprdreq
STOPLTID=/usr/openv/volmgr/bin/stopltid
VMCTRLDBM=/usr/openv/volmgr/bin/vmctrldbm
MKDEV_OVPASS=/usr/openv/volmgr/bin/driver/mkdev_ovpass
BMRD=/usr/openv/netbackup/bin/rc.bmrd
BPCLNTCMD=/usr/openv/netbackup/bin/bpclntcmd

FORCE_INIT_INQUIRY=/usr/openv/netbackup/db/config/FORCE_INIT_INQUIRY

SPACE=' '
BLANK=''
RETURN=0

if [ -d /usr/openv/tmp ] ; then
	TMPDIR=/usr/openv/tmp
else
	TMPDIR=/tmp
fi

PATH=$PATH:/usr/bin
export PATH

PRG=`basename $0`
TMPF=${TMPDIR}/${PRG}.$$
/bin/rm -f $TMPF

is_media_server_only=0
PS="/bin/ps -ea"
OSTYPE=`uname -s`
case $OSTYPE in
	Linux)
		if [ "`uname -m`" = "s390x" ] ; then
			is_media_server_only=1
		fi
		;;
esac

ARG="${1}"

if [ -z "${ARG}" ] ; then
	ARG=start
fi

# Convert AIX/HP-UX/Solaris 'Mon DD' in bpps output to Linux 'MonDD'.
# Allows accurate whitespace delimited parsing of STIME, TTY, TIME, CMD.

MonDD ()
{
	case $OSTYPE in
		"AIX"|"HP-UX"|"SunOS") 
  			sed -e 's,^\( *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ]*  *[^ ][^ ][^ ]\)  *,\1,'
			;;
		*)
			cat
			;;
	esac
}

wait_for_process()
{
  process_name=$1

  retrycnt=0
  # Wait for a particular process to terminate
  PROCESS_TMPF=${TMPDIR}/nb_term_$process_name.$$
  while [ ${retrycnt} -lt 30 ]
  do
    /bin/rm -f $PROCESS_TMPF
    eval $NBPATH/bpps | grep $process_name | grep -v terminate | MonDD > $PROCESS_TMPF
    process_found=`awk "\\$8 ~ /$process_name/" ${PROCESS_TMPF}`
    if [ Y"${process_found}" != Y ] ; then
      sleep 1
    else
      break
    fi
    retrycnt=`expr ${retrycnt} + 1`
  done
  /bin/rm -f $PROCESS_TMPF
}

term_with_kill ()
{
	killstr=$1
	signal=$2
	processes=`${BPPS} | egrep -v '[ /\[]NB_dbsrv' | grep -v grep \
	grep "$killstr" | awk '{print $2}'`
	for process in $processes
	do
		if [ Y"$signal" != Y ] ; then
			kill -${signal} $process > /dev/null 2>/dev/null
		else
			kill $process > /dev/null 2>/dev/null
		fi
	done
}

stop_acs_daemon ()
{
	process="$*"
	kill $process > /dev/null 2>/dev/null
}

case "${ARG}" in
start)

	if [ ${FTO_HARDENING_NVE60_PHASE2} -ne 0 ] ; then
		if [ -x ${NBPATH}/nbpas ] ; then
			${NBPATH}/nbpas
			echo "NetBackup Privileged Access Service started."
		else
			RETURN=2
			echo "NetBackup Privileged Access Service not started."
		fi
	fi

	if [ -x ${NBPATH}/vnetd ] ; then
		${NBPATH}/vnetd -standalone
		echo "NetBackup network daemon started."
	else
		RETURN=2
		echo "NetBackup network daemon not started."
	fi

	# Changing the startup order of services on master server to enable 
	# secure communications. One of the primary services for secure comms 
	# is NetBackup Web Management Console(nbwmc), and it is required to be started 
	# as high in order as possile. All the services that nbwmc depends on need to
	# started prior to it. NB_dbsrv being the most important one without which nbwmc
	# cannot start is started first, also NB_dbsrv does not have any dependencies.

	if [ -f /usr/openv/netbackup/version ] ; then
		# Need to start NB_dbsrv before anyone else gets started.
		NB_dbsrv_PID=`$BPPS | grep NB_dbsrv | grep -v grep | awk '{print $2}'`
		if [ "$NB_dbsrv_PID" = "" -a "${is_media_server_only}" -eq 0 ] ; then
			${NBPATH}/nbdbms_start_stop start
			if [ $? != 0 ] ; then
				echo "NetBackup will not run without ${NB_DBSRV} running"
			else
				echo "NetBackup Database Server started."
			fi
		fi

		if [ -x ${NBPATH}/nbatd ] ; then
			${NBPATH}/nbatd
			echo "NetBackup Authentication daemon started."
		else
			RETURN=2
			echo "NetBackup Authentication daemon not started."
		fi

		if [ -x ${NBPATH}/nbazd ] ; then
			${NBPATH}/nbazd
			echo "NetBackup Authorization daemon started."
		else
			RETURN=2
			echo "NetBackup Authorization daemon not started."
		fi

		if [ "${is_media_server_only}" -eq 0 ] ; then
			if [ -x ${NBPATH}/nbaudit ] ; then
				${NBPATH}/nbaudit
				echo "NetBackup Audit Manager started."
			else
				RETURN=2
				echo "NetBackup Audit Manager not started."
			fi
		fi

		if [ "${is_media_server_only}" -eq 0 ] ; then
			if [ -x ${NBPATH}/nbwmc ] ; then
				${NBPATH}/nbwmc
			fi
		fi
		if [ ${FTO_DNAT_SUPPORT} -ne 0 -a "${is_media_server_only}" -eq 0 ] ; then
			check_for_nbmqbroker
			if [ $? -eq 1 ] ; then
				if [ -x ${MQBROKERPATH}/nbmqbroker ] && [ -f ${MQBROKER_CONFIG_FILE} ] ; then
					${MQBROKERPATH}/nbmqbroker
				fi
			fi
		fi
	fi

	# NB client daemons start first except for bmrbd which
	# is dependent on bmrd.

	if [ -x ${NBPATH}/bpcd ] ; then
		${NBPATH}/bpcd -standalone
		echo "NetBackup client daemon started."
	else
		RETURN=2
		echo "NetBackup client daemon not started."
	fi

	if [ -x ${NBPATH}/nbftclnt ] ; then
		# Intentionally shut off stdout messages but not stderr.
		${NBPATH}/nbftclnt > /dev/null
		echo "NetBackup SAN Client Fibre Transport daemon started."
	fi

	if [ -x ${NBPATH}/nbdisco ] ; then
		${NBPATH}/nbdisco
		echo "NetBackup Discovery Framework started."
	fi

	# This section is for NB server daemons.

	if [ -f /usr/openv/netbackup/version ] ; then
		if [ "$OSTYPE" = "AIX" ] ; then
			if [ -x ${MKDEV_OVPASS} ] ; then
				$MKDEV_OVPASS

				if [ -f "$FORCE_INIT_INQUIRY" ] ; then
					INQUIRY=/usr/openv/volmgr/bin/scsi_command
					TPCONFIG=/usr/openv/volmgr/bin/tpconfig
					for i in `$TPCONFIG -d | grep rmt | awk '{ print $3 }' `
					do
						echo "Forcing initial SCSI inquiry to $i" > /dev/console
						$INQUIRY -d $i > /dev/null 2>&1
					done
				fi
			else
				echo "ERROR executing mkdev_ovpass !"
			fi
		fi

		if [ -x ${NBPATH}/nbevtmgr ] ; then
			${NBPATH}/nbevtmgr
			echo "NetBackup Event Manager started."
		else
			RETURN=2
			echo "NetBackup Event Manager not started."
		fi

		# If PDDE is present, start its daemons.

		if [ -x ${PDDEPATH}/pdconfigure/pdde ] ; then
			${PDDEPATH}/pdconfigure/pdde ocsd start --services-override-nodelay >/dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "NetBackup Open Cloud Storage Daemon not started."
				RETURN=2
			else
				echo "NetBackup Open Cloud Storage Daemon started."
			fi

			${PDDEPATH}/pdconfigure/pdde spad start --services-override-nodelay >/dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "NetBackup Deduplication Manager not started."
				RETURN=2
			else
				echo "NetBackup Deduplication Manager started." 
			fi
		
			${PDDEPATH}/pdconfigure/pdde spoold start --services-override-nodelay >/dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "NetBackup Deduplication Engine not started."
				RETURN=2
			else
				echo "NetBackup Deduplication Engine started." 
			fi
		fi

		# Start the NetBackup Cloud Object Store Protection daemon.

		if [ -x ${PDDEPATH}/pdcr/bin/nbcosp ] ; then
			${PDDEPATH}/pdcr/bin/nbcosp start >/dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "NetBackup Cloud Object Store Protection daemon not started."
				RETURN=2
			else
				echo "NetBackup Cloud Object Store Protection daemon started."
			fi
		fi

		# If VPFS is present, start the mounts.

		if [ -x ${PDDEPATH}/vpfs/etc/init.d/vpfs_mounts -a -L ${VPFS_ENABLED} ] ; then
			${PDDEPATH}/vpfs/etc/init.d/vpfs_mounts start >/dev/null 2>&1
			if [ $? != 0 ] ; then
				echo "NetBackup Deduplication Provision Filesystem not started."
				RETURN=2
			else
				echo "NetBackup Deduplication Provision Filesystem started."
			fi
		fi

		# If spws is present, start its daemon.

		if [ -x ${PDDEPATH}/vpfs/etc/init.d/pdde-spws -a -f ${SPWS_SERVICE} ] ; then
			if [ -x /bin/systemctl ] ; then
				if [ -f /etc/systemd/system/pdde-spws.service ] ; then
					/bin/systemctl start pdde-spws
				fi
			else
				${PDDEPATH}/vpfs/etc/init.d/pdde-spws start >/dev/null 2>&1
			fi

			if [ $? != 0 ] ; then
				echo "NetBackup Deduplication Storage Pool Web Service not started."
			else
				echo "NetBackup Deduplication Storage Pool Web Service started."
			fi
		fi

	fi

	# This section is for NB server daemons.

	if [ -f /usr/openv/netbackup/version ] ; then

		if [ "${is_media_server_only}" -eq 0 ] ; then
			if [ -x ${NBPATH}/nbemm ] ; then
				${NBPATH}/nbemm
				echo "NetBackup Enterprise Media Manager started."
			else
				RETURN=2
				echo "NetBackup Enterprise Media Manager not started."
			fi

			if [ -x ${NBPATH}/nbrb ] ; then
				${NBPATH}/nbrb
				echo "NetBackup Resource Broker started."
			else
				RETURN=2
				echo "NetBackup Resource Broker not started."
			fi
		fi

		# Be sure that the sg driver module is loaded
		if [ "$OSTYPE" = "Linux" ] ; then
			/sbin/modprobe sg
		fi

		# Don't start ltid if already started
		$VMPS > $TMPF
		if [ -x "$LTID" ] ; then
			ltid=`grep ltid $TMPF`
			if test Y"$ltid" = Y
			then
				if [ -x ${MAKE_SCSI_DEV} -a "$OSTYPE" = "Linux" ] ; then
					echo "Rebuilding device nodes."
					$MAKE_SCSI_DEV
				fi
				$LTID > /dev/null 2>&1
				echo "Media Manager daemons started."
			fi
		else
			RETURN=1
			echo "Media Manager daemons not started."
		fi

		/bin/rm -f $TMPF

		if [ "${is_media_server_only}" -eq 0 ] ; then
			if [ -x "${BPRD}" ] ; then
				${BPRD}
				echo "NetBackup request daemon started."
			else
				RETURN=2
				echo "NetBackup request daemon not started."
			fi
		fi

		if [ -x ${NBPATH}/bpcompatd ] ; then
			${NBPATH}/bpcompatd
			echo "NetBackup compatibility daemon started."
		else
			RETURN=2
			echo "NetBackup compatibility daemon not started."
		fi

		if [ -x ${NBPATH}/nbjm ] ; then
			${NBPATH}/nbjm
			echo "NetBackup Job Manager started."
		else
			RETURN=2
			echo "NetBackup Job Manager not started."
		fi

		if [ -x ${NBPATH}/nbpem ] ; then
			${NBPATH}/nbpem
			echo "NetBackup Policy Execution Manager started."
		else
			RETURN=2
			echo "NetBackup Policy Execution Manager not started."
		fi

		if [ -x ${NBPATH}/nbstserv ] ; then
			${NBPATH}/nbstserv
			echo "NetBackup Storage Lifecycle Manager started."
		else
			RETURN=2
			echo "NetBackup Storage Lifecycle Manager not started."
		fi

		if [ -x ${NBPATH}/nbrmms ] ; then
			${NBPATH}/nbrmms
			echo "NetBackup Remote Monitoring Management System started."
		else
			RETURN=2
			echo "NetBackup Remote Monitoring Management System not started."
		fi

		if [ -x ${NBPATH}/nbkms ] ; then
			${NBPATH}/nbkms
			echo "NetBackup Key Management daemon started."
		else
			RETURN=2
			echo "NetBackup Key Management daemon not started."
		fi

		if [ -x ${NBPATH}/nbsl ] ; then
			${NBPATH}/nbsl
			echo "NetBackup Service Layer started."
		else
			RETURN=2
			echo "NetBackup Service Layer not started."
		fi

		if [ "${is_media_server_only}" -eq 0 ] ; then
			if [ -x ${NBPATH}/nbim ] ; then
				${NBPATH}/nbim
				echo "NetBackup Indexing Manager started."
			else
				RETURN=2
				echo "NetBackup Indexing Manager not started."
			fi

			if [ -x ${NBPATH}/nbars ] ; then
				${NBPATH}/nbars
				echo "NetBackup Agent Request Server started."
			else
				RETURN=2
				echo "NetBackup Agent Request Server not started."
			fi

			if [ -x "${BMRD}" ] ; then
				${BMRD} start >/dev/null 2>&1
				echo "NetBackup Bare Metal Restore daemon started."
			else
				RETURN=2
				echo "NetBackup Bare Metal Restore daemon not started."
			fi

		fi

		if [ -x ${NBPATH}/nbvault ] ; then
			${NBPATH}/nbvault >/dev/null 2>&1
			echo "NetBackup Vault daemon started."
		else
			RETURN=2
			echo "NetBackup Vault daemon not started."
		fi

		if [ -x ${NBPATH}/nbanomalymgmt ] ; then
			${NBPATH}/nbanomalymgmt -start > /dev/null 2>&1
			echo "NetBackup Anomaly Management daemon started."
		else
			echo "NetBackup Anomaly Management daemon not started."
		fi

		if [ -x ${NBPATH}/nbcctd ] ; then
			${NBPATH}/nbcctd -X
			echo "NetBackup Continuous Data Protection daemon started."
		fi

		if [ -x ${NBPATH}/nbsvcmon ] ; then
			${NBPATH}/nbsvcmon
			echo "NetBackup Service Monitor started."
		else
			RETURN=2
			echo "NetBackup Service Monitor not started."
		fi

		if [ -x /etc/init.d/nbftserver -a -x ${NBPATH}/nbftsrvr ] ; then
			/etc/init.d/nbftserver start >/dev/null 2>&1
			echo "NetBackup Fibre Transport Service daemon started."
		elif [ -x /etc/rc.d/init.d/nbftserver -a -x ${NBPATH}/nbftsrvr ] ; then
			/etc/rc.d/init.d/nbftserver start >/dev/null 2>&1
			echo "NetBackup Fibre Transport Service daemon started."
		fi
	fi

	#  See if BMR Boot Server has been installed.  If so, start its
	#  daemon.  Must happen after bmrd.

	if [ -x ${NBPATH}/rc.bmrbd ] ; then
		${NBPATH}/rc.bmrbd start >/dev/null 2>&1
		echo "NetBackup Bare Metal Restore Boot Server daemon started."
	else
		RETURN=2
		echo "NetBackup Bare Metal Restore Boot Server daemon not started."
	fi

#
# This locking file is used by LINUX init
#
	if [ -d /var/lock/subsys ] ; then
		touch /var/lock/subsys/netbackup
	fi
	;;
stop)

	IS_NETBACKUP_DAEMON=YES
	export IS_NETBACKUP_DAEMON

	#  See if BMR Boot Server has been installed.  If so, stop its daemon.
	#  bmrbd blocks SIGTERM so use SIGKILL here, if it won't go
	#  down gracefully.  Must go down before bmrd.

	BMRBS_PID=`${PS} | grep bmrbd | grep -v grep | awk '{print $1}'`
	if [ "${BMRBS_PID}" != "" -a -x ${NBPATH}/rc.bmrbd ] ; then
		echo "stopping the NetBackup Bare Metal Restore Boot Server daemon"
		${NBPATH}/rc.bmrbd stop >/dev/null 2>&1

		sleep 2
		BMRBS_PID=`${PS} | grep bmrbd | grep -v grep | awk '{print $1}'`
		if [ "${BMRBS_PID}" != "" ] ; then
			kill -KILL ${BMRBS_PID} >/dev/null 2>&1
		fi
	fi

	# This section is for NB server daemons.

	if [ -f /usr/openv/netbackup/version ] ; then
		NBSVCMON_PID=`$PS | grep nbsvcmon | grep -v grep | awk '{print $1}'`
		if [ "$NBSVCMON_PID" != "" ] ; then
			echo "stopping the NetBackup Service Monitor"
			${NBPATH}/nbsvcmon -terminate 2> /dev/null
		fi

		NBCCTD_PID=`${PS} | grep nbcctd | grep -v grep | awk '{print $1}'`
		if [ "${NBCCTD_PID}" != "" -a -x ${NBPATH}/nbcctd ] ; then
			echo "stopping the NetBackup Continuous Data Protection daemon"
			${NBPATH}/nbcctd -terminate 2> /dev/null
		fi

		NBANOMALYMGMT_PID=`${PS} | grep nbanomalymgmt | grep -v grep | awk '{print $1}'`
		if [ "${NBANOMALYMGMT_PID}" != "" -a -x ${NBPATH}/nbanomalymgmt ] ; then
			echo "stopping the NetBackup Anomaly Management daemon"
			${NBPATH}/nbanomalymgmt -terminate > /dev/null 2>&1
		fi

		#  If vault is present, stop its daemon.

		NBVLT_PID=`${BPPS} | grep nbvault | grep -v grep | awk '{print $2}'`
		if [ "${NBVLT_PID}" != "" -a -x ${NBPATH}/nbvault ] ; then
			echo "stopping the NetBackup Vault daemon"
			${NBPATH}/nbvault -terminate >/dev/null 2>&1
		fi

		#  See if BMR has been installed.  If so, stop its daemon.
		#  bmrd blocks SIGTERM so use SIGKILL here, if it won't go
		#  down gracefully.

		BMR_PID=`$PS | grep bmrd | grep -v grep | awk '{print $1}'`
		if [ "${BMR_PID}" != "" -a -x ${BMRD} ] ; then
			echo "stopping the NetBackup Bare Metal Restore daemon"
			${BMRD} stop >/dev/null 2>&1

			sleep 2
			BMR_PID=`$PS | grep bmrd | grep -v grep | awk '{print $1}'`
			if [ "${BMR_PID}" != "" ] ; then
				kill -KILL ${BMR_PID} > /dev/null 2>/dev/null
			fi
		fi

		NBARS_PID=`${PS} | grep nbars | grep -v grep | awk '{print $1}'`
		if [ "${NBARS_PID}" != "" ] ; then
			echo "stopping the NetBackup Agent Request Server"
			${NBPATH}/nbars -terminate 2>/dev/null
		fi

		NBIM_PID=`$PS | grep nbim | grep -v grep | awk '{print $1}'`
		if [ "$NBIM_PID" != "" ] ; then
			echo "stopping the NetBackup Indexing Manager"
			${NBPATH}/nbim -terminate 2> /dev/null
		fi

		NBSL_PID=`${BPPS} | grep nbsl | grep -v grep | awk '{print $2}'`
		if [ "${NBSL_PID}" != "" ] ; then
			echo "stopping the NetBackup Service Layer"
			${NBPATH}/nbsl -terminate 2>/dev/null
		fi

		NBKMS_PID=`${BPPS} | grep nbkms | grep -v grep | awk '{print $2}'`
		if [ "${NBKMS_PID}" != "" ] ; then
			echo "stopping the NetBackup Key Management daemon"
			${NBPATH}/nbkms -terminate 2>/dev/null
		fi

		NBRMMS_PID=`$PS | grep nbrmms | grep -v grep | awk '{print $1}'`
		if [ "$NBRMMS_PID" != "" ] ; then
			echo "stopping the NetBackup Remote Monitoring Management System"
			${NBPATH}/nbrmms -terminate 2> /dev/null
		fi

		NBSTSERV_PID=`$PS | grep nbstserv | grep -v grep | awk '{print $1}'`
		if [ "$NBSTSERV_PID" != "" ] ; then
			echo "stopping the NetBackup Storage Lifecycle Manager"
			${NBPATH}/nbstserv -terminate 2> /dev/null
		fi

		NBPEM_PID=`$PS | grep nbpem | grep -v grep | awk '{print $1}'`
		if [ "$NBPEM_PID" != "" ] ; then
			echo "stopping the NetBackup Policy Execution Manager"
			${NBPATH}/nbpem -terminate 2> /dev/null
		fi

		NBJM_PID=`$PS | grep nbjm | grep -v grep | awk '{print $1}'`
		if [ "$NBJM_PID" != "" ] ; then
			echo "stopping the NetBackup Job Manager"
			${NBPATH}/nbjm -terminate 2> /dev/null
		fi

		NBPROXY_PID=`$PS | grep nbproxy | grep -v grep | awk '{print $1}'`
		if [ "${NBPROXY_PID}" != "" ] ; then
			echo "stopping nbproxy..."
			${NBPATH}/admincmd/nbproxyreq -all > /dev/null
		fi

		BPRD_PID=`$PS | grep bprd | grep -v grep | awk '{print $1}'`
		if [ "$BPRD_PID" != "" ] ; then
			# Comments in bp.kill_all explain why we explicitly
			# specify -M localhost in this command.
			echo "stopping the NetBackup request daemon"
			${BPRDREQ} -M localhost -terminate 2> /dev/null
		fi

		COMPATD_PID=`$PS | grep bpcompat | grep -v grep | awk '{print $1}'`
		if [ "$COMPATD_PID" != "" ] ; then
			echo "stopping the NetBackup compatibility daemon"
			${NBPATH}/bpcompatd -terminate > /dev/null 2>&1
		fi

		BPDBM_PID=`$PS | grep bpdbm | grep -v grep | awk '{print $1}'`
		if [ "$BPDBM_PID" != "" ] ; then
			# Comments in bp.kill_all explain why we specify
			# -terminate_local rather than -terminate in this command.
			echo "stopping the NetBackup database daemon"
			/usr/openv/netbackup/bin/bpdbm -terminate_local 2> /dev/null
		fi

		LTID_PID=`$PS | grep ltid | grep -v grep | awk '{print $1}'`
		if [ "$LTID_PID" != "" ] ; then
			echo "stopping the Media Manager device daemon"
			${STOPLTID} 2> /dev/null
		fi

		VMD_PID=`$PS | egrep '[ /]vmd' | grep -v grep | awk '{print $1}'`
		if [ "$VMD_PID" != "" ] ; then
			echo "stopping the Media Manager volume daemon"
			${VMCTRLDBM} -t 2> /dev/null
		fi
		
		# The case for stopping tl*cd daemons is unique.
		# Usually, ltid daemon will take down the robotic
		# control daemons when it is being terminated.
		# However, in some configurations, ltid is not running
		# on a server. Therefore, we need to make sure that
		# the tl*cd daemons are down.

		robotic_pids=`$PS | egrep '[ /]tl[8dh]cd' | grep -v grep | awk '{print $1}'`
		if [ "$robotic_pids" != "" ] ; then
			echo "stopping the Media Manager robotic control daemons"
		fi

		TL8CD_PID=`$PS | egrep '[ /]tl8cd' | grep -v grep | awk '{print $1}'`
		if [ "$TL8CD_PID" != "" ] ; then
			${TL8CD} -t 2> /dev/null
		fi

		TLDCD_PID=`$PS | egrep '[ /]tldcd' | grep -v grep | awk '{print $1}'`
		if [ "$TLDCD_PID" != "" ] ; then
			${TLDCD} -t 2> /dev/null
		fi
		
		TLHCD_PID=`$PS | egrep '[ /]tlhcd' | grep -v grep | awk '{print $1}'`
		if [ "$TLHCD_PID" != "" ] ; then
			${TLHCD} -t 2> /dev/null
		fi

		NBRB_PID=`$PS | grep nbrb | grep -v grep | awk '{print $1}'`
		if [ "$NBRB_PID" != "" ] ; then
			echo "stopping the NetBackup Resource Broker"
			${NBPATH}/nbrb -terminate 2> /dev/null
		fi

		NBEMM_PID=`${PS} | grep nbemm | grep -v grep | awk '{print $1}'`
		if [ "${NBEMM_PID}" != "" ] ; then
			echo "stopping the NetBackup Enterprise Media Manager"
			${NBPATH}/nbemm -terminate 2>/dev/null
		fi

		NBFTMS_PID=`${PS} | grep nbftsrvr | grep -v grep | awk '{print $1}'`
		if [ "${NBFTMS_PID}" != "" ] ; then
			echo "stopping the NetBackup Fibre Transport Service daemon"
			if [ -x /etc/init.d/nbftserver ] ; then
				/etc/init.d/nbftserver stop >/dev/null 2>&1
			elif [ -x /etc/rc.d/init.d/nbftserver ] ; then
				/etc/rc.d/init.d/nbftserver stop >/dev/null 2>&1
			else
				${NBPATH}/nbftsrvr -terminate 2>/dev/null
			fi
			wait_for_process nbftsrvr
		fi

		ACSSSI_PID=`$PS | grep acsssi | grep -v grep | awk '{print $1}'`
		if [ "$ACSSSI_PID" != "" ] ; then
			echo "stopping the Automated Cartridge System Storage Server Interface daemon"
			# sleep matches what was already in bp.kill_all
			sleep 20
			stop_acs_daemon $ACSSSI_PID
		fi

		ACSSEL_PID=`$PS | grep acssel | grep -v grep | awk '{print $1}'`
		if [ "$ACSSEL_PID" != "" ] ; then
			echo "stopping the Automated Cartridge System SSI Event Logger daemon"
			stop_acs_daemon $ACSSEL_PID
		fi
	fi

	# This section is for NB server daemons.

	if [ -f /usr/openv/netbackup/version ] ; then

		# If PDDE is present, stop its daemons.

		SPWS_PID=`${BPPS} | grep spws | grep -v grep | awk '{print $2}'`
		if [ "${SPWS_PID}" != "" -a -x ${PDDEPATH}/vpfs/etc/init.d/pdde-spws ] ; then
			echo "stopping the NetBackup Deduplication Storage Pool Web Service"
			${PDDEPATH}/vpfs/etc/init.d/pdde-spws stop > /dev/null 2>&1
		fi

		VPFSD_PID=`${BPPS} | grep vpfsd | grep -v grep | awk '{print $2}'`
		if [ "${VPFSD_PID}${SPWS_PID}" != "" -a -x ${PDDEPATH}/vpfs/etc/init.d/vpfs_mounts ] ; then
			echo "stopping the NetBackup Deduplication Provision Filesystem"
			${PDDEPATH}/vpfs/etc/init.d/vpfs_mounts stop > /dev/null 2>&1
		fi

		SPOOLD_PID=`${BPPS} | grep spoold | grep -v grep | awk '{print $2}'`
		if [ "${SPOOLD_PID}" != "" -a -x ${PDDEPATH}/pdconfigure/pdde ] ; then
			echo "stopping the NetBackup Deduplication Engine"
			${PDDEPATH}/pdconfigure/pdde spoold stop --services-override-nodelay > /dev/null 2>&1
		fi

		SPAD_PID=`${BPPS} | grep spad | grep -v grep | awk '{print $2}'`
		if [ "${SPAD_PID}" != "" -a -x ${PDDEPATH}/pdconfigure/pdde ] ; then
			echo "stopping the NetBackup Deduplication Manager"
			${PDDEPATH}/pdconfigure/pdde spad stop --services-override-nodelay > /dev/null 2>&1
		fi

		OCSD_PID=`${BPPS} | grep ocsd | grep -v grep | awk '{print $2}'`
		if [ "${OCSD_PID}" != "" -a -x ${PDDEPATH}/pdconfigure/pdde ] ; then
			echo "stopping the NetBackup Open Cloud Storage Daemon"
			${PDDEPATH}/pdconfigure/pdde ocsd stop --services-override-nodelay > /dev/null 2>&1
		fi

		NBCOSP_PID=`${BPPS} | grep nbcosp | grep -v grep | awk '{print $2}'`
		if [ "${NBCOSP_PID}" != "" -a -x ${PDDEPATH}/pdcr/bin/nbcosp ] ; then
			echo "stopping the NetBackup Cloud Object Store Protection Daemon"
			{PDDEPATH}/pdcr/bin/nbcosp stop > /dev/null 2>&1
		fi

		# nbevtmgr is used by nb daemons so bring it down right before
		# VxDBMS.

		NBEVTMGR_PID=`${BPPS} | grep nbevtmgr | grep -v grep | awk '{print $2}'`
		if [ "${NBEVTMGR_PID}" != "" ] ; then
			echo "stopping the NetBackup Event Manager"
			${NBPATH}/nbevtmgr -terminate 2>/dev/null
		fi

	fi

	# NB client daemons stop last except for bmrbd which stops
	# first due to a dependency on bmrd.

	NBDISCO_PID=`${PS} | grep nbdisco | grep -v grep | awk '{print $1}'`
	if [ "${NBDISCO_PID}" != "" -a -x ${NBPATH}/nbdisco ] ; then
		echo "stopping the NetBackup Discovery Framework"
		${NBPATH}/nbdisco -terminate 2> /dev/null
	fi

	NBFTCLNT_PID=`${PS} | grep nbftclnt | grep -v grep | awk '{print $1}'`
	if [ "${NBFTCLNT_PID}" != "" -a -x ${NBPATH}/nbftclnt ] ; then
		echo "stopping the NetBackup SAN Client Fibre Transport daemon"
		${NBPATH}/nbftclnt -terminate 2> /dev/null
	fi

	#  nbsmartdiag blocks SIGTERM so use SIGKILL here, if it won't go
	#  down gracefully. nbsmartdiag is not expected to run or start default
	#  but need to stop with other processes.

	NBSMARTDIAG=`${PS} | grep nbsmartdiag | grep -v grep | awk '{print $1}'`
	if [ "${NBSMARTDIAG}" != "" -a -x ${NBPATH}/nbsmartdiag ] ; then
		echo "stopping the NetBackup Smart Diagnosis daemon"
		${NBPATH}/nbsmartdiag -terminate >/dev/null 2>&1

		sleep 2
		NBSMARTDIAG_PID=`${PS} | grep nbsmartdiag | grep -v grep | awk '{print $1}'`
		if [ "${NBSMARTDIAG_PID}" != "" ] ; then
			kill -KILL ${NBSMARTDIAG_PID} >/dev/null 2>&1
		fi
	fi

	BPCD_PID=`${PS} | grep bpcd | grep -v grep | awk '{print $1}'`
	if [ "${BPCD_PID}" != "" -a -x ${NBPATH}/bpcd ] ; then
		echo "stopping the NetBackup client daemon"
		${NBPATH}/bpcd -terminate 2> /dev/null
	fi

	BPCLNTCMD_CRL_PID=`${BPPS} | grep bpclntcmd | grep crl_download | grep -v grep | awk '{print $2}'`
	if [ "${BPCLNTCMD_CRL_PID}" != "" -a -x ${NBPATH}/bpclntcmd ] ; then
		echo "stopping the CRL download service"
		kill ${BPCLNTCMD_CRL_PID} 2> /dev/null
	fi

	NBRNTD_PID=`${PS} | grep nbrntd | grep -v grep | awk '{print $1}'`
	if [ "${NBRNTD_PID}" != "" -a -x ${NBPATH}/nbrntd ] ; then
		echo "stopping the NetBackup network resiliency process"
		${NBPATH}/nbrntd -terminate 2> /dev/null
	fi

	if [ -f /usr/openv/netbackup/version ] ; then

		if [ "$OSTYPE" = "SunOS" ] ; then
			# Detect nbwmc using raw output
			NBWMC_PID=`${BPPS} -r | grep nbwmc | grep -v grep | ${AWK} -F'|' '{print $2}'`
		else
			NBWMC_PID=`${BPPS} | grep nbwmc | grep -v grep | ${AWK} '{print $2}'`
		fi
		if [ "${NBWMC_PID}" != "" -a -x ${NBPATH}/nbwmc ] ; then
			echo "stopping the NetBackup Web Management Console"
			${NBPATH}/nbwmc -terminate 2>/dev/null
		fi

		if [ ${FTO_DNAT_SUPPORT} -ne 0 ] ; then
			if [ "$OSTYPE" = "SunOS" ] ; then
				# Detect nbmqbroker using raw output
				NBMQBROKER_PID=`${BPPS} -r | grep beam.smp | grep -v grep | ${AWK} -F'|' '{print $2}'`
				EPMD_PID=`${BPPS} -r | grep epmd | grep -v grep | ${AWK} -F'|' '{print $2}'`
			else
				NBMQBROKER_PID=`${BPPS} | grep beam.smp | grep -v grep | ${AWK} '{print $2}'`
				EPMD_PID=`${BPPS} | grep epmd | grep -v grep | ${AWK} '{print $2}'`
			fi
			if [ \( "${NBMQBROKER_PID}" != "" -a -x ${MQBROKERPATH}/nbmqbroker \)  -o \( "${EPMD_PID}" != "" -a -x ${ERLPATH}/epmd \) ] ; then
				echo "stopping the NetBackup Messaging Broker Service"
				${MQBROKERPATH}/nbmqbroker -terminate 2>/dev/null
			fi
		fi

		NBAUDIT_PID=`${BPPS} | grep nbaudit | grep -v grep | awk '{print $2}'`
		if [ "${NBAUDIT_PID}" != "" ] ; then
			echo "stopping the NetBackup Audit Manager"
			${NBPATH}/nbaudit -terminate 2>/dev/null
		fi

		NBAZD_PID=`${PS} | grep nbazd | grep -v grep | awk '{print $1}'`
		if [ "${NBAZD_PID}" != "" ] ; then
			echo "stopping the NetBackup Authorization daemon"
			kill ${NBAZD_PID} > /dev/null 2>/dev/null
		fi

		NBATD_PID=`${PS} | grep nbatd | grep -v grep | awk '{print $1}'`
		if [ "${NBATD_PID}" != "" ] ; then
			echo "stopping the NetBackup Authentication daemon"
			kill ${NBATD_PID} > /dev/null 2>/dev/null
		fi

		# Checking if nbaudit is shut down, if not, waiting
		# so that any left over audit records can be sent to database
		wait_for_process "nbaudit"

		# Need to stop NB_dbsrv last.

		NB_dbsrv_PID=`$BPPS | grep NB_dbsrv | grep -v grep | awk '{print $2}'`
		if [ "$NB_dbsrv_PID" != "" ] ; then
			echo "stopping the NetBackup Database Server"
			${NBPATH}/nbdbms_start_stop stop

			# Give NB_dbsrv a chance to come down before moving on
			wait_for_process "NB_dbsrv"
		fi
	fi

    VNETD_PID=`${PS} | grep vnetd | grep -v grep | awk '{print $1}'`
	if [ "${VNETD_PID}" != "" -a -x ${NBPATH}/vnetd ] ; then
		echo "stopping the NetBackup network daemon"
		${NBPATH}/vnetd -terminate 2> /dev/null
	fi

	NBPAS_PID=`${PS} | grep nbpas | grep -v grep | awk '{print $1}'`
	if [ "${NBPAS_PID}" != "" -a -x ${NBPATH}/nbpas ] ; then
		echo "stopping the NetBackup Privileged Access Service"
		${NBPATH}/nbpas -terminate 2> /dev/null
	fi

#
# Lock file used by LINUX init
#
	rm -f /var/lock/subsys/netbackup
	;;
status)
	procs=""
	MASTER_PROCS="nbevtmgr nbstserv vmd bprd bpdbm nbpem nbjm"
	MEDIA_PROCS="vmd"
	CLIENT_PROCS="bpcd vnetd"
	BPPS_OUTPUT="$TMPDIR/bpps_output.$$"

	if [ ${FTO_HARDENING_NVE60_PHASE2} -ne 0 ] ; then
		CLIENT_PROCS="${CLIENT_PROCS} nbpas"
	fi

	if [ ! -f "${BPCONF}" ] ; then
		echo "$BPCONF does not exist!"
		exit 3
	fi

	my_name=`$BPCLNTCMD -gethostname`

	is_master=`$BPCLNTCMD -is_master_server $my_name`
	if [ $? = 0 ] ; then
		echo "$is_master"
		echo ""
		procs=$MASTER_PROCS
	fi

	if [ "$procs" = "" ] ; then
		is_media=`$BPCLNTCMD -is_media_server $my_name`
		if [ $? = 0 ] ; then
			echo "$is_media"
			echo ""
			procs=$MEDIA_PROCS
		fi
	fi

	if [ "$procs" = "" ] ; then
		echo "$my_name is a client"
		echo ""
		procs=$CLIENT_PROCS
	fi

	$BPPS -a > $BPPS_OUTPUT 2>&1
	cat $BPPS_OUTPUT
	echo ""

	for i in $procs; do
		iPID=`grep $i $BPPS_OUTPUT | grep -v grep | grep -v nbproxy | head -1 | awk '{print $2}'`
		if [ "$iPID" = "" ] ; then
			echo "Critical process $i is NOT running..."
			RETURN=3
		fi
	done

	rm -f $BPPS_OUTPUT
	;;
start_msg)
	echo "Starting NetBackup"
	;;
stop_msg)
	echo "Stopping NetBackup"
	;;
*)
	echo "usage: $0 { start | stop | start_msg | stop_msg | status }"
	RETURN=1
	;;
esac
exit $RETURN
