#! /bin/ksh
#
# Copyright (c) 2004 Silicon Graphics, Inc  All Rights Reserved.
# Copyright (c) 2007 Cray, Inc  All Rights Reserved.
# This script is derived from SGI open source script csackpacct
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
# Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
# Mountain View, CA  94043, or:
#
# http://www.sgi.com
#
# Contact information:  Cray Inc., 1340 Mendota Heights Road,
# Mendota Heights, MN 55120
#
# http://www.cray.com
#

. '/usr/sbin/csanodefuncs'
#
# Set some variables retrieved from csa.conf
#
# Filesystem containing /var/csa:
ACCT_FS=`/usr/sbin/csagetconfig ACCT_FS`
UMASK=$(/usr/sbin/csagetconfig UMASK)		# umask value
if [ "${UMASK}" ]
then
	umask ${UMASK}
else
	umask 0017
	echo -e "`date` - csanodeacct: UMASK symbol is missing from the"
	echo -e "csa.conf file, using (0017) as the value."
fi
#
# Some variables this script uses
#
# Define the lock files
LOCKFILE=/tmp/csanodeacct.lock		# local file system lock file


# Check for / create lock file.  This should be done before the 
# trap so a signal doesn't make us remove the lock file when we don't
# want to.
if [ -r $LOCKFILE ]
then
	# lock file exists, exit 1
	echo "$0 ERROR: ${LOCKFILE} found"
	exit 1
else
	# use cat /dev/null command to create lock file.
	# touch command does not exist on compute nodes
	cat /dev/null > $LOCKFILE
	chmod 400 $LOCKFILE
fi

#
# Run cleanup when we get signals:
# 1,2,3,9,15
#
trap cleanup 1 2 3 9 15


#
# Main...
#
# Switch the local accounting file.
# NOTE:	csaswitch moves the accounting file to the next available file
#	eg. /var/csa/day/pacct*
#	/var/csa/day/pacct remains the current file.
#	TODO: Need to pass name of accouting file to csanodesum which will
#		read it and form a summary record.

PACCT_FILE=$(/usr/sbin/csagetconfig PACCT_FILE)
# Exit if pacct file does not exist or is empty
[[ ! -s $PACCT_FILE ]] && cleanup
/usr/sbin/csaswitch -c switch

#
# Set the correct pathname for the rca-helper utility.
# On service nodes, the path is: RCA_PATH=/opt/xt-boot/default/bin/snos64
# On compute nodes, the path is: RCA_ALT_PATH=/sbin
# On BW compute nodes, there is no rca-helper.  The node physical
# location is obtained by reading /proc/sys/kernel/physloc
#
if [ -x $RCA_PATH/rca-helper ]; then
	RCA_HELPER=$RCA_PATH/rca-helper
	NODE_TYPE=$XT
elif [ -x $RCA_ALT_PATH/rca-helper ]; then
	RCA_HELPER=$RCA_ALT_PATH/rca-helper
	NODE_TYPE=$XT
elif [ -r /proc/sys/kernel/physloc ]; then
	NODE_TYPE=$X2
else
	echo "$0 ERROR: rca-helper not available."
	echo "$0 ERROR: /proc/sys/kernel/physloc not available."
	exit 1
fi

#
# Get count of System I/O Nodes that have mounted account file systems
#
SIO_NODES=$(/usr/sbin/csagetconfig ACCT_SIO_NODES)
if (( $? != 0 )) || (( $SIO_NODES == 0 ))
then
	echo "$0 ERROR: No SIO nodes are available for account files"
	exit 1
fi
if (( $SIO_NODES > $SIO_NODES_MAX ))
then
	echo "$0 ERROR: SIO nodes configured: $SIO_NODES"
	echo "    exceeds maximum SIO nodes allowed: $SIO_NODES_MAX"
	exit 1
