#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# perf720 src/perf/cmdperft/usr/bin/pmcfg/config_pm.sh 1.8.5.17 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2009,2014 
# 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 
# @(#)09	1.8.4.8  src/perf/cmdperft/usr/bin/pmcfg/config_pm.sh, PMAIX, perf61V 8/15/12 08:59:50
# @(#)09        1.8.4.10  src/perf/cmdperft/usr/bin/pmcfg/config_pm.sh, PMAIX, perf61X, x2012_34A5 8/18/12 03:15:01
# set -x

#config_pm.sh -T <ARGS from SMIT>   --> To enable or Disable PM Data transmission
#config_pm.sh -R <ARGS from SMIT>   --> To Set the data retention value in PMCONF file
#config_pm.sh -S <ARGS from SMIT>   --> To Set the shift timings in PMCONF File

#config_pm.sh -r      --> Retrieve RETAIN value for SMIT display. 
#config_pm.sh -s      --> Retrieve Shift timings for SMIT display.
#config_pm.sh status  --> Reports the status of the PM Service. It returns either "Enabled" or "Disabled"

LANG=C

## Check for RBAC authorization
if [[ -f /usr/ios/cli/ioscli ]]
then
	# If its vios, check for vios authorization
	/usr/bin/ckauth vios.system.perfmgr.config
	rc=$?
	if [[ $rc -ne 0 ]]; then
		exit 1
	fi
else
	# If its aix, check for aix authorization
	/usr/bin/ckauth aix.system.perfmgr.config
	rc=$?
	if [[ $rc -ne 0 ]]; then
		exit 1
	fi
fi


Usage()
{
	echo "Usage : Use smitty topas for PM Recording Setup."
}

EnableDisablePM()
{

	while getopts ':T:' flag
	do
		case "$flag" in
		T)	Enable_Pm_Transmission=$OPTARG
		;;
		[?])	Usage
			exit 10
		;;

		esac
	done
	PMCONF=/var/perf/pm/config/pmconf

	if [[ ${Enable_Pm_Transmission} = "yes" ]]
	then
		#Check for persistent local binary recording
		/usr/bin/listtrec -l local_bin -P >/dev/null 2>&1
		rc=$?
		if [[ ${rc} -ne 0 ]]
		then
			echo "Error: Persistent local binary recording is not running."
			echo "Error: Please start the persistent local binary recording using SMIT."
			exit 1
		fi

		#Persistent recording file should contain only one day of recording
		PID=`/usr/bin/listtrec -l local_bin -P | /usr/bin/tail -1|/usr/bin/awk '{print $1}'`
		DaysPerFile=`/usr/bin/ps -eaf|/usr/bin/grep ${PID}|/usr/bin/grep -v grep |/usr/bin/cut -d "R" -f 2 | /usr/bin/awk '{print $1}'`
		if [[ ${DaysPerFile} -ne 1 ]]
		then
			echo "Error: Not able to enable PM Data transmission."
			echo "Note: Please start the persistent binary recording with 1 day of recorded data per file."
			echo "Note: Set 'Number of Days to store per file' to 1 while starting recording using SMIT."
			exit 1
		fi
		#Check if the user has entered Customer information using SMIT
		# Check for email id
		email=`/usr/bin/grep email_PM ${PMCONF} | /usr/bin/cut -d ":" -f 2 2>/dev/null`
		if [[ -z ${email} ]]
		then
			echo "Error: Customer information has not been entered."
			echo "Please enter customer information using SMIT.\n"
			echo "====================================
