#!/bin/ksh

#    SAM-QFS_notice_begin
#
#      Solaris 2.x Sun Storage & Archiving Management File System
#
#      Copyright (c) 2004 Sun Microsystems, Inc.
#      All Rights Reserved.
#
#      Government Rights Notice
#      Use, duplication, or disclosure by the U.S. Government is
#      subject to restrictions set forth in the Sun Microsystems,
#      Inc. license agreements and as provided in DFARS 227.7202-1(a)
#      and 227.7202-3(a) (1995), DRAS 252.227-7013(c)(ii) (OCT 1988),
#      FAR 12.212(a)(1995), FAR 52.227-19, or FAR 52.227-14 (ALT III),
#      as applicable.  Sun Microsystems, Inc.
#
#    SAM-QFS_notice_end
#
# ident	$Id: samadm,v 1.5 2004/08/04 16:14:33 priyak Exp $
#
# samadm:
# This script is used to start, stop, restart and get status of the
# SAM-QFS Manager daemon (sam-mgmtrpcd).  It is also used to set
# the trace level of the SAM-QFS Manager.  
#
# Accepts start|stop|restart|config|trace
# Exits with a value 1 on error
#
#
# start		Start daemon (do not modify inittab)
# stop		Stop daemon (it will auto-restart if so configured)
# restart	Restart daemon (do not modify inittab)
# config -n	Remove any inittab settings for daemon
# config -a	Configure inittab to start daemon on everytime it dies
# config -b	Configure inittab to start daemon on a system boot
# status        shows SAM-QFS Manager version and configuration status
# trace {level} set trace level of SAM-QFS Manager
#
#

BOOT_ENTRY="sfad:3:once:/opt/SUNWsamfs/sbin/sam-mgmtrpcd"
ECHO="/usr/bin/echo"
INITTAB="/etc/inittab"
RESPAWN_ENTRY="sfad:3:respawn:/opt/SUNWsamfs/sbin/sam-mgmtrpcd"
SAM_MGMT_RPCD="/opt/SUNWsamfs/sbin/sam-mgmtrpcd"
PKGINFO="/usr/bin/pkginfo"

# for SAM-QFS Manager smcwebserver
USR_SBIN_DIR=/usr/sbin
SMREG=${USR_SBIN_DIR}/smreg
SMCWEBSERVER=${USR_SBIN_DIR}/smcwebserver
PACKAGE_PATH=com.sun.netstorage.samqfs.web
SMCLOG_DIR=/var/log/webconsole
TRACE_NAME=samqfsui.trace_syslog
SMCPID_FILE=/var/run/smcwebserver.pid

# ---------------------
#	FUNCTIONS
# ---------------------
#
# Usage
#
# Depending upon which pkgs are installated, show appropriate usage
#
usage() {

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	if [ $samfs_exists == 1 ]; then
		
		pkg_exists SUNWqfsr
		samfs_exists=$?
	fi

	pkg_exists SUNWsamqfsuiu
	samfsui_exists=$?

	if [ $samfs_exists == 0 -a $samfsui_exists == 0 ]; then

		$ECHO "Usage: samadm sub-command [option]"

		usage_detail_samfs

		usage_detail_samfsui

	elif [ $samfs_exists == 0 -a $samfsui_exists == 1 ]; then

		$ECHO "Usage: samadm sub-command [option]"

		usage_detail_samfs

	elif [ $samfs_exists == 1 -a $samfsui_exists == 0 ]; then

		$ECHO "Usage: samadm sub-command [option]"

		usage_detail_samfsui

	else
		# should never arise, how did samadm get there without pkg?
		# may be incomplete pkg removal
		exit 1
	fi

	$ECHO
	$ECHO "For more information, see samadm(1M)."
	$ECHO

	exit 1
}


#
# Usage details if SUNWsamfsr/u or SUNWqfsr/u is installed
#
usage_detail_samfs() {

	$ECHO
	#
	$ECHO "Sub-command:"
	$ECHO
	#
	$ECHO "start\t\tStart SAM-QFS Manager daemon"
	$ECHO "\t\tDo not configure daemon to auto-restart"
	$ECHO
	#
	$ECHO "stop\t\tStop SAM-QFS Manager daemon"
	$ECHO
	#
	$ECHO "restart\t\tRestart SAM-QFS Manager daemon"
	$ECHO
	#
	$ECHO "config -n\tStop SAM-QFS Manager daemon"
	$ECHO "\t\tDisable auto-restart feature of daemon"
	$ECHO
	#
	$ECHO "config -a\tStart SAM-QFS Manager daemon"
	$ECHO "\t\tAuto-restart daemon everytime it dies"
	$ECHO
	#
	$ECHO "config -b\tStart SAM-QFS Manager daemon"
	$ECHO "\t\tAuto-restart daemon at system reboot only"
	$ECHO
	#
	$ECHO "status\t\tDisplay config info for SAM-QFS Manager daemon"
	$ECHO
	#
}


