#! /bin/sh
# PCP QA Test No. 115
# exercises pmie_check functionality
#
# Copyright (c) 2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -f /etc/gentoo-release ] && _notrun "Gentoo is lacking chkconfig support"
if [ -z "$PCP_PMIECONTROL_PATH" ]
then
    echo "Error: botched installation - PCP_PMIECONTROL_PATH not defined"
    exit 1
fi

_cleanup()
{
    if $needclean
    then
	_restore_config $PCP_PMIECONTROL_PATH
	_restore_config $PCP_PMCDCONF_PATH
    fi
    $sudo rm -f $tmp.*
}

status=1	# failure is the default!
$sudo rm -f $tmp.* $seq.full
signal=$PCP_BINADM_DIR/pmsignal
needclean=false
trap "_cleanup; exit \$status" 0 1 2 3 15

lhost=`hostname | sed -e 's/\..*//'`
host=`_get_fqdn`
remote=`./getpmcdhosts -n 1 -L 2>$tmp.out`
rhost=`echo $remote | sed -e 's/\..*//'`
[ -z "$remote" ] && _notrun `cat $tmp.out`

# Now that we have pmmgr in the mix, there is more than one pmie
# potentially running, so lines like this
# ... try /var/lib/pcp/tmp/pmie/25453: different logfile, skip
#  /var/log/pcp/pmmgr/bozo/pmie.log differs to /tmp/SEQ-PID.log1
# are now expected, and not a problem.
#
_filter()
{
    sed \
	-e "s/$seq-$$/SEQ-PID/g" \
	-e "s;$PCP_BIN_DIR/pmie;\$PCP_BIN_DIR/pmie;" \
	-e 's/$/ /' \
	-e 's/\([ "]\)'"$remote"'\([ "]\)/\1REMOTEHOST\2/g' \
	-e 's/\([ "]\)'"$rhost"'\([ "]\)/\1REMOTEHOST\2/g' \
	-e 's/\([ "]\)'"$host"'\([ "]\)/\1LOCALHOST\2/g' \
	-e 's/\([ "]\)local:\([ "]\)/\1LOCALHOST\2/g' \
	-e "s;/private/tmp;/tmp;g" \
	-e '/ try .* different logfile, skip/d' \
	-e '/\/pmmgr\/.* differs to /d' \
	-e 's/ $//'
}

_count_pmies()
{
    count=0
    if [ -d $PCP_TMP_DIR/pmie ] 
    then cd $PCP_TMP_DIR/pmie
    else return 0
    fi

    ls -l >>$here/$seq.full
    plist=`ls -1`
    echo "plist=$plist" >>$here/$seq.full
    cd $here

    for process in $plist
    do
	ps -p $process >/dev/null 2>&1
	if [ $? = 1 ]
	then
	    echo "urk, $PCP_TMP_DIR/pmie/$process has no running pmie instance"
	else
	    count=`expr $count + 1`
	    ps -f -p $process >>$here/$seq.full
	    if `which pstree >/dev/null 2>&1`
	    then
		pstree $process >>$here/$seq.full
	    fi
	fi
    done

    return $count
}

# create a basic pmcd config file
cat >$tmp.pmcd.conf << EOF
# from QA $seq
irix	1	dso	irix_init	libirixpmda.so
pmcd	2	dso	pmcd_init	pmda_pmcd.so
proc	3	dso	proc_init	pmda_proc.so
EOF

# create a pmie config file
cat >$tmp.conf << EOF
foo = sample.long.one;
doo = sample.long.ten;
EOF

# create pmie control files
cat >$tmp.control << EOF
\$version=1.0
$remote         n $tmp.log1 -c $tmp.conf
$remote         n $tmp.log2 -c $tmp.conf
LOCALHOSTNAME   n $tmp.log0 $tmp.conf
EOF

# real QA test starts here
_save_config $PCP_PMIECONTROL_PATH
_save_config $PCP_PMCDCONF_PATH
needclean=true
$sudo cp $tmp.pmcd.conf $PCP_PMCDCONF_PATH 

$sudo $signal -a -s TERM pmie >/dev/null 2>&1
sleep 1
_change_config pmcd on
_change_config pmie off
$sudo rm -f $PCP_TMP_DIR/pmie/*

_count_pmies
echo "pmie count at start of QA testing: $?"
echo

echo === check default install operation ===
$sudo $PCP_RC_DIR/pmie restart 2>&1 | _filter_pmie_start
_count_pmies
# chkconfig no longer controls start-ability, expect non-zero
echo "pmie count after chkconfig pmie off: $?"
echo

echo === check for missing control file ===
_change_config pmie off
$sudo rm -f $PCP_PMIECONTROL_PATH
_change_config pmie on
$sudo $PCP_RC_DIR/pmie restart 2>&1 | _filter_pmie_start
_count_pmies
echo "pmie count after attempt without control file: $?"
echo

echo === check pmie_check and custom config ===
pmie_check -V -V -N -c $tmp.control -l $tmp.log
cat $tmp.log | _filter
# don't bother counting pmie processes, as -N is used above!
#
echo

$sudo $PCP_RC_DIR/pmie stop 2>&1 \
| _filter_pmie_start \
| sed -e '/pmie: PMIE not running/d'
_change_config pmie off

status=0
exit
