#!/bin/sh
# PCP QA Test No. 956
# Exercise pmcd attribute PDU handling after agent failure.
#
# Copyright (c) 2015 Red Hat.
#

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

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

_get_libpcp_config
$unix_domain_sockets || _notrun "No unix domain socket support available"

_filter()
{
    sed -e 's/\.\.\.*/\.\.\./g'
}
_cleanup()
{
    if pmprobe -I pmcd.agent.status | grep '"dynamic"' >/dev/null
    then
        cd $here/pmdas/dynamic
        $sudo ./Remove >>$here/$seq.full 2>&1
        cd $here
    fi
    $sudo rm -f $tmp.*
}

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

cd $here/pmdas/dynamic
if [ -f GNUmakefile.install ]
then
    make -f GNUmakefile.install clean >>$here/$seq.full 2>&1
    make -f GNUmakefile.install >>$here/$seq.full 2>&1
else
    make clean >>$here/$seq.full 2>&1
    make >>$here/$seq.full 2>&1
fi
$sudo ./Install < /dev/null >$tmp.out 2>&1
cat $tmp.out | tee -a $here/$seq.full | _filter_pmda_install | _filter
cd $here

# real QA test starts here
echo "Initial check of some metric access"
pmprobe -h unix: -i hinv.ncpu

pmsleep 0.2

echo "Terminate a PMDA needing attributes"
$sudo $signal -a pmdadynamic >> $seq.full 2>&1

pmsleep 0.2

echo "Tickle access to the failed PMDA, must see 'Try Again'"
pmprobe -h unix: -i hinv.ncpu

echo "Verify subsequent return to healthy state"
pmprobe -h unix: -i hinv.ncpu

# success, all done
status=0
exit