#
# Usage details if SUNWsamqfsuir/u is installed
#
usage_detail_samfsui() {

	$ECHO
	#
	$ECHO "Sub-command:"
	$ECHO
	$ECHO "trace {level}\tTrace SAM-QFS Manager execution"
	$ECHO
	$ECHO "\t1\tTrace important messages only"
	$ECHO "\t2\tTrace moderately important message, include"
	$ECHO "\t\tmessages in trace level 1"
	$ECHO "\t3\tTrace all messages"
	$ECHO "\toff\tTurn off tracing"
	$ECHO
	$ECHO "\tIf no options are given, display current trace level"
	$ECHO
}


#
# Return pid of named process in variable "pid"
#
pidproc() {
	pid=`/usr/ucb/ps -ax |
		/usr/bin/awk '{print $1 " " $5}' |
		/usr/bin/sed -e 's/\/.*\///' |
		/usr/bin/grep " ${1}$" |
		/usr/bin/awk '{print $1}'`
}


#
# Kill named process
#
killproc() {
	pidproc $1

	if [ "${pid}" != "" ]; then

		$ECHO "Stopping SAM-QFS Manager daemon"
		$ECHO
		kill -9 $pid	# Stopping  SAM-QFS Manager daemon

		if [ $? != 0 ]; then

			$ECHO "SAM-QFS Manager daemon is not running"
		fi

	fi
}


#
# Checks if package is installed
# INPUT	: package name
# RETURN: 0 if package exits, else 1
#
pkg_exists() {

	$PKGINFO $1 >> /dev/null 2>&1
		
	return $?		# return 0 if package exists, else 1

}


#
# Checks if SAM-QFS Manager daemon is running
# RETURN: 0 if daemon is running, else 1
#
is_mgmtrpcd_running() {

	pidproc sam-mgmtrpcd

	if [ "${pid}" == "" ]; then
		return 1	# SAM-QFS Manager daemon is not running
	else 
		return 0	# SAM-QFS Manager daemon is running

	fi
}


#
# Checks if SAM-QFS Manager entry exists in /etc/inittab
# RETURN: 0 if respawn entry exists, 1 if boot entry and 2 if no entry
#
entry_exists() {

	respawn_entry_exists
	respawn=$?

	boot_entry_exists
	boot=$?

	if [ $respawn == 0 ]; then

		return 0
	elif [ $boot == 0 ]; then

		return 1
	else

		return 2
	fi
}


boot_entry_exists() {

	/bin/grep "${BOOT_ENTRY}" $INITTAB >/dev/null

	return $?		# return 0 if entry exists, else 1
}


respawn_entry_exists() {

	/bin/grep "${RESPAWN_ENTRY}" $INITTAB >/dev/null

	return $?		# return 0 if entry exists, else 1
}


#
# Remove the SAM-QFS Manager entry from /etc/inittab
#
remove_from_inittab() {

	entry_exists

	if [ $? != 2 ]; then

		# Delete entry from /etc/inittab

		sed "/sfad:3:/d" $INITTAB > $INITTAB.tmp
		mv $INITTAB.tmp $INITTAB
		chmod 644 $INITTAB

		kill -HUP 1
	fi
}


#
# Add given entry to $INITTAB
# INPUT: entry with required action
# RETURN: 0 on success, 1 on failure
#
add_to_inittab() {

	entry_exists

	if [ $? == 2 ]; then

		# A previous entry does not exist
		# Add entry to /etc/initttab

		$ECHO $1 >> $INITTAB

		if [ $? != 0 ]; then
			$ECHO "Failed to add entry to $INITTAB"
			$ECHO "Check logs and inittab"
			$ECHO

			return 1	# failure
		else
			# If a previous instance of sam-mgmtrpcd is
			# already running, then init still respawns
			# another process

			kill -HUP 1
		fi
	fi

	return 0	# if entry already exists in $INITTAB, return success
}


#
# Start sam-mgmtrpcd daemon
# RETURN: 0 on success, 1 on failure
#
start_mgmtrpcd() {

	$ECHO "Starting SAM-QFS Manager daemon"
	$ECHO

	$SAM_MGMT_RPCD

	pidproc sam-mgmtrpcd

	if [ "${pid}" = "" ]; then

		$ECHO "Failed to start SAM-QFS Manager daemon"
		$ECHO "Check logs and inittab"
		$ECHO

		return 1
	else 
		entry_exists

		if [ $? == 2 ]; then

			$ECHO "SAM-QFS daemon is not configured to auto-restart"
			$ECHO
			$ECHO "To enable the auto-restart feature"
			$ECHO "stop the daemon and then use samadm config -a"
			$ECHO
		fi
	fi

	return 0
}


