#!/usr/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r720 src/43haes/usr/sbin/cluster/sa/printServer/sbin/cl_print_server_powerpc_monitor.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2010 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)27	1.6  src/43haes/usr/sbin/cluster/sa/printServer/sbin/cl_print_server_powerpc_monitor.sh, hacmp, 61haes_r720, 1527A_hacmp720 6/24/15 05:31:02
###########################################################
#
# Name:         cleanup
#
# Description:  Cleaning up required resources created.
#
# Arguments:    none
#
# Returns:      does not return
#
###########################################################
cleanup()
{
	if [[ $VERBOSE_LOGGING == 'high' ]]
	then
	    PS4_FUNC=cleanup
	    set -x
	fi

	#
	: Cleanup activity
	:       removing print queue
	:       removing the temporary file
	#
	KLIB_SA_logmsg INFO 5 1 print_server_sa.cat "Cleanup activity\n"
	KLIB_SA_logmsg INFO 5 2 print_server_sa.cat "Removing print queue\n"
	integer LIMIT=3
	for (( TRIES=0 ; TRIES<LIMIT ; TRIES++ ))
	do
	  /usr/lib/lpd/pio/etc/piomisc_ext rmpq_other \
"${PRINT_QUEUE}:$TEMP_FILE" 'file' >> $LOG_FILE 2>&1
	  lpstat -p"${PRINT_QUEUE}" >> $LOG_FILE 2>&1
	  (( $? !=0 )) && {
	    break
	  }
	  sleep 2
	done
	KLIB_SA_logmsg INFO 5 3 print_server_sa.cat "Removing temporary file created \
for printing to a file using print queue\n"
	/usr/bin/rm -rf /dev/$TEMP_FILE >> $LOG_FILE 2>&1
}

