#!/bin/ksh93
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# perf720 src/perf/pmaix/usr/bin/pmcfg/pmcfg_ext 1.17 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2012,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 
#########################################################################
# Purpose:                                                              #
#       pmcfg_ext is configuration script for Performance Management    #
#       called from pmcfg to collect additional data                    #
# Functions:                                                            #
#       Configure_Environment                                           #
#       CollectSppData                                                  #
#       CollectCrossLparData                                            # 
#		CollectAmsData                                                  #
#       Parse_Inputs                                                    #
#########################################################################
LANG=C

Usage()
{
echo "Usage: pmcfg_ext [ -A | -C ] "
#echo "-S Prints shared processor pool info"
echo "-A Prints Active memory sharing info"
echo "-C Provide Cross Lpar Info "
}

Configure_Environment()
{
	#  ------------------------------------------------------------
	#  Get the hostname from uname and append to the data directory 
	#  -----------------------------------------------------------

	HOSTNAME=`/usr/bin/hostname -s`
	#only DATADIR,TODAY, LOGFILE variables are exported as they
        # are required in pmcfg_ext script
	DATADIR=/var/perf/pm/daily/${HOSTNAME}
	PMCONF=/var/perf/pm/config/pmconf
	PMRecService=pmperfrec
	
	TODAY=`/usr/bin/date +%Y.%m.%d.%a`
	LOGFILE=/var/perf/pm/daily/pmcfg.log
	# Compress today's stats and process files
	PmcfgExtStartHr=$(date +%H)
	PmcfgExtStartMin=$(date +%M)
	
	TYP=`/usr/bin/lparstat -i | /usr/bin/grep -iw Type | /usr/bin/cut -d":" -f 2 | /usr/bin/cut -d"-" -f 1`
	TYP=`echo $TYP | /usr/bin/sed 's/ //g'`
	if [[ $TYP == "Dedicated" ]]
	then
		EXT_SECTION="E1"
	else
		EXT_SECTION="shrpool E1"
	fi
	
	hw_sup=`/usr/sbin/getsystype -i | /usr/bin/awk '{print $2}' 2>/dev/null`
	
	memtyp=`lparstat -i | grep -iw "Memory Mode" | /usr/bin/cut -d":" -f 2 | /usr/bin/cut -d"-" -f 1`
	memtyp=`echo $memtyp | /usr/bin/sed 's/ //g'`
	if [[ $memtyp != "Dedicated" ]] then
		EXT_SECTION="$EXT_SECTION shrmem"
	fi
	
	/usr/bin/lssrc -s ${PMRecService} >/dev/null 2>&1
	if [[ $? -eq 0 ]]
	then
		EXT_SECTION="$EXT_SECTION meminfo"
	fi
	
}

CollectSppData()
{
    #set -x
    
	
	if [[ ${hw_sup} -le 6 ]]
	then
		return
	fi
	
	SppstatsFile="${DATADIR}/pm_shrprocstat.${TODAY}"
	SPP_SCRIPT_CMD="/var/perf/pm/bin/shrpoolinfo"
	
	${SPP_SCRIPT_CMD} ${SppstatsFile} 2>&4
	
}
CollectAmsData()
{
    #set -x


    if [[ ${hw_sup} -le 6 ]]
    then
        return
    fi

    AmsstatsFile="${DATADIR}/pm_amsstat.${TODAY}"
    AMS_SCRIPT_CMD="/var/perf/pm/bin/amsinfo"

    ${AMS_SCRIPT_CMD} ${AmsstatsFile} 2>&4

}

