#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/bin/cdat/types/psrasgrab/psrasgrab.sh 1.3 
#  
# 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 

usage()
{
    mlog -1 "Usage: psrasgrab -p mgmt_port -P cmd_port [-n count] [-s max_keep_size] [-t max_keep_time]"
    exit 128
}

phase_check()
{
    # Only PSCALE nodes are supported.
    [ "$CDAT_TYPE" = 'PSCALE' ] || {
        mlog -1 "psrasgrab is not supported on $CDAT_TYPE"
        exit 1	# IGNORE
    }

    # Check that the pureScale.client.rte fileset is installed
    fileset="pureScale.client.rte"
    /usr/bin/lslpp -lq $fileset 2>/dev/null | grep -wq COMMITTED
    [ $? -ne 0 ] && {
        mlog -1 "Fileset $fileset not installed"
        exit 1	# IGNORE
    }

    # Check that we have the aix.ras.pureScale and ibm.ps.client.bind
    # authorizations required by psrasc and psbind.
    /usr/bin/ckauth -A aix.ras.pureScale,ibm.ps.client.bind
    [ $? -ne 0 ] && {
        mlog -1 "aix.ras.pureScale and ibm.ps.client.bind authorizations required"
        exit 1	# IGNORE
    }
}

phase_init()
{
    #
    # Establish transient binding with the server and check (-c) connectivity.
    #
    /usr/bin/psbind register -t CentralizedLogService -i "$CDAT_HOST" \
        -p "$mgmtport" -P "$cmdport" -x INTERNET -c CentralizedLogService
    [ $? -ne 0 ] && {
        mlog -1 "Could not establish binding with server $CDAT_HOST"
        exit 1
    }
}

phase_grab()
{
    # Always delete collected log records
    psrasc_opts="-d"
    # Add "-n count" option to psrasc if -n is specified
    [ ! -z "$count" ] && psrasc_opts="$psrasc_opts -n $count"

    #
    # Harvest syslog logs.
    #
    [ -f "$CDAT_DEST_DIR/syslog" ] && {
        # Save existing collect
        mlog 0 "Rotating syslog logs"
        mv "$CDAT_DEST_DIR/syslog" "$CDAT_DEST_DIR"/syslog.`date +%Y"%m"%d-%T`
    }
    mlog 0 "Retrieving syslog logs from $CDAT_HOST..."
    /usr/sbin/psrasc syslog $psrasc_opts -o "$CDAT_DEST_DIR/syslog" \
        CentralizedRAS/Syslog
    [ $? -ne 0 ] && {
	mlog -1 "Could not retrieve syslog logs"
	exit 128
    }
    mlog 0 "Done."

    #
    # Harvest errlog logs.
    #
    [ -f "$CDAT_DEST_DIR/errlog" ] && {
        # Save existing collect
        mlog 0 "Rotating errlog logs"
        mv "$CDAT_DEST_DIR/errlog" "$CDAT_DEST_DIR"/errlog.`date +%Y"%m"%d-%T`
    }
    mlog 0 "Retrieving errlog logs from $CDAT_HOST..."
    /usr/sbin/psrasc errlog $psrasc_opts -o "$CDAT_DEST_DIR/errlog" \
        CentralizedRAS/Errlog
    [ $? -ne 0 ] && {
	mlog -1 "Could not retrieve errlog logs"
	exit 128
    }
    mlog 0 "Done."

    #
    # Remove logs that are too old according to -t limit.
    #
    [ ! -z "$max_keep_time" ] && {
	[ $max_keep_time -lt 1 ] && max_keep_time=1
	mlog 0 "Removing files older than $max_keep_time minutes:"
	/usr/bin/find "$CDAT_DEST_DIR" \( -name 'syslog*' -o -name 'errlog*' \) \
	    ! -mmin "$max_keep_time" -print -exec /usr/bin/rm -f {} \;
	mlog 0 "Done."
    }
    #
    # Remove logs that exceed the -s limit.
    #
    [ ! -z "$max_keep_size" ] && {
	# convert MiB to KiB
	max_keep_size=$((max_keep_size * 1024))
	mlog 0 "Removing old files that exceed the ${max_keep_size}KB limit:"
	total=0
	/usr/bin/ls -1st "$CDAT_DEST_DIR"/syslog* 2>/dev/null |
	while read size name; do
	    total=$((total + size))
	    [ $total -gt $max_keep_size ] && {
		mlog 0 "Removing $name"
		/usr/bin/rm -f "$name"
	    }
	done

	total=0
	/usr/bin/ls -1st "$CDAT_DEST_DIR"/errlog* 2>/dev/null |
	while read size name; do
	    total=$((total + size))
	    [ $total -gt $max_keep_size ] && {
		mlog 0 "Removing $name"
		/usr/bin/rm -f "$name"
	    }
	done
	mlog 0 "Done."
    }
}

phase_clean()
{
    #
    # Disestablish binding with the CA server.
    #
    /usr/bin/psbind remove CentralizedLogService
}

PATH=$CDAT_SRVC_DIR:${PATH}

# Parse collect type options
while getopts n:p:P:s:t: opt
do
    case $opt in
    n)
	count="$OPTARG"
	;;
    p)
	mgmtport="$OPTARG"
	;;
    P)
	cmdport="$OPTARG"
	;;
    s)
	max_keep_size="$OPTARG"
	;;
    t)
	max_keep_time="$OPTARG"
	;;
    *)
	usage
    esac
done

[ -z "$mgmtport" -o -z "$cmdport" ] && usage

case $CDAT_PHASE in
check)
    phase_check
    ;;
init)
    phase_init
    ;;
grab)
    phase_grab
    ;;
clean)
    phase_clean
    ;;
esac

exit 0