###########################################################
#
# Name:		powerpc_monitor
#
# Description:	Verify the health of PowerPC print subsystem
#
# Arguments:	none
#
# Returns:
#		0 on SUCCESS
#		1 on FAILURE
#
###########################################################
powerpc_monitor()
{
	if [[ $VERBOSE_LOGGING == 'high' ]]
	then
	    PS4_FUNC=powerpc_monitor
	    set -x
	fi

	TEMP_FILE="temp_powerpc_$$"
	PRINT_QUEUE="temp_queue_$$"
	DAEMON="qdaemon"
	KLIB_SA_logmsg INFO 5 4 print_server_sa.cat "%1\$s print \
subsystem Health check initiated\n" "PowerPC"
	#
	: printing to a temporary file using PowerPC print subsystem
	: for PowerPC print subsystem Health check
	#
	KLIB_SA_logmsg INFO 5 5 print_server_sa.cat "Creating a temporary file in /dev directory \
for printing to a file using %1\$s print subsystem\n" "PowerPC"
	/usr/bin/touch /dev/${TEMP_FILE} >> $LOG_FILE 2>&1
	(( $? != 0 )) && {
                #
                : We are unable to create a new file in /dev, this could be because of
                : problems with system but print subsystem might function properly.
                : So just logging a message with returning the value of SUCCESS.
                #
                KLIB_SA_logmsg WARN 5 6 print_server_sa.cat "The file creation failed \
for printing to a file using %1\$s print subsystem\n" "PowerPC"
                KLIB_SA_logmsg WARN 5 7 print_server_sa.cat "The test print to a file \
using %1\$s print subsystem can't be initiated\n" "PowerPC"
                return 0
        }

	#
	: Provide required permission to the file
	#
	KLIB_SA_logmsg INFO 5 8 print_server_sa.cat "Providing required \
permission to temporary file created\n"
	/usr/bin/chmod 666 /dev/${TEMP_FILE} >> $LOG_FILE 2>&1
	(( $? != 0 )) && {
                #
                : We are unable to provide required permissions to the file, this
                : could be because of problems with system but print subsystem
                : might function properly.
                : So just logging a message with returning the value of SUCCESS.
                #
                KLIB_SA_logmsg WARN 5 9 print_server_sa.cat "The required permission modification \
to the temporary file failed.\n"
                KLIB_SA_logmsg WARN 5 7 print_server_sa.cat "The test print to a file \
using %1\$s print subsystem can't be initiated\n" "PowerPC"

                #
                : Removing the file created for printing
                #
                /usr/bin/rm -rf /dev/${TEMP_FILE} >> $LOG_FILE 2>&1
                return 0
        }
	
	#
	: Create a print queue printing to a file
	#
	KLIB_SA_logmsg INFO 5 10 print_server_sa.cat "Creating \
a %1\$s print queue printing to a file\n" "PowerPC"
	/usr/lib/lpd/pio/etc/piomkpq -A 'file' -p 'generic' \
-d "$TEMP_FILE" -D asc -q "$PRINT_QUEUE" >> $LOG_FILE 2>&1
	(( $? != 0 )) && {
		#
                : We are unable to create a print queue.
                #
                KLIB_SA_logmsg ERROR 5 11 print_server_sa.cat "The creation of print queue %1\$s \
for %2\$s subsystem failed.\n" "$PRINT_QUEUE" "PowerPC"

		#
                : Removing the file created for printing
                #
                /usr/bin/rm -rf /dev/${TEMP_FILE} >> $LOG_FILE 2>&1
                return 1
        }

	#
	: Test print to the queue
	#
	KLIB_SA_logmsg INFO 5 12 print_server_sa.cat "Test print to the file \
using %1\$s print subsystem\n" "PowerPC"
	/usr/sbin/lptest 5 5 | qprt -P $PRINT_QUEUE >> $LOG_FILE 2>&1
	(( $? != 0 )) && {
                #
                : We are unable to initate print using PowerPC print subsystem
                #
                KLIB_SA_logmsg ERROR 5 13 print_server_sa.cat "The print request using %1\$s print \
subsystem failed\n" "PowerPC"

		#
		: Cleanup here
		#
		cleanup

                return 1
        }

	#
	: sleeping here for sometime for the print queue to become empty
	#
	sleep 2

	#
	: Ensure that the printing is done, otherwise sleep for more
	: time before indicating fail
	#
	integer LIMIT=3
	for (( TRIES=0 ; TRIES<LIMIT ; TRIES++ ))
	do
		NUM_LINES=$(/usr/bin/cat /dev/$TEMP_FILE | LC_ALL=C /usr/bin/wc -l 2>/dev/null)
		NUM_LINES=${NUM_LINES// /}

		if (( $NUM_LINES == 0 ))
		then
			#
			: If qdaemon is active then wait for more time to get the file to be printed 
			#

			LC_ALL=C /usr/bin/lssrc -s $DAEMON | grep -iw active >> $LOG_FILE 2>&1

			(( $? == 0 )) && {
				#
				: Provide more time to print file before indicating fail
				#
				KLIB_SA_logmsg INFO 3 8 print_server_sa.cat "%1\$s daemon is active\n" "$DAEMON"
				sleep 3
			}
		else
			break
		fi
	done

	#
	: Verify the printing is done or not after counting
	: the number of lines in the file
	#
	KLIB_SA_logmsg INFO 5 14 print_server_sa.cat "The number of lines in the temporary file \
should be greater than 0 after print\n"
	NUM_LINES=$(/usr/bin/cat /dev/$TEMP_FILE | LC_ALL=C /usr/bin/wc -l 2>/dev/null)

	#
	: Removing white spaces
	#
	NUM_LINES=${NUM_LINES// /}

	#
	: if NUM_LINES is greater than 0
	:	print subsystem is active
	: else
	:	print subsystem is not active
	#
	(( $NUM_LINES > 0 )) && {
		KLIB_SA_logmsg INFO 3 1 print_server_sa.cat "%1\$s print \
subsystem is active\n" "PowerPC"

		#
                : Cleanup here
                #
		cleanup

		return 0
	}

	#
	: If we are here then print subsystem is not active
	#
	KLIB_SA_logmsg ERROR 3 2 print_server_sa.cat "%1\$s print \
subsystem is not active\n" "PowerPC"

	#
	: Cleanup here
	#
	cleanup

	return 1
}

###############################################################################
# Main program procedure.
###############################################################################
PROGNAME=$(basename ${0})
PATH="$($(dirname ${0})/../../../utilities/cl_get_path all)"
PATH=$PATH:/usr/bin:/etc:/usr/sbin:/usr/ucb:/sbin
FPATH_BASE=/usr/es/lib/ksh93
FPATH=$FPATH_BASE/hacmp:$FPATH_BASE/sa:$FPATH_BASE/aix:$FPATH_BASE/aix/odm
export PATH FPATH

# We need to set this so that we can gather enough details in
# /var/hacmp/log/clappmond.<INSTANCE_OWNER>.monitor.log for debugging.
export VERBOSE_LOGGING=${VERBOSE_LOGGING:-"high"}

eval export $(cllsparam -n $(clodmget -f nodename HACMPcluster))

if [[ $VERBOSE_LOGGING == 'high' ]]
then
    PS4_TIMER=true
    set -x
    version='1.6'
fi

#
: Load the common variables
#
. /usr/es/sbin/cluster/sa/printServer/sbin/cl_print_server_common_variables

LOG_FILE=$(clodmget -q name=$KLIB_HACMPLOG_ENTRY -f value -d "=" HACMPlogs)
LOG_FILE=${LOG_FILE//\"/}
LOG_FILE="$LOG_FILE/$KLIB_HACMPLOG_ENTRY"

[[ "$VERBOSE_LOGGING" == "high" ]] && set -x

BASENAME=$(basename $0)

echo "######################################################" >> $LOG_FILE
dspmsg -s 5 print_server_sa.cat 15 "# POWERPC PRINT SUBSYSTEM HEALTH MONITOR" >> $LOG_FILE
echo "\n######################################################" >> $LOG_FILE

#
: PowerPC print subsystem health monitoring
#
powerpc_monitor

exit $?
