#! /bin/sh
# $Header: bpps.sh,v 1.21 2002/07/22 19:51:00 djs Stab $

#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2002 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#
# Script for NetBackup to determine what processes are running.
#
progname='bpps'

# This script will display Media Manager processes if the -a switch
# is specified.  Processes that are not NetBackup related can also
# be added to program lists. 
# The -r option provides a way to list only the Advanced Reporter
# processes. Advanced Reporter processes will be included in requests
# specifying the -a option. These -r and -a changes are implemented
# on Solaris only as of the NBU 3.4 release.

Usage="usage: $progname [-a | -r]"
MySQL_bin="/usr/openv/db/bin"

# Note - do not add any more program/script names to this list.
# Since the shell on some unix platforms will not except a list 
# longer than progs1.  Be sure to adjust the lists for ALPHA also.
# and in bp.kill_all

progs1="(bpdm|bptm|xnb|bpbrm|bpsched|bpdbm|bpadm|xbpadm|bprd|bpcd|xbp|bp|bparchive|bpbackup|bpbkar|bpimagelist|bplist|bpinst|bprestore|bpbackupdb|bprecover|bpverify|bpduplicate|bpimport|bplabel)"

# Add new program/script names to this list
# Be sure to adjust the lists for ALPHA also.
# and in bp.kill_all

progs2="(backup_notify|restore_notify|session_notify|dbbackup_notify|userreq_notify|bpend_notify|bpstart_notify|bpdbjobs|bpfsmap|xbpmon|tar|bpbkar32|bpjava-msvc|bpjava-usvc|visd)"

# AdvancedReporter daemons. (Only implemented for Solaris and HP).
progs_aro="(ardbmon|arhttpd|armonitor|ardbd|arloader|arpusher)"

# MySQL utilities and daemon
progs_mysql="(start_nbdbd|nbdbsetport|nbdbsetpw|nbdbstop|nbdbupdatedb|nbdbd|nbdbdmon)"

### ALPHA PLATFORM RELATED *BEGIN*
### 
# The following lists are special for the Alpha platform. If bp is
# included with the other commands, wildcarding will happen causing 
# unwanted commands like bpps to print. So treat bp on its own.

progs1Alpha="(bpdm|bptm|xnb|bpbrm|bpsched|bpdbm|bpadm|xbpadm|bprd|bpcd|xbp|bparchive|bpbackup|bpbkar|bpimagelist|bplist|bpinst|bprestore|bpbackupdb|bprecover|bpverify|bpduplicate|bpimport|bplabel)"

progs2Alpha="(backup_notify|restore_notify|session_notify|dbbackup_notify|userreq_notify|bpend_notify|bpstart_notify|bpdbjobs|bpfsmap|xbpmon|tar|bpjava-msvc|bpjava-usvc|visd)"

progs3Alpha="(bp)"

# OPTIONAL feature for Alpha platform 
# (generally not required for other platforms)
#  To exclude specified processes from the bpps command:
#  -Create a line of pipe-separated strings that you wish to have
#  bpps bypass and place in /usr/openv/netbackup/.psexcludes. 
#  -An example /usr/openv/netbackup/.psexcludes file: 
#  (xterm|extra)	!Parentheses are required!
#	Processes can be excluded by replacing the word
#	"extra" above with whatever process you want excluded.

if [ -f /usr/openv/netbackup/.psexcludes ] ; then
	ALPHA_EXCLUDES=`cat /usr/openv/netbackup/.psexcludes`
else
	ALPHA_EXCLUDES=""
fi
###
### ALPHA PLATFORM RELATED *END*

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


if [ $HARDWARE = SUN4 ] ; then
	ECHO="/usr/5bin/echo"
else
	ECHO="/bin/echo"
fi

#
# process command line arguments
#

opt_all=0
opt_aro=0

#
# Since main's $* gets clobbered when bourne shell functions are called
# with arguments on an HP700 running 9.0x, stuff the argument list into
# a variable and implement shifts using awk.
#

args=$*

while [ -n "$args" ]
do
	option=`$ECHO "$args" | awk '{ print $1 }'`

	if [ "`$ECHO $option | cut -c1`" != "-" ] ; then
		$ECHO "$Usage"
		exit 1
	fi

	case $option in
	-a|-all|-v)	opt_all=1;;
	-r|-ar)		opt_aro=1;;
	*)	$ECHO "$progname: invalid option $option"
		$ECHO "$Usage"
		exit 1;;
	esac

	# simulate a "shift"
	args=`$ECHO "$args" | awk '{ for (i=2; i <= NF; i++) printf "%s ", $i }'`
done

# Advanced Reporter is only implemented on Solaris, HP and NT.
if [ $opt_aro -eq 1 ] ; then
	if [ "$HARDWARE" != "SOLARIS" -a "${HARDWARE}" != "HP9000-700" -a \
	     "${HARDWARE}" != "HP9000-800" ] ; then
		$ECHO "NetBackup Advanced Reporter is not available on this platform"
		exit 0
	fi