GetYesterday()
{
	year=`echo ${TODAY} | /usr/bin/cut -c 1-4`
	mon=`echo ${TODAY} | /usr/bin/cut -c 6,7`
	day=`echo ${TODAY} | /usr/bin/cut -c 9,10`
	wday=`echo ${TODAY} | /usr/bin/cut -c 12-14`
	
	myyear=${year}
	mymon=${mon}
	myday=$((${day}-1))
	
	if [[ ${myday} -eq 0 ]] then
			mymon=$((${mon}-1))
			
			if [[ ${mymon} -eq 0 ]] then
					myyear=$((${year}-1))
					mymon=12
					myday=31
			else
					case ${mymon} in
							1|3|5|7|8|10|12)	myday=31;;
							4|6|9|11)			myday=30;;
							2)
									modyear=$((${year}%4))
									if [[ ${modyear} -eq 0 ]] then
											myday=29
									else
											myday=28
									fi
							;;
					esac
			fi
	fi
	case $wday in
			"Mon")	mywday="Sun";;
			"Tue")	mywday="Mon";;
			"Wed")	mywday="Tue";;
			"Thu")	mywday="Wed";;
			"Fri")	mywday="Thu";;
			"Sat")	mywday="Fri";;
			"Sun")	mywday="Sat";;
	
	esac
	if [[ $myday -lt 10 ]] then
			myday="0${myday}"
	fi
	if [[ $mymon -lt 10 ]] then
			mymon=$((${mymon}/1))
			mymon="0${mymon}"
	fi
	YESTERDAY="${myyear}.${mymon}.${myday}.${mywday}"
	YESTERDAY_YYYYMMDD="${myyear}${mymon}${myday}"
	
}