Steps to enter customer information
====================================
1. From the command line, type 'smit topas'.
2. Select 'Setup Performance Management'.
3. Select 'Change/Show Customer Information' to show/change the customer information.\n"
			exit 1
		fi

		#check for "# Enabled" string in pmconf file to check
		#if pm transmission is enabled
		/usr/bin/grep "# Enabled" ${PMCONF} > /dev/null 2>&1
		rc=$?
		if [ $rc -eq 0 ]
		then
			echo "PM Data Transmission is already enabled.\n\n"
			exit 20
		else
			#add # Enabled to the end of pmconf file
			# if pm transmission is enabled .
			/usr/bin/cp ${PMCONF} /tmp/_pmconf
			if [ $? -eq 0 ]
			then 
		  	   echo "# Enabled" >> /tmp/_pmconf 2>/dev/null
			   if [ $? -eq 0 ]
			   then
				/usr/bin/cp /tmp/_pmconf ${PMCONF}
				/usr/bin/crontab -l | /usr/bin/grep "pmcfg" >/dev/null 2>&1
				rc=$?
				
				if [[ $rc -eq 0 && ! -L /var/adm/perfmgr ]]
				then
					/usr/bin/ln -sf /var/perf/pm/ /var/adm/perfmgr
					echo "PM Data Transmission enabled.\n"
				else
					/usr/bin/crontab -l | /usr/bin/grep -v "pmcfg" > /tmp/_cron_tmp
					echo '55 23 * * * /var/perf/pm/bin/pmcfg  >/dev/null 2>&1     #Enable PM Data Collection' >>/tmp/_cron_tmp
					/usr/bin/crontab /tmp/_cron_tmp
					if [[ $? -ne 0 ]]
					then
						echo "Failed to update crontab "
						/usr/bin/rm -f /tmp/_cron_tmp
						exit 1
					fi
					
					if [[ ! -L /var/adm/perfmgr ]]
					then
						/usr/bin/ln -sf /var/perf/pm/ /var/adm/perfmgr
					fi
					
					# Clean the file
					/usr/bin/rm -f 	/tmp/_cron_tmp
					
					echo "PM Data Transmission enabled.\n"
				fi
				# Logic to handle extended PM Capabilities
				/usr/bin/grep "# PMExt" ${PMCONF} > /dev/null 2>&1
				if [ $? -eq 0 ]
				then
					Xlpar_Interval=`/usr/bin/grep Xlpar_Interval ${PMCONF} | /usr/bin/cut -d ':' -f 2-10 | /usr/bin/sed 's/:/#!:/g'`
					if [[ -f /var/perf/pm/bin/config_pm_ext.sh ]]
					then
						/var/perf/pm/bin/config_pm_ext.sh -C ${Xlpar_Interval}
					fi
				fi
				
				# Enable pmperfrec recording
				if [[ -f /var/perf/pm/bin/config_pm_ext.sh ]]
				then
					/var/perf/pm/bin/config_pm_ext.sh -P 'yes'
				fi
				
				/usr/bin/rm -f /tmp/_pmconf
			fi
		    fi	
		fi
		
		echo "Note: By enabling PM Service, you agree that IBM may use and 
share the data collected by PM for Power Systems within the IBM enterprise 
without limitation, including for purposes of problem determination, of 
assisting you with performance and capacity planning, of maintaining your 
existing and new business relationships with IBM, of notifying you of 
existing or projected resource constraints, and to assist us to enhance 
IBM products. You also agree that your data may be transferred to such 
entities in any country whether or not a member of the European Union."

	elif [[ ${Enable_Pm_Transmission} = "no" ]]
	then	
		
		#check for "# Enabled" string in pmconf file to check
		#if pm transmission is enabled. Remove this entry if exists
		/usr/bin/grep "# Enabled" ${PMCONF} > /dev/null 2>&1
		rc=$?

		if [[ $rc -eq 0 ]]
		then	
			/usr/bin/grep -v "# Enabled" ${PMCONF} > /tmp/_pmconf
			rc1=$?
			/usr/bin/cp /tmp/_pmconf ${PMCONF}
			rc2=$?
			if [[ $rc1 -eq 0 && $rc2 -eq 0 ]]
			then
				echo "Successfully disabled PM data transmission"
				/usr/bin/rm -f /tmp/_pmconf
			else
				echo "Error: Not able to disable PM data transmission".
			fi
			
			/usr/bin/rm -f /var/adm/perfmgr	> /dev/null 2>&1
			rc1=$?
			if [[ $rc1 -eq 0 ]]
			then
				echo "Successfully removed symbolic link /var/adm/perfmgr"
			else
				echo "Error: Not able to remove symbolic link /var/adm/perfmgr"
			fi
			
			# Remove all the entries related to pmcfg form crontab
			/usr/bin/crontab -l | /usr/bin/grep "pmcfg" >/dev/null 2>&1
			if [[ $? -eq 0 ]]
			then
				/usr/bin/crontab -l | /usr/bin/grep -v "pmcfg" > /tmp/_cron_tmp
				/usr/bin/crontab /tmp/_cron_tmp
				if [[ $? -ne 0 ]]
				then
					echo "Failed to update crontab "
					/usr/bin/rm -f /tmp/_cron_tmp
					exit 1
				fi
				
				echo "Successfully removed pmcfg entry from crontab\n"
				
				# Clean the file
				/usr/bin/rm -f 	/tmp/_cron_tmp
			fi
			
			# Disable pmperfrec recording
			if [[ -f /var/perf/pm/bin/config_pm_ext.sh ]]
			then
				/var/perf/pm/bin/config_pm_ext.sh -P 'no'
			fi
			
			# Clean up the PM files 
			HOSTNAME=`/usr/bin/hostname -s`
			DATADIR=/var/perf/pm/daily/${HOSTNAME}
			PM_DELETE_STATUS=0
			for pmfile in pm_stats pm_process pm_amsstat pm_crosslparstat pm_shrprocstat pm_meminfo
			do
				/usr/bin/ls ${DATADIR}/${pmfile}.* 2>/dev/null >&2
				if [[ $? -eq 0 ]]
				then
					/usr/bin/rm ${DATADIR}/${pmfile}.*
					if [[ $? -ne 0 ]]
					then
						PM_DELETE_STATUS=1
					fi
				fi
			done
			if [[ ${PM_DELETE_STATUS} -eq 0 ]]
			then
				echo "Successfully deleted all PM data file from ${DATADIR}/"
			else
				echo "Unable to delete all or some of the PM data files from ${DATADIR}/"
			fi
	
			
		else	
			echo "PM Data Transmission is already disabled"
			exit 20
		fi
	else
		Usage
		exit 30
	fi
}
	