#
# Set the trace level to the given level
# INPUT: level
# 
set_trace_level() {

	$ECHO "Setting trace level to $1"
	${SMREG} add --properties ${PACKAGE_PATH}.tracedevice=${SMCLOG_DIR}/${TRACE_NAME} ${PACKAGE_PATH}.tracelevel=$1

}


#
# Turn off tracing
#
set_trace_off() {

	$ECHO "Turning off tracing"
	${SMREG} remove --properties ${PACKAGE_PATH}.tracedevice ${PACKAGE_PATH}.tracelevel

}

#
# Display the current trace level
#
show_current_level() {

	# show current tracing level
	tracelevel=`/usr/sbin/smreg list |grep com.sun.netstorage.samqfs.web.tracelevel | awk -F=  '{print $2}'`

        if [ -z $tracelevel ]; then
                echo "Current Trace Level: off"
        else
	        echo "Current Trace Level: $tracelevel"
	fi
}


# ----------------------------------
# 		MAIN PROGRAM EXECUTION
# ----------------------------------

$ECHO	# first blank line

# option is mandatory with subcommand 'config',
#
# with subcommand 'trace', if options are given the trace is set to the desired
# if no options are given, just display the current trace setting
#
# for subcommands (start| stop| restart| status), no options are accepted

if [ $# -gt 0 ]; then
	if [ "$1" = "config" ]; then

		if [ $# != 2 ]; then
			usage
		fi
	elif [ "$1" = "trace" ]; then
		if [ $# != 1 -a $# != 2 ]; then
			usage
		fi
	else

		if [ $# != 1 ]; then
			usage
		fi
	fi

else	
	# arg count = 0
	usage
fi


case "$1" in
#####################################################
#	Status SAM-QFS Manager daemon		    #
#####################################################
'status')

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	pkg_exists SUNWqfsr
	qfs_exists=$?
	
	if [ $samfs_exists != 0 -a $qfs_exists != 0 ]; then
		usage
	fi

	# correct usage

	is_mgmtrpcd_running
	mgmt_running=$?
	if [ $mgmt_running != 0 ]; then
		$ECHO "SAM-QFS Manager daemon is not running"
	else 
		
		$ECHO "SAM-QFS Manager daemon is running"
		$ECHO
		$SAM_MGMT_RPCD -v
	fi

	$ECHO

	# Parse the SAM-QFS Manager entry in $INITTAB

	entry_exists

	case $? in
	0)
		$ECHO "The SAM-QFS Manager daemon is configured to auto-restart"
		$ECHO
		$ECHO "To stop the daemon and disable auto-restart feature use:"
		$ECHO "samadm config -n"
		$ECHO
		;;
	1)	
		$ECHO "The SAM-QFS Manager daemon is configured to auto-restart at system reboot"
		$ECHO
		;;
	2)	
		if [ $mgmt_running == 0 ]; then
			$ECHO "Use samadm stop to stop SAM-QFS Manager daemon"
		else 
			$ECHO "To start daemon and enable auto-restart feature use:"
			$ECHO "samadm config -a"
		fi
		$ECHO
		;;
	esac
	;;
#####################################################
#	Start SAM-QFS Manager daemon		    #
#####################################################

'start')

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	pkg_exists SUNWqfsr
	qfs_exists=$?

	if [ $samfs_exists != 0 -a $qfs_exists != 0 ]; then
		usage
	fi

	# correct usage

	is_mgmtrpcd_running

	if [ $? == 0 ]; then
		$ECHO "SAM-QFS Manager daemon is running"
		$ECHO

		exit 0	# success
	fi

	start_mgmtrpcd

	;;
#####################################################
#	Stop SAM-QFS Manager daemon		    #
#####################################################

'stop')

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	pkg_exists SUNWqfsr
	qfs_exists=$?

	if [ $samfs_exists != 0 -a $qfs_exists != 0 ]; then
		usage
	fi

	killproc sam-mgmtrpcd

	entry_exists

	case $? in
	0)
		$ECHO "SAM-QFS Manager daemon is configured to auto-restart"
		$ECHO
		$ECHO "To stop daemon and disable auto-restart feature use:"
		$ECHO "samadm config -n"
		$ECHO
		;;
	1)
		$ECHO "SAM-QFS Manager daemon is configured to auto-restart at system reboot"
		$ECHO
		$ECHO "To stop daemon and disable auto-restart feature use:"
		$ECHO "samadm config -n"
		$ECHO
		;;
	esac
	;;