fi
#
#	Get SIO account file system mount points from config file
#	For each mount point, get the range of compute node nids
#	that are supported by that mount point.
#	The configuration file entries look like the following:
#
#	First the number of SIO nodes has an entry like the following
#	ACCT_SIO_NODES  2
#
#	Next, there must be one entry for each SIO_NODE count specified.
#	Each account file system has an entry of the following form.
#	The file system shown is a mount point.	 In order to avoid
#	changing the configuration file parser code which could have
#	unanticipated issues, each file account file system mount
#	point must have a "_" character in place of each "/" character.
#	Hence, a file system mount point name is not allowed to contain a
#	"_" character.
#
#	ACCT_FILE_SYSTEM_00	_lus_nid00007
#	ACCT_FILE_SYSTEM_01	_lus_nid00128
#
#	Each mount point has an entry that shows the range of node names
#	that have their accounting files written to each of the respective
#	account file system mount points. Since a Cray XT computer system
#	may have more than one type of compute node, a separate path naming
#	convention is defined for each type of compute node.  Following
#	the file system mount point, the next component of the path name is
#	the character string "csa".  This is followed by a path name component
#	that defines the architecture type of the node.
#	XT - Cray XT node type
#	X2 - Cray X2 node type
#	The beginning and end node names for each range are separated by
#	"--" as shown in the following examples:
#
#	For XT nodes
#	_lus_nid00007_csa_XT	c0-0c0s0n0--c0-0c2s7n3
#	_lus_nid00128_csa_XT	c1-0c0s0n0--c1-0c2s7n3
#
#	For X2 Nodes
#	_lus_nid00007_csa_X2	r10-0c0s0n0--r11-1c1s7n1
#
(( i = 0 ))
while (( i < $SIO_NODES ))
do
	sio_node=$(/usr/sbin/csagetconfig $ACCT_FS_MOUNT$i)
	if [ $NODE_TYPE == $XT ]; then
		SIO_NODE[i]=$(cfpath2path ${sio_node}_${CSA}_${XT})
		cname_range=$(/usr/sbin/csagetconfig ${sio_node}_${CSA}_${XT})
		#
		# cname_range in form of cCC-RRcCCsSSnNN--cCC-RRcCCsSSnNN
		#
		cname_range=${cname_range##*"	"}
		#
		# set minimum cname component values from cname_range
		#
		cname_min=${cname_range%--*}
		cname2comps $cname_min
		CAB_MIN[i]=$CAB
		ROW_MIN[i]=$ROW
		CAGE_MIN[i]=$CAGE
		XT_SLOT_MIN[i]=$XT_SLOT
		MCOMP_MIN[i]=$MCOMP
		#
		# set maximum cname component values from cname_range
		#
		cname_max=${cname_range#*--}
		cname2comps $cname_max
		CAB_MAX[i]=$CAB
		ROW_MAX[i]=$ROW
		CAGE_MAX[i]=$CAGE
		XT_SLOT_MAX[i]=$XT_SLOT
		MCOMP_MAX[i]=$MCOMP
	elif [ $NODE_TYPE == $X2 ]; then
		SIO_NODE[i]=$(cfpath2path ${sio_node}_${CSA}_${X2})
		rname_range=$(/usr/sbin/csagetconfig ${sio_node}_${CSA}_${X2})
		#
		# rname_range in form of r11XX-YYcCCsSSnNN--r1XX-YYcCCsSSnNN
		#
		rname_range=${rname_range##*"	"}
		#
		# set minimum rname component values from rname_range
		#
		rname_min=${rname_range%--*}
		rname2comps $rname_min
		X_MIN[i]=$X
		Y_MIN[i]=$Y
		CHASS_MIN[i]=$CHASSIS
		X2_SLOT_MIN[i]=$X2_SLOT
		NODE_MIN[i]=$NODE
		#
		# set maximum rname component values from rname_range
		#
		rname_max=${rname_range#*--}
		rname2comps $rname_max
		X_MAX[i]=$X
		Y_MAX[i]=$Y
		CHASS_MAX[i]=$CHASSIS
		X2_SLOT_MAX[i]=$X2_SLOT
		NODE_MAX[i]=$NODE
	else
		echo "$0 ERROR: Illegal node type: $NODE_TYPE"
		exit 1
	fi
	
	(( i = i + 1 ))
done
#
# Get the local node cname and generate a complete cname path on the
# common file system where we can write our accounting file.
#
[[ $NODE_TYPE == ${XT} ]] && LOCALNODE=$($RCA_HELPER -I)
[[ $NODE_TYPE == ${X2} ]] && LOCALNODE=$(cat /proc/sys/kernel/physloc)
(( i = 0 ))
while (( i < $SIO_NODES ))
do
	if [ $NODE_TYPE == ${XT} ]; then
		cname_in_range ${LOCALNODE} i
		if [ $RANGE_OK -eq 0 ]; then
			SIO_PATH=${SIO_NODE[i]}
			break
		fi
	elif [ $NODE_TYPE == ${X2} ]; then
		rname_in_range ${LOCALNODE} i
		if [ $RANGE_OK -eq 0 ]; then
			SIO_PATH=${SIO_NODE[i]}
			break
		fi
	else
		echo "$0 ERROR: Illegal node type: $LOCALNODE"
		cleanup
	fi
	(( i = i + 1 ))
done
if [ $RANGE_OK -ne 0 ]; then
	echo "$0 ERROR: No Account File System Mount point configured for"
	echo "   node: $LOCALNODE"
	cleanup
fi
#
# Use configuration file information to determine account file system
# mount point where we will write the accounting files for this node.
#
# Compute cname path subdirectories on account file system, based on
# local cname.
#
[[ $NODE_TYPE == ${XT} ]] && NODEPATH=$(cname2path $LOCALNODE)
[[ $NODE_TYPE == ${X2} ]] && NODEPATH=$(rname2path $LOCALNODE)
if [ ! -d $SIO_PATH/$NODEPATH ]
then
	echo "$0 INFO: Creating $SIO_PATH/$NODEPATH"
	mkdir -p  $SIO_PATH/$NODEPATH
	if [ $? -ne 0 ]
	then
		echo "$0 ERROR: mkdir $SIO_PATH/$NODEPATH failed"
		exit 1
	fi
fi
NODE_PACCT_SUM=$SIO_PATH/$NODEPATH/pacct
NODE_LOCK=$SIO_PATH/$NODEPATH/$CFSLOCKFILE
if [ -r $NODE_LOCK ]
then
	# node lock file exists, clear lock and exit.
	rm -f $LOCKFILE
	exit 1
else
	# use cat /dev/null command to create cname lock file.
	# touch command does not exist on compute nodes
	cat /dev/null > $NODE_LOCK
	chmod 400 $NODE_LOCK
fi
#
# Get Process Accounting Configuration information
#
PROC_ACCT=$(/usr/sbin/csagetconfig $DO_PROCESS_ACCT)
[[ $? -eq 0 && $PROC_ACCT = "ON" ]] && PROCESS="-P"
#
# Process switched pacct files.
# Switched pacct file is pacct file with one digit appended.
# Use a while loop to process the pacct files, in case csaswitch(8)
# has been invoked more than once.  This would only happen if for
# some reason, this script exited after a call to csaswitch(8) and
# this before getting to this point.
# Remove the pacct input file after processing it.
#
(( j = 1 ))
while [[ -r $PACCT_FILE$j ]]
do
	/usr/sbin/csanodesum -p $PACCT_FILE$j -s $NODE_PACCT_SUM $PROCESS
	rm $PACCT_FILE$j
	(( j = j + 1 ))
done
rm -f $NODE_LOCK
rm -f $LOCKFILE
exit 0