SetRetentionValue()
{
	retain_value=$*

	#Check if the reatin value is "-" or "+". It can happen if a user types '-' or '+' in SMIT
	#without an number
	if [[ $retain_value = "-" || $retain_value = "+" ]]
	then
		echo "Error: Invalid retention period."
		echo "Note: Minimum 7 days of retention period is recommended."
		exit 1
	fi
	
	#Typecast the retain value to integer. 
	#This is used to remove '+', if a user enters values  like '+21'
	typeset -i retain_value=$retain_value

	# Check for values less than or equal to 0
	if [[ $retain_value -lt 1 ]]
	then
		echo "Error: Invalid retention period."
		echo "Note: Minimum 7 days of retention period is recommended."
		exit 1
	fi	

	PMCONF=/var/perf/pm/config/pmconf
	if /usr/bin/grep RETAIN ${PMCONF} >/dev/null 2>&1
	then
		/usr/bin/sed "s/RETAIN:.*/RETAIN:${retain_value}/" ${PMCONF} > /tmp/_pmconf
		/usr/bin/cp /tmp/_pmconf ${PMCONF}
	else
		echo "RETAIN:${retain_value}" >> ${PMCONF}
	fi
	if [[ ${retain_value} -lt 7 ]]
	then
		echo "Warning: Minimum 7 days of retention period is recommended."
	fi
}

GetRetainValue()
{
	PMCONF=/var/perf/pm/config/pmconf
	RETAIN=`/usr/bin/grep RETAIN ${PMCONF} | /usr/bin/cut -d : -f2`
	echo "#RETAIN:"
	echo "${RETAIN}:"
}


SetShiftTimings()
{
	PMCONF=/var/perf/pm/config/pmconf
	TrendingDays=""
	while getopts 'm:t:w:T:f:s:S:z:Z:x:X:' option
	do
		case $option in
			m|t|w|T|f|s|S)
			Value=`echo ${OPTARG}| /usr/bin/cut -c 1` 
			TrendingDays="${TrendingDays}${Value}" ;;

			z)FirstShiftStart=${OPTARG} ;;
			Z)FirstShiftEnd=${OPTARG} ;;
			x)SecShiftStart=${OPTARG} ;;
			X)SecShiftEnd=${OPTARG} ;;
		esac
	done
	/usr/bin/sed "s/^TrendingDays=.*/TrendingDays=${TrendingDays}/" ${PMCONF} > /tmp/_pmconf
	/usr/bin/cp /tmp/_pmconf ${PMCONF}

	/usr/bin/sed "s/First_Shift_Start=.*/First_Shift_Start=${FirstShiftStart}/" ${PMCONF} > /tmp/_pmconf
	/usr/bin/cp /tmp/_pmconf ${PMCONF}
	/usr/bin/sed "s/First_Shift_End=.*/First_Shift_End=${FirstShiftEnd}/" ${PMCONF} > /tmp/_pmconf
	/usr/bin/cp /tmp/_pmconf ${PMCONF}
	/usr/bin/sed "s/Sec_Shift_Start=.*/Sec_Shift_Start=${SecShiftStart}/" ${PMCONF} > /tmp/_pmconf
	/usr/bin/cp /tmp/_pmconf ${PMCONF}
	/usr/bin/sed "s/Sec_Shift_End=.*/Sec_Shift_End=${SecShiftEnd}/" ${PMCONF} > /tmp/_pmconf
	/usr/bin/cp /tmp/_pmconf ${PMCONF}
}


