#! /bin/sh
# PCP QA Test No. 445
# check bug #580005 - trace PMDA doesn't exit if address in use
#
# Copyright (c) 2015 Red Hat.
# 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

[ -f $PCP_PMDAS_DIR/trace/pmdatrace ] || _notrun "trace pmda not installed"

host=`hostname`

_cleanup()
{
    _restore_config $PCP_PMCDCONF_PATH
    _service pcp restart | _filter_pcp_start
    _wait_for_pmcd
    _wait_for_pmlogger
    $sudo rm -f $tmp.*
}

status=1	# failure is the default!
logging=0
trap "_cleanup; exit \$status" 0 1 2 3 15

case "$PCP_PLATFORM" in

linux)
    TRACELOG=$PCP_LOG_DIR/pmcd/trace.log
    cat >> $tmp.conf << EOF
# temporary pmcd.conf for qa/$seq
pmcd    2       dso     pmcd_init       $PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
linux   60      dso     linux_init      $PCP_PMDAS_DIR/linux/pmda_linux.so
trace	10	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 10 -l ${TRACELOG}1
trace	99	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 99 -l ${TRACELOG}2
EOF
    ;;

darwin)
    TRACELOG=$PCP_LOG_DIR/pmcd/trace.log
    cat >> $tmp.conf << EOF
# temporary pmcd.conf for qa/$seq
pmcd    2       dso     pmcd_init       $PCP_PMDAS_DIR/pmcd/pmda_pmcd.dylib
darwin  78      dso     darwin_init     $PCP_PMDAS_DIR/darwin/pmda_darwin.dylib
trace	10	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 10 -l ${TRACELOG}1
trace	99	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 99 -l ${TRACELOG}2
EOF
    ;;

solaris)
    TRACELOG=$PCP_LOG_DIR/pmcd/trace.log
    cat >> $tmp.conf << EOF
# temporary pmcd.conf for qa/$seq
pmcd	2	dso	pmcd_init	$PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
solaris	75	dso	solaris_init	$PCP_PMDAS_DIR/solaris/pmda_solaris.so
trace	10	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 10 -l ${TRACELOG}1
trace	99	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 99 -l ${TRACELOG}2
EOF
    ;;

freebsd)
    TRACELOG=$PCP_LOG_DIR/pmcd/trace.log
    cat >> $tmp.conf << EOF
# temporary pmcd.conf for qa/$seq
pmcd    2       dso     pmcd_init       $PCP_PMDAS_DIR/pmcd/pmda_pmcd.so
freebsd	85	dso	freebsd_init	$PCP_PMDAS_DIR/freebsd/pmda_freebsd.so
trace	10	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 10 -l ${TRACELOG}1
trace	99	pipe	binary 		$PCP_PMDAS_DIR/trace/pmdatrace -d 99 -l ${TRACELOG}2
EOF
    ;;

*)
    echo "Unknown platfrom $PCP_PLATFORM"
    exit 1
    ;;
esac

# real QA test starts here
$sudo rm -f ${TRACELOG}? $seq.full
_save_config $PCP_PMCDCONF_PATH
$sudo cp $tmp.conf $PCP_PMCDCONF_PATH
_service pcp restart | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger
sleep 15

for log in ${TRACELOG}?
do
    fgrep "Address already in use" <$log >/dev/null 2>&1
    status=$?
    if [ $status -eq 0 ]
    then
	# filter the log ...
	sed <$log \
	    -e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9] [0-9][0-9][0-9][0-9]$/DATE/'\
	    -e 's/[A-Z][a-z][a-z] [A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]:[0-9][0-9]/TIMESTAMP/g' \
	    -e "s/$host/HOST/g" \
	    -e 's/\([0-9][0-9]*\)/PID/'
	break
    fi
done

if [ $status -ne 0 ]
then
    echo "--- No match on 'Address already in use' in trace logs ---"
    echo "--- trace log #1 ---"
    cat ${TRACELOG}1
    echo "--- trace log #2 ---"
    cat ${TRACELOG}2
    echo "--- End of trace logs ---"
fi

exit