fi

if [ $opt_all -eq 1 ] ; then
	$ECHO "NB Processes"
	$ECHO "------------"
fi

# Because of the idiosyncracies of the ps command on different platforms, the
# grepping process to obtain bp processes has some variations.
if [ "$HARDWARE" = "SUN4" ] ; then
	processes=`/bin/ps -cax | egrep ' '$progs1'$' | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps -wuax$process | tail +2
	done
	processes=`/bin/ps -cax | egrep ' '$progs2'$' | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps -wuax$process | tail +2
	done
elif [ "$HARDWARE" = "ALPHA" -a -s /usr/openv/netbackup/.psexcludes ] ; then
	# ALPHA's 'ps -ea' returns command with args, so can't put $ in egrep regExpr.
	# Furthermore without $ egrep returns too much, so an 'egrep -v' option is
	# provided to filter out user-supplied processes.

	# Provide list of MySQL processes on ALPHA.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`/bin/ps -ea | egrep '[ /]'$progs_mysql | egrep -v $ALPHA_EXCLUDES | awk '{print $1}'`
		for process in $processes
		do
			/bin/ps wwuax$process | tail +2
		done
	fi

	processes=`/bin/ps -ea | egrep '[ /]'$progs1Alpha | egrep -v $ALPHA_EXCLUDES | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
	processes=`/bin/ps -ea | egrep '[ /]'$progs2Alpha | egrep -v $ALPHA_EXCLUDES | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
	processes=`/bin/ps -ea | egrep '[ /]'$progs3Alpha'$' | egrep -v $ALPHA_EXCLUDES | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
elif [ "$HARDWARE" = "ALPHA" ] ; then

	# Provide list of MySQL processes on ALPHA.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`/bin/ps -ea | egrep '[ /]'$progs_mysql | awk '{print $1}'`
		for process in $processes
		do
			/bin/ps wwuax$process | tail +2
		done
	fi

	processes=`/bin/ps -ea | egrep '[ /]'$progs1Alpha | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
	processes=`/bin/ps -ea | egrep '[ /]'$progs2Alpha | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
	processes=`/bin/ps -ea | egrep '[ /]'$progs3Alpha'$' | awk '{print $1}'`
	for process in $processes
	do
		/bin/ps wwuax$process | tail +2
	done
elif [ "$HARDWARE" = "HP9000-700" -o "$HARDWARE" = "HP9000-800" ] ; then
	#
	# ps on hp only prints 15 characters of the command name.  Commands
	# like dbbackup_notify get missed.  Use the -o option to widen the
	# command name portion of the report (UNIX95 must be set to get
	# this XPG4 behavior).

	# Provide option on HP to list Advanced Reporter daemons only.
	if [ $opt_aro -eq 1 ] ; then
		processes=`env UNIX95=1 /bin/ps -ea -o pid,comm | egrep ' '$progs_aro'$' | awk '{print "-p " $1}'`
		#  Incredibly disgusting hack just for NBAR processes on HP.  Due to the
		#  peculiarities of the OS, ardbmon appears like this
		#  "/bin/sh /usr/openv/nbar/bin/ardbmon" and gets missed.  Do a special
		#  check just for it.
		tmp_processes=`env UNIX95=1 /bin/ps -ea -o pid,args | egrep ardbmon'$' | awk '{print "-p " $1}'`
		if test -n "$tmp_processes" ; then
			processes="$processes $tmp_processes"
		fi
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
		exit 0
	fi

	# Provide list of MySQL processes on HP.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`env UNIX95=1 /bin/ps -ea -o pid,comm | egrep ' '$progs_mysql'$' | awk '{print "-p " $1}'`
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
	fi

	processes=`env UNIX95=1 /bin/ps -ea -o pid,comm | egrep ' '$progs1'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	processes=`env UNIX95=1 /bin/ps -ea -o pid,comm | egrep ' '$progs2'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	# find jnbSA/jbpSA processes.
	/bin/ps -aef | grep jre | grep openv

elif [ "$HARDWARE" = "NCR" -o "$HARDWARE" = "PYRAMID" -o \
	"$HARDWARE" = "SEQUENT4.2" ] ; then
	#
	# ps on these platforms only prints 8 characters of the command name.
	# Commands like bpduplicate get missed.  Use the -o option to widen
	# the command name portion of the report and since it pads blanks on
	# the end, use sed to strip them off.

	processes=`/bin/ps -ea -o pid,comm | sed 's/[ ]*$//' | egrep ' '$progs1'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	processes=`/bin/ps -ea -o pid,comm | sed 's/[ ]*$//' | egrep ' '$progs2'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