GetShiftTimings()
{
	PMCONF=/var/perf/pm/config/pmconf
	if [[ -f ${PMCONF} ]]
	then
		first_shift_start=`/usr/bin/grep First_Shift_Start $PMCONF| /usr/bin/cut -d "=" -f2 | /usr/bin/sed 's/:/#!:/g'`
		first_shift_end=`/usr/bin/grep First_Shift_End $PMCONF| /usr/bin/cut -d "=" -f2 | /usr/bin/sed 's/:/#!:/g'`
		sec_shift_start=`/usr/bin/grep Sec_Shift_Start $PMCONF| /usr/bin/cut -d "=" -f2 | /usr/bin/sed 's/:/#!:/g'`
		sec_shift_end=`/usr/bin/grep Sec_Shift_End $PMCONF| /usr/bin/cut -d "=" -f2 | /usr/bin/sed 's/:/#!:/g'`

		# Collect the Values of Trending Days from ${PMCONF} file
	
		Sunday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 1`
		if [[ ${Sunday} = "Y" ]]
		then
			Sunday="Yes,No"
		else
			Sunday="No,Yes"
		fi

		Monday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 2`
		if [[ ${Monday} = "Y" ]]
		then
			Monday="Yes,No"
		else
			Monday="No,Yes"
		fi

		Tuesday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 3`
		if [[ ${Tuesday} = "Y" ]]
		then
			Tuesday="Yes,No"
		else
			Tuesday="No,Yes"
		fi

		Wednesday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 4`
		if [[ ${Wednesday} = "Y" ]]
		then
			Wednesday="Yes,No"
		else
			Wednesday="No,Yes"
		fi

		Thursday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 5`
		if [[ ${Thursday} = "Y" ]]
		then
			Thursday="Yes,No"
		else
			Thursday="No,Yes"
		fi

		Friday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 6`
		if [[ ${Friday} = "Y" ]]
		then
			Friday="Yes,No"
		else
			Friday="No,Yes"
		fi

		Saturday=`/usr/bin/grep TrendingDays ${PMCONF} | /usr/bin/cut -d = -f 2 | /usr/bin/cut -c 7`
		if [[ ${Saturday} = "Y" ]]
		then
			Saturday="Yes,No"
		else
			Saturday="No,Yes"
		fi
	fi


	echo "#FirstShiftStart:FirstShiftEnd:SecShiftStart:SecShiftEnd:Sunday:Monday:Tuesday:Wednesday:Thursday:Friday:Saturday:"
	echo "${first_shift_start}:${first_shift_end}:${sec_shift_start}:${sec_shift_end}:${Sunday}:${Monday}:${Tuesday}:${Wednesday}:${Thursday}:${Friday}:${Saturday}:"

}

# This function returns the current status of the 
# PM data transmission.
# It returns either "Enabled" or "Disabled". 
isPMEnabled()
{
	PMCONF=/var/perf/pm/config/pmconf
	#check for "# Enabled" string in pmconf file to check
	#if pm transmission is enabled
	/usr/bin/grep "# Enabled" ${PMCONF} > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
		echo "Enabled"
	else
		echo "Disabled"
	fi
}




main()
{

#IT should not run inside WPAR
Is_WPAR=`/usr/bin/uname -W`
if [[ ${Is_WPAR} -ne 0 ]]
then
	echo "PM Service is not supported inside WPAR"
	exit 1
fi

ARG1=$1

if [[ ${ARG1} = "-T" ]]
then
	EnableDisablePM $*
elif [[ ${ARG1} = "-R" ]]
then
	shift
	SetRetentionValue $*
elif [[ ${ARG1} = "-S" ]]
then
	shift
	SetShiftTimings $*
elif [[ ${ARG1} = "-r" ]]
then
	shift
	GetRetainValue
elif [[ ${ARG1} = "-s" ]]
then
	shift
	GetShiftTimings
elif [[ ${ARG1} = "status" ]]    # Reports the status of the PM. It returns either "Enabled" or "Disabled"
then
	shift
	isPMEnabled
fi
}
main $*

# End of the script config_pm.sh