# To filenames passed as arguments located in PM output folder will be compressed
CompressFiles()
{
	FileList=$*
	if [[ $FileList == "" ]]
	then
		return
	fi
	
	# Loop throught each file name
	for pmfile in $FileList
	do
		# If filename is not compressed already
		if [[ ${pmfile##*.} != "Z" ]]
		then
			# Force compress to avoid any prompt
			/usr/bin/compress -f ${DATADIR}/${pmfile} >&4 2>&4
			if [[ $? -ne 0 ]]
			then
				echo "Failed to compress ${DATADIR}/${pmfile} file" >&4 2>&4
			fi
		fi
	done
	
}

# To filenames passed as arguments located in PM output folder will be uncompressed 
UnCompressFiles()
{
	FileList=$*
	if [[ $FileList == "" ]]
	then
		return
	fi
	
	# Loop throught each file name
	for pmfile in $FileList
	do
		# if Z is at end of filename and uncompressed file does not exist
		if [[ ${pmfile##*.} == "Z" && ! -e ${pmfile%.Z} ]]
		then
			/usr/bin/uncompress ${DATADIR}/${pmfile} >&4 2>&4
			if [[ $? -ne 0 ]]
			then
				echo "Failed to uncompress ${DATADIR}/${pmfile} file" >&4 2>&4
			fi
		else
			echo "Either file ${pmfile} is not compressed or uncompressed file ${pmfile%.Z} already exist!" >&4 2>&4
		fi
	done
}

CollectCrossLparData()
{
	# Appending errors to log file
	
	#set -x
	
	if [[ ${hw_sup} -lt 6 || ${hw_sup} -gt 7 ]]
	then
		if [[ ${hw_sup} -gt 7 ]]
		then
			XlparInt=`/usr/bin/grep Xlpar_Interval ${PMCONF} | /usr/bin/awk -F ':' '{print $2}'`
			/var/perf/pm/bin/config_pm_ext.sh -C "${XlparInt}"
		fi
		return
	fi
	
	ClparTmpFile="${DATADIR}/pm_crosslparstat.tmp"
	CLPAR_SCRIPT_CMD="/var/perf/pm/bin/crosslpar"
	
	${CLPAR_SCRIPT_CMD} ${ClparTmpFile} 2>&4
	
	if [[ -f ${ClparTmpFile} ]] then
		Xlpar_Hour=`date +%H`
		Xlpar_Min=`date +%M`
		
		if [[ (${Xlpar_Hour} -eq 0) && (${Xlpar_Min} -lt 14) ]] then
			GetYesterday
			CLPARstatsFile="${DATADIR}/pm_crosslparstat.${YESTERDAY}"
			statsfile="${DATADIR:?}/pm_stats.${YESTERDAY}"
			statssend="${DATADIR:?}/pm_stats.send"
			
			if [[ -f ${CLPARstatsFile} ]] then
				`/usr/bin/egrep -v "date and time|Clock Offset|^$" ${CLPARstatsFile} >> ${ClparTmpFile}` 2>&4
				#`/usr/bin/cat ${CLPARstatsFile} >> ${ClparTmpFile}` 2>&4
			fi
			/usr/bin/mv ${ClparTmpFile} ${CLPARstatsFile} 2>&4
			
			# Wait for pmcfg to complete to avoid corrupting pm_stats.date file
			if [[ ! -f $statssend ]]
		 	then	
				/usr/bin/sleep 60
				if [[ ! -f $statssend ]]
                then
               	    echo " Error Generating $statsfile " 2>&4
					return
                 fi
			fi
			if [[ -f $statsfile ]] then
				echo "<E1>" >> ${statsfile} 2>&4
				echo "#<datacols> </datacols>" >> ${statsfile} 2>&4
				/usr/bin/cat $CLPARstatsFile >> ${statsfile} 2>&4
				echo "</E1>" >> ${statsfile} 2>&4
			fi

			# Call pmcfg to regenerate pm_stats.send file
			/var/perf/pm/bin/pmcfg -D ${YESTERDAY_YYYYMMDD} -n
		else
			CLPARstatsFile="${DATADIR}/pm_crosslparstat.${TODAY}"
			if [[ -f ${CLPARstatsFile} ]] then
				`/usr/bin/egrep -v "date and time|Clock Offset|^$" ${CLPARstatsFile} >> ${ClparTmpFile}` 2>&4
				#`/usr/bin/cat ${CLPARstatsFile} >> ${ClparTmpFile}` 2>&4
			fi
			/usr/bin/mv ${ClparTmpFile} ${CLPARstatsFile} 2>&4
		fi
	fi
		
}


# Process the pmcfg required flags 
ProcessPmcfgFlag()
{
	# Get the first argument in action variable
	action=$1
	
	# Remove the first argument
	shift
	
	while getopts ':D:' flag
	do
		case ${flag} in
			D)
				# -D option for pmcfg, re-transmit required stats file
				if [[ ${action} == "-pre" ]]
				then
					# uncompress the required stats files before re-transmitting
					Dvalue=${OPTARG}
					if [[ ${Dvalue} -eq 0 ]]
					then
						# Get all compressed stats file names
						Files_To_UnCompress=$(/usr/bin/ls ${DATADIR} | /usr/bin/grep "^pm_stats.[[:digit:]]\{4\}.[[:digit:]]\{2\}.[[:digit:]]\{2\}.[[:alpha:]]\{3\}.Z$" 2>&4)
						
					else
						#Convert the YYYYMMDD to YYYY.MM.DD
						
						year=`echo ${Dvalue} | /usr/bin/cut -c 1-4`
						month=`echo ${Dvalue} | /usr/bin/cut -c 5,6`
						day=`echo ${Dvalue} | /usr/bin/cut -c 7,8`
						date=${year}.${month}.${day}
						
						# Uncompress the stats.date file if its found and compressed
						Files_To_UnCompress=$(/usr/bin/ls ${DATADIR} | /usr/bin/grep "^pm_stats.${date}.*.Z$" 2>&4)
						
					fi
					
					# Get all filenames in one line
					FileList=$(echo ${Files_To_UnCompress} | tr '\n' ' ')
					# If no compressed stats files found, dont call Uncompress Function
					if [[ ! -z ${FileList} ]]
					then
						UnCompressFiles "${FileList}" 2>&4
					fi
				elif [[ ${action} == "-post" ]]
				then
					# compress the stats files after re-transmitting
					Dvalue=${OPTARG}
					if [[ ${Dvalue} -eq 0 ]]
					then
						# Get all uncompressed stats file names
						Files_To_Compress=$(/usr/bin/ls ${DATADIR} | /usr/bin/grep "^pm_stats.[[:digit:]]\{4\}.[[:digit:]]\{2\}.[[:digit:]]\{2\}.[[:alpha:]]\{3\}$" | \
							/usr/bin/grep -v "${TODAY}$" 2>&4)
					else
						#Convert the YYYYMMDD to YYYY.MM.DD
						
						year=`echo ${Dvalue} | /usr/bin/cut -c 1-4`
						month=`echo ${Dvalue} | /usr/bin/cut -c 5,6`
						day=`echo ${Dvalue} | /usr/bin/cut -c 7,8`
						date=${year}.${month}.${day}
						
						# Compress the stats.date file if its found and uncompressed
						Files_To_Compress=$(/usr/bin/ls ${DATADIR} | /usr/bin/grep "^pm_stats.${date}.*" | /usr/bin/grep -v ".Z$" | /usr/bin/grep -v "${TODAY}$" 2>&4)
						
						# Now search for the pm_stats file
					fi
					
					# Get all filenames in one line
					FileList=$(echo ${Files_To_Compress} | tr '\n' ' ')
					# If no uncompressed stats files found, dont call Compress Function
					if [[ ! -z ${FileList} ]]
					then
						CompressFiles "${FileList}" 2>&4
					fi
					
				fi
				
				;;
		esac
	done
}


# Parse the input flags and set the appropriate flags
Parse_Inputs()
{
	while getopts 'Cgs' flag 2>/dev/null
	do
		case ${flag} in 
			
			#S)
				# Collect Shared Processor related statistics and exit
				# Call CollectSppData function
				#CollectSppData
				#exit 
				#;;
			
			#A)
				# Collect Shared Memory Pool related statistics and exit
				# Call CollectAmsData function
				#CollectAmsData
				#exit
				#;;
			
			C)
				# Collect Cross Lpar related statistics and exit
				# Call CollectCrossLparData function
				CollectCrossLparData
				exit
				;;
			
			g) 
				#Get all new section tags
				echo ${EXT_SECTION}
				exit
				;;

			s)
				# Collect Software info and exit
				# Call GetSoftwareInfo function
				GetSoftwareInfo
				exit 
				;;
			
			[?]) 

			  Usage
			;;
		esac
	done
}


GetSoftwareInfo()
{
	#set -x
	
	# Check if AIX 6.1 and if its not VIO, do not collect software info
	OSLEVEL=`/usr/bin/oslevel | awk -F'.' '{printf $1"."$2}'`
	if [[ ${OSLEVEL} == "6.1" && ! -f /usr/ios/cli/ioscli ]]
	then
		return
	fi
	
	statsfile="${DATADIR:?}/pm_stats.${TODAY}"
	if [[ ! -e ${statsfile} ]] 
	then
		# If the stats file is not generated, return to calling function
		echo "statsfile ${statsfile} not found to update software info" >&4
		return
	fi
	
	# Get today's day of the month (1 to 31)
	CurDayOfMon=`date +%d`
	
	# Collect software info only if day is first day of the month
	if [[ ${CurDayOfMon} -eq 1 ]] then
		echo "<swinfo>" >> ${statsfile} 2>&4
		echo "#<datacols>Fileset,Level,Description</datacols>" >> ${statsfile} 2>&4
		
		# Get list of licensed software, 
		# Replace blank space with #_# to use complete line in for loop
		for lin in `/usr/sbin/inulag -lc -n all | /usr/bin/awk -F':' '{print $1":"$4}' | /usr/bin/sed 's/ /#_#/g' | /usr/bin/grep -v ^Fileset`
		do
			levl=""
			# Replace #_# with blank space
			line=`echo $lin | /usr/bin/sed 's/#_#/ /g'`
			filset=`echo $line | /usr/bin/cut -d':' -f1`
			# If the fileset name is blank, go to next line
			if [[ -z $filset ]]
			then
				continue
			fi
			
			# Get fileset level
			levl=`/usr/bin/lslpp -lc $filset | /usr/bin/grep -v '#Fileset' | /usr/bin/head -1 | /usr/bin/awk -F':' '{print $3}'`
			fildes=`/usr/bin/lslpp -lc $filset | /usr/bin/grep -v '#Fileset' | /usr/bin/head -1 | /usr/bin/awk -F':' '{print $7}'`
			echo "$filset,$levl,$fildes" >> ${statsfile} 2>&4
		done
		
		echo  >> ${statsfile} 2>&4
		echo "RPM" >> ${statsfile} 2>&4
		
		# Get rpm package list
		for pkgs in `/usr/bin/rpm -qa`
		do
			# For each rpm package, get details like version and summary info
			val=`/usr/bin/rpm -qi $pkgs | /usr/bin/egrep 'Name|Version|Summary'`
			pkgnam=`echo "$val" | /usr/bin/grep Name | /usr/bin/awk '{print $3}'`
			pkgver=`echo "$val" | /usr/bin/grep Version | /usr/bin/awk '{print $3}'`
			pkgsum=`echo "$val" | /usr/bin/grep Summary | /usr/bin/awk -F':' '{print $2}'`
			echo "$pkgnam,$pkgver,$pkgsum" >> ${statsfile} 2>&4
		done
		echo "</swinfo>" >> ${statsfile} 2>&4
	fi
}

main()
{
	## 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
	
	Configure_Environment
	exec 4>>${LOGFILE}
	#collect shared processor pool and shared memory pool data if this program is called without arguments
	#else parse the inputs to determine the path to follow.
	if [ $# -eq 0 ]
	then 
		statsfile="${DATADIR:?}/pm_stats.${TODAY}"
		
		# Customer Demographics from ESA by calling function GetESACustInfo in config_pm_ext.sh
		/var/perf/pm/bin/config_pm_ext.sh -E 2>&4
		
		SppstatsFile="${DATADIR}/pm_shrprocstat.${TODAY}"
		if [[ -f $SppstatsFile ]]  
		then
			echo "<shrpool>" >> ${statsfile} 2>&4
			/usr/bin/cat $SppstatsFile >> ${statsfile} 2>&4
			echo "</shrpool>" >> ${statsfile} 2>&4
		fi
		
		MemInfoFile="${DATADIR}/pm_meminfo.${TODAY}"
		if [[ -f $MemInfoFile ]]  
		then
			 echo "<meminfo>" >> ${statsfile} 2>&4
			/usr/bin/cat $MemInfoFile >> ${statsfile} 2>&4
			 echo "<\meminfo>" >> ${statsfile} 2>&4
		fi
		
		AmsstatsFile="${DATADIR}/pm_amsstat.${TODAY}"
		if [[ -f $AmsstatsFile ]]
		then
			echo "<shrmem>" >> ${statsfile} 2>&4
			/usr/bin/cat $AmsstatsFile >> ${statsfile} 2>&4
			echo "</shrmem>" >> ${statsfile} 2>&4
		fi
		
		# If POWER 8, append Crosslpar output to PM stats file
		if [[ ${hw_sup} -gt 7 ]]; then
			CLPARstatsFile="${DATADIR}/pm_crosslparstat.${TODAY}"
			if [[ -f ${statsfile} && -f ${CLPARstatsFile} ]]; then
				echo "<E1>" >> ${statsfile} 2>&4
				echo "#<datacols> </datacols>" >> ${statsfile} 2>&4
				/usr/bin/cat $CLPARstatsFile >> ${statsfile} 2>&4
				echo "</E1>" >> ${statsfile} 2>&4
			fi
			
		fi
		
		# Get software information
		GetSoftwareInfo
		
		# Compress PM output files
		if [[ (${PmcfgExtStartHr} -eq 23) && (${PmcfgExtStartMin} -ge 55) ]] then
			Files_To_Compress=$(/usr/bin/ls ${DATADIR} | /usr/bin/egrep "^pm_stats|^pm_process|^pm_shrprocstat|^pm_amsstat|^pm_meminfo|^pm_crosslparstat" | \
					/usr/bin/egrep -v ".send$|${TODAY}$|.Z$" 2>&4)
			
			# Get all filenames in one line
			FileList=$(echo ${Files_To_Compress} | tr '\n' ' ')
			# If no compressed stats files found, dont call Compress Function
			if [[ ! -z ${FileList} ]]
			then
				CompressFiles ${FileList} 2>&4
			fi
		fi
		
		/usr/bin/grep "# PMExt:2" ${PMCONF} > /dev/null 2>&1
		if [ $? -ne 0 ]
		then
			return
		fi
		
		# Collect shared processor pool data
		# ----------------------------------
		#CollectSppData
		#CollectAmsData
	else
		# pre and post processing of pmcfg flag
		if [[ "$1" == "-pre" || "$1" == "-post" ]]
		then
			ProcessPmcfgFlag $*
		else
			Parse_Inputs $*
		fi
	fi
	
	return
}	
main $*