#####################################################
#	Restart SAM-QFS Manager daemon		    #
#####################################################

'restart')

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	pkg_exists SUNWqfsr
	qfs_exists=$?

	if [ $samfs_exists != 0 -a $qfs_exists != 0 ]; then
		usage
	fi

	# Stop the daemon
	killproc sam-mgmtrpcd

	entry_exists

	# if respawn entry exists in /etc/inittab,
	# then init will restart the daemon

	if [ $? != 0 ]; then

		# Start the daemon
		start_mgmtrpcd
	else
		# init will restart the daemon
		$ECHO
		$ECHO "Starting SAM-QFS Manager daemon"
		$ECHO
	fi
	;;
#####################################################
#	Configuring auto-restart		    #
#####################################################
'config')

	pkg_exists SUNWsamfsr
	samfs_exists=$?

	pkg_exists SUNWqfsr
	qfs_exists=$?

	if [ $samfs_exists != 0 -a $qfs_exists != 0 ]; then
		usage
	fi

	# Process the configuration option
	OPTIND=2	# start after the subcommand

	while getopts :abn OPTIONS
	do
		case $OPTIONS in
		n)
		#####################################################
		#	Stop daemon and disable auto-restart        #
		#####################################################

			entry_exists

			case $? in
			0)
				# init will also stop the daemon

				$ECHO "Disabling auto-restart feature"
				$ECHO
				$ECHO "Stopping SAM-QFS Manager daemon"
				$ECHO

				remove_from_inittab
				;;
			1)
				$ECHO "Disabling auto-restart feature"
				$ECHO
				remove_from_inittab
				# stop the daemon

				is_mgmtrpcd_running
				if [ $? == 0 ]; then
					killproc sam-mgmtrpcd
				fi
				;;
			*)
				# stop the daemon

				is_mgmtrpcd_running
				if [ $? == 0 ]; then
					killproc sam-mgmtrpcd
				fi
				;;
			esac
				
			;;

		b)
		#####################################################
		# Start SAM-QFS Manager and enable auto-restart at system boot
		#####################################################

			respawn_entry_exists
			if [ $? == 0 ]; then

				$ECHO "SAM-QFS Manager daemon is configured to"
				$ECHO "auto-restart everytime the daemon dies"
				$ECHO
				$ECHO "Use 'samadm config -n' to first disable"
				$ECHO "that feature and then use this option"

				exit 1
			fi
			# no entry found in /etc/inittab
			add_to_inittab $BOOT_ENTRY
			if [ $? == 0 ]; then
				$ECHO "SAM-QFS Manager daemon is configured to auto-restart at system reboot"
			fi

			# Start the daemon
			is_mgmtrpcd_running
			if [ $? != 0 ]; then
				start_mgmtrpcd
			fi
			;;

		a)
		#####################################################
		# Start SAM-QFS Manager and enable auto-restart (respawn)
		#####################################################

			respawn_entry_exists
			if [ $? == 0 ]; then
				exit 0
			fi

			boot_entry_exists
			if [ $? == 0 ]; then
				$ECHO "SAM-QFS Manager daemon is configured to"
				$ECHO "auto-restart only at system reboot"
				$ECHO
				$ECHO "Use 'samadm config -n' to first disable"
				$ECHO "that feature and then use this option"
				$ECHO
				exit 1
			fi
			
			is_mgmtrpcd_running
			if [ $? == 0 ]; then
				$ECHO "Stop SAM-QFS Manager daemon and then use this option" 
				$ECHO
				exit 1
			fi
				
			add_to_inittab $RESPAWN_ENTRY
			if [ $? == 0 ]; then
				$ECHO "SAM-QFS Manager daemon is configured to auto-restart everytime the daemon dies"
				$ECHO
				$ECHO "Starting SAM-QFS Manager daemon"
			fi
			# init will start the daemon
			;;


		?)	usage 
			;; 
		*)	usage 
			;; 
		esac 
	done 
	;;
#####################################################
#	SAM-QFS Manager (client side tracing)       #
#####################################################
'trace')
	pkg_exists SUNWsamqfsuiu

	if [ $? != 0 ]; then
		usage
	fi

	if [ $# == 1 ]; then
		show_current_level
		$ECHO
		exit 0
	fi	

	case $2 in
	'off')	set_trace_off
		;;
	[1-3])	set_trace_level $2
		;;
	*)	$ECHO "Invalid trace level value"
		$ECHO
		usage
        	;;
	esac
	;;
*)
	usage
	;;
esac
exit 0
