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

phase_check()
{
    # Only AIX LPARs are supported for the perfpmr collect type
    if [ "$CDAT_TYPE" != 'LPAR' ]; then
        mlog -1 "perfpmr collect type is not supported on $CDAT_TYPE"
        exit 1	# IGNORE
    fi

    mlog 0 "retrieving OS level from $CDAT_HOST"
    OSLEVEL=`remote_cmd "/usr/bin/oslevel"`
    if [ $? -ne 0 -o -z "$OSLEVEL" ]; then
        mlog -1 "Could not retrieve OS level from $CDAT_HOST"
        exit 128
    fi
    mlog 0 "oslevel=$OSLEVEL"

    REV=`echo $OSLEVEL | /bin/awk 'FS="." {print $1 $2}'`
    PERFPMR="perf$REV.tar.Z"

    if [ ! -f "$CDAT_TYPE_DIR/$PERFPMR" ]; then
        (
        echo "$CDAT_TYPE_DIR/$PERFPMR not found."
        echo "Please install a version of PERFPMR suitable for AIX $OSLEVEL under"
        echo "$CDAT_TYPE_DIR/$PERFPMR."
        echo "You may find it at the following URL:"
        echo "ftp://ftp.software.ibm.com/aix/tools/perftools/perfpmr/"
        ) | tee -a errors.txt
        exit 128
    fi

    mlog 0 "Using $CDAT_TYPE_DIR/$PERFPMR."
    echo "PERFPMR=$PERFPMR" > "$CDAT_DEST_DIR/.perfpmr"

    size=`remote_cmd "LANG=C df -k /tmp" | /bin/awk '(NR==2){print $2}'`
    if [ $? -ne 0 -o "$size" -lt 30720 ]; then
        mlog -1 "perfpmr needs at least 30MB under /tmp on $CDAT_HOST"
        exit 128
    fi
}

phase_init()
{
    eval `cat "$CDAT_DEST_DIR/.perfpmr"`
    [ -z "$PERFPMR" ] && exit 128

    push_file "$CDAT_TYPE_DIR/$PERFPMR" /tmp/
    [ $? -ne 0 ] && {
	mlog -1 "Could not transfer $CDAT_TYPE_DIR/$PERFPMR"
	exit 128
    }
    remote_cmd "mkdir ~/perfpmr; cd ~/perfpmr && (zcat /tmp/$PERFPMR | tar xf -) && rm -f /tmp/$PERFPMR"
    [ $? -ne 0 ] && {
	mlog -1 "Could not install perfpmr"
	exit 128
    }
}

phase_execute()
{
    remote_cmd "mkdir /tmp/perfdata; cd /tmp/perfdata && ~/perfpmr/perfpmr.sh $dval"
    [ $? -ne 0 ] && {
	mlog -1 "Could not run perfpmr, see logs for details"
	exit 128
    }
}

phase_grab()
{
    FILENAME=cdat_perfpmr$$.pax.gz

    remote_cmd "cd /tmp/ && ~/perfpmr/perfpmr.sh -o perfdata -z $FILENAME"
    [ $? -ne 0 ] && {
	mlog -1 "Could not compress perfpmr results"
	exit 128
    }
    get_file /tmp/$FILENAME perfpmr.pax.gz
    [ $? -ne 0 ] && {
	mlog -1 "Could not retrieve perfpmr results"
	exit 128
    }
}

phase_clean()
{
    remote_cmd "rm -fr /tmp/perfdata /tmp/cdat_perfpmr*.pax.gz ~/perfpmr"
    rm -f "$CDAT_DEST_DIR/.perfpmr"
}

PATH=$CDAT_SRVC_DIR:${PATH}

dval=600	# default to 600 seconds (10 minutes)

while getopts d: opt
do
    case $opt in
    d)
	# duration
	dval="$OPTARG"
	;;
    ?)
	mlog -1 "Usage: perfpmr [-d duration]"
	exit 128
	;;
    esac
done

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

exit 0