elif [ "$HARDWARE" = "SGI" ] ; then
	# ps(1) on IRIX 6.2 puts a space on the end of "ps -ea"
	# and IRIX 5.2 doesn't, so chop off the space if it's there.
	#
	# ps on sgi only prints 9 characters of the command name.  Commands
	# like bpduplicate get missed.  Use the -o option to widen the command
	# name portion of the report.  The long XXX string is required for this
	# to work...do NOT delete it.

	# Provide list of MySQL processes on SGI.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`/bin/ps -ea -o pid,comm=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | sed 's/ $//' | egrep ' '$progs_mysql'$' | awk '{print "-p " $1}'`
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
	fi

	processes=`/bin/ps -ea -o pid,comm=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | sed 's/ $//' | egrep ' '$progs1'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	processes=`/bin/ps -ea -o pid,comm=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | sed 's/ $//' | egrep ' '$progs2'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
elif [ "$HARDWARE" = "SOLARIS" ] ; then
	# ps on solaris only prints 8 characters of the command name. commands
	# like bpduplicate get missed. use solaris -o option to widen the commandName
	# portion of the report.  The long XXX string is required for this to
	# work...do NOT delete it.

	# Provide option on Solaris to list Advanced Reporter daemons only.
	if [ $opt_aro -eq 1 ] ; then
		processes=`/bin/ps -ea -o pid,fname=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | egrep ' '$progs_aro'$' | awk '{print "-p " $1}'`
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
		exit 0
	fi

	# Provide list of MySQL processes on Solaris.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`/bin/ps -ea -o pid,fname=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | egrep ' '$progs_mysql'$' | awk '{print "-p " $1}'`
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
	fi

	processes=`/bin/ps -ea -o pid,fname=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | egrep ' '$progs1'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	processes=`/bin/ps -ea -o pid,fname=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | egrep ' '$progs2'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	# find jnbSA/jbpSA processes.
	/bin/ps -ef | grep jre | grep openv
else
	# WARNING:  ps -eaf (which would give us more room for long
	# names) causes the output to contain any arguments.  Since we are
	# grepping for the program/script names specifically at the end,
	# those situations are missed.  Don't use it.

	if [ "$HARDWARE" = "LINUX" ] ; then
		DASH_F=-fww
	else
		DASH_F=-f
	fi

	# Provide list of MySQL processes on LINUX and RS6000.
	if [ -f ${MySQL_bin}/nbdbd ] ; then
		processes=`/bin/ps -ea | egrep ' '$progs_mysql'$' | awk '{print "-p " $1}'`
		if test -n "$processes" ; then
			/bin/ps $DASH_F $processes | tail +2
		fi
	fi

	processes=`/bin/ps -ea | egrep ' '$progs1'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	processes=`/bin/ps -ea | egrep ' '$progs2'$' | awk '{print "-p " $1}'`
	if test -n "$processes" ; then
		/bin/ps -f $processes | tail +2
	fi
	# find jnbSA/jbpSA processes.
	/bin/ps -f | grep jre | grep openv
fi

if [ $opt_all -eq 1 ] ; then
	$ECHO 
	$ECHO 

	if [ -f /usr/openv/volmgr/bin/vmps ] ; then
		VMPS=/usr/openv/volmgr/bin/vmps
	else
		$ECHO "/usr/openv/volmgr/bin/vmps is not installed"
		$ECHO "$progname: could not display Media Manager processes"
		exit 1
	fi

	$ECHO "MM Processes"
	$ECHO "------------"

	$VMPS
	stat=$?
	if [ $stat -ne 0 ] ; then
		$ECHO "$progname: could not display Media Manager processes"
		exit $stat
	fi

fi

if [ $opt_all -eq 1 ] ; then
	do_it=0
	if [ "${HARDWARE}" = "SOLARIS" ] ; then
		if [ -f /opt/SUNWnbaro/bin/ardbd ] ; then
			do_it=1
			ps_command="/bin/ps -ea -o pid,fname=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
		fi
	elif [ "${HARDWARE}" = "HP9000-700" -o "${HARDWARE}" = "HP9000-800" ] ; then
		if [ -f /usr/openv/nbar/bin/ardbd ] ; then
			do_it=1
			ps_command="env UNIX95=1 /bin/ps -ea -o pid,comm"
		fi
	fi

	if [ ${do_it} -eq 1 ] ; then
		$ECHO
		$ECHO
		$ECHO "ARO Processes"
		$ECHO "-------------"
		processes=`eval ${ps_command} | egrep ' '${progs_aro}'$' | awk '{print "-p " $1}'`
		#  Incredibly disgusting hack just for NBAR processes on HP.  Due to the
		#  peculiarities of the OS, ardbmon appears like this
		#  "/bin/sh /usr/openv/nbar/bin/ardbmon" and gets missed.  Do a special
		#  check just for it.
		if [ "${HARDWARE}" = "HP9000-700" -o "${HARDWARE}" = "HP9000-800" ] ; then
			tmp_processes=`env UNIX95=1 /bin/ps -ea -o pid,args | egrep ardbmon'$' | awk '{print "-p " $1}'`
			if test -n "$tmp_processes" ; then
				processes="$processes $tmp_processes"
			fi
		fi
		if test -n "$processes" ; then
			/bin/ps -f $processes | tail +2
		fi
	fi
fi
