#!/bin/sh
# PCP QA Test No. 1339
# check out sample PMDA
# - Internet socket part, see qa/156 for the other PMDA connection
#   cases
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_filter()
{
    _filter_pmda_install \
    | _filter_compiler_babble \
    | sed \
	-e 's/ 1[34][0-9][0-9] values/ 1300+ values/' \
	-e 's/ 1[0-9] warnings/ 10+ warnings/' \
	-e 's/ [12][0-9][0-9] metrics/ 100+ metrics/' \
	-e "s;$tmp;TMP;" \
	-e "s/$port1/PORT1/" \
	-e "s/$port2/PORT2/" \
    # end
}

_check_pmda_gone()
{
    sleep 2
    pminfo -v $1 && echo "Error: pminfo -v found some \"$1\" metrics"
    # PMDA process may be a zombie because pmcd has not yet harvested
    # it ... in this case, name will appear as [pmda$1] or (pmda$1)
    # so skip these ones
    #
    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep "[^([][p]mda$1[^])]" >$tmp.tmp
    if [ -s $tmp.tmp ]
    then
	echo "Error: some \"$1\" PMDAs still present"
	cat $tmp.tmp
    fi
    rm -f $tmp.tmp
}

_snag_logs()
{
    for x in sample pmcd
    do
	if [ -f "$PCP_LOG_DIR/pmcd/$x.log" ]
	then
	    cat "$PCP_LOG_DIR/pmcd/$x.log" >>$here/$seq.full
	else
	    echo "$PCP_LOG_DIR/pmcd/$x.log: missing?"
	fi
    done
}

status=1

$sudo rm -f $seq.full

echo "PCPQA_IN_CI=$PCPQA_IN_CI" >>$seq.full
if [ "$PCPQA_IN_CI" = yes ]
then
    # in CI
    # failing on test-ubuntu1804i386-container and nowhere else ...
    # despite extensive triage, tcp port "already in use" problem
    # persists
    #
    eval `src/sizeof ptr`
    echo "ptr=$ptr" >>$seq.full
    if [ "$ptr" = 4 ]
    then
	admin/whatami >>$seq.full
	# 32-bit platform
	case `admin/whatami`
	in
	    *Ubuntu\ 18.04\ *)
		# bingo ... no cigar
		_notrun "cannot make this test work for test-ubuntu1804i386-container"
		;;
	esac
    fi
fi

# save pmcd.conf and restore it after the test
cp $PCP_PMCDCONF_PATH $tmp.pmcd.conf

home=$PCP_PMDAS_DIR
iam=sample
if [ ! -d $home/$iam ]
then
    echo "Where is $home/$iam?"
    exit 1
fi
cd $home/$iam
unset ROOT MAKEFLAGS

# force dynamic.indom to be what we expect ...
#
echo "1 one" >$tmp.indom
echo "2 two" >>$tmp.indom
$sudo cp $tmp.indom dynamic.indom

# make sure sample agent is running again at the end, so other QA tests
# continue to work
#
trap "echo; echo 'Reinstall PMDA ...'; $sudo ./Install </dev/null >$tmp.out; _filter <$tmp.out; $sudo cp $tmp.pmcd.conf $PCP_PMCDCONF_PATH; $sudo rm -f $tmp.*; exit \$status" 0 1 2 3 15

# pick two tcp ports that are not in use
#
port1=`_find_free_port 5670`
if [ -z "$port1" ]
then
    echo "Arrgh ... port1: no free TCP port in the range 5670 ... "
    exit
fi
echo "port1=$port1" >>$here/$seq.full
if which netstat >/dev/null 2>&1
then
    netstat -a | grep $port1 >>$here/$seq.full
fi
try=`expr $port1 + 1`
port2=`_find_free_port $try`
if [ -z "$port2" ]
then
    echo "Arrgh ... port2: no free TCP port in the range $try ... "
    exit
fi
echo "port2=$port2" >>$here/$seq.full
if which netstat >/dev/null 2>&1
then
    netstat -a | grep $port2 >>$here/$seq.full
fi

# real QA test starts here

echo
echo "=== remove agent ==="
$sudo ./Remove >$tmp.out 2>&1
cat $tmp.out
_check_pmda_gone $iam

echo | tee -a $here/$seq.full
echo "=== socket Internet agent ===" | tee -a $here/$seq.full

# Give the PMDA + internet socket a chance to be setup before pminfo check
#
export PCPQA_CHECK_DELAY=3

echo 'socket
Internet
'$port1 | $sudo ./Install -e >$tmp.out 2>&1
_filter <$tmp.out
if pminfo -v sample
then
    :
else
    echo "... failed!" | tee -a $here/$seq.full
    pminfo -fd sample.long.ten >>$here/$seq.full
fi
_snag_logs

echo 'socket
Internet
'$port2 | $sudo ./Install -e >$tmp.out 2>&1
_filter <$tmp.out
if pminfo -v sample
then
    :
else
    echo "... failed!" | tee -a $here/$seq.full
    pminfo -fd sample.long.ten >>$here/$seq.full
fi
_snag_logs

$sudo ./Remove >$tmp.out 2>&1
cat $tmp.out
_check_pmda_gone $iam

status=0
exit
