#! /bin/sh
# PCP QA Test No. 119
# pmlogger_check failure produces way too many lines of output
# pv 877570
#
# 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

$sudo rm -rf $tmp.*
status=1	# failure is the default!
_needclean=true
[ -z "$PCP_PMLOGGERCONTROL_PATH" ] && \
	PCP_PMLOGGERCONTROL_PATH="$PCP_SYSCONF_DIR/pmlogger/control"

LOGGING_DIR=$PCP_LOG_DIR/pmlogger_qa
PMCHECK_LOG=$LOGGING_DIR/pmlogger_check.log

myhost=`hostname`

trap "_cleanup" 0 1 2 3 15

_stop_auto_restart pmcd
_stop_auto_restart pmlogger
_stop_auto_restart pmmgr

_cleanup()
{
    if $_needclean
    then
	[ -f $tmp.control ] && \
	    $sudo cp $tmp.control $PCP_PMLOGGERCONTROL_PATH
	[ -d $tmp.control.d ] && \
	    $sudo mv $tmp.control.d $PCP_PMLOGGERCONTROL_PATH.d
	_service pcp restart | _filter_pcp_start
	_restore_auto_restart pmcd
	_restore_auto_restart pmlogger
	_restore_auto_restart pmmgr
	_wait_for_pmcd
	_wait_for_pmlogger
	# just in case pmmgr was configured and running
	_service pmmgr restart >/dev/null 2>&1
	_needclean=false
    fi
    $sudo rm -rf $LOGGING_DIR
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    sed \
	-e "s/$myhost/LOCALHOST/g" \
	-e "/UID.*PID/d" \
	-e "/USER.*PID/d" \
	-e "s/.* .*pmlogger .*-c .*/<pmlogger process>/" \
	-e "/Warning: creating directory/s/(.*\/\([^/][^/]*\))/(...\/\1)/" \
    | _filter_pmlogger_log \
    | $PCP_AWK_PROG '
/pmlogger_check: Error: archive file .* missing/ {
		    $5 = "FILE"
		    print
		    skip = 1
		    next
		}
skip == 1	{ next }
		{ print }'
}

$sudo cp $PCP_PMLOGGERCONTROL_PATH $tmp.control
$sudo mv $PCP_PMLOGGERCONTROL_PATH.d $tmp.control.d

cat <<End-of-File >$tmp.tmp
# dummy file created by qa/$seq on `date`
# the goal here is to have just two pmlogger instances running and
# a third one that will never run ...
#
\$version=1.1
LOCALHOSTNAME	y n $LOGGING_DIR/LOCALHOSTNAME-one -c /dev/null
LOCALHOSTNAME	n n $LOGGING_DIR/LOCALHOSTNAME-two -c /dev/null -L
no.such.host.pcp.io	n n $LOGGING_DIR/no.such.host.pcp.io -c /dev/null
End-of-File
$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH

# stop pmcd, create the archive directories, start pmcd ...
#
_service pcp stop >$tmp.out 2>&1
cat $tmp.out >$seq.full
# test may not pass if pmlogger from pmmgr is running
#
_service pmmgr stop >$tmp.out 2>&1
cat $tmp.out >$seq.full

$sudo mkdir -p $LOGGING_DIR/${myhost}-one >/dev/null 2>&1
$sudo mkdir -p $LOGGING_DIR/${myhost}-two >/dev/null 2>&1
$sudo mkdir -p $LOGGING_DIR/no.such.host.pcp.io >/dev/null 2>&1
id pcp >/dev/null 2>&1 && $sudo chown -R pcp:pcp $LOGGING_DIR
echo "+++ before pmcd start ..." >>$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger' >>$seq.full
echo "+++" >>$seq.full
_service pcp start >$tmp.out 2>&1
cat $tmp.out >>$seq.full
_wait_for_pmcd
_wait_for_pmlogger
echo "+++ after pmcd start ..." >>$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger' >>$seq.full
echo "+++" >>$seq.full

# avoid race with pmloggers not being started before pmlogger_check
# is run below
for i in 1 2 3 4 5
do
    [ -f $LOGGING_DIR/${myhost}-one/pmlogger.log -a -f $LOGGING_DIR/${myhost}-two/pmlogger.log ] && break
    sleep 1
done
if [ ! -f $LOGGING_DIR/${myhost}-one/pmlogger.log ]
then
    echo "Failed to start pmlogger for ${myhost}-one"
    echo "Logging directory contents ..."
    ls -l $LOGGING_DIR/${myhost}-one
    exit
fi

if [ ! -f $LOGGING_DIR/${myhost}-two/pmlogger.log ]
then
    echo "Failed to start pmlogger for ${myhost}-two"
    echo "Logging directory contents ..."
    ls -l $LOGGING_DIR/${myhost}-two
    exit
fi

# real QA test starts here
echo
echo "=== pmlogger_check ==="
echo "+++ before pmlogger_check ..." >>$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger' >>$seq.full
echo "+++" >>$seq.full
echo >>$seq.full
echo "=== pmlogger_check ===" >>$seq.full
$sudo $PCP_BINADM_DIR/pmlogger_check -l $PMCHECK_LOG
echo "+++ after pmlogger_check ..." >>$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep '[p]mlogger' >>$seq.full
echo "+++" >>$seq.full
_filter < $PMCHECK_LOG
cat $PMCHECK_LOG >>$seq.full

echo | tee -a $seq.full
echo "=== pmlogger_check -T ===" | tee -a $seq.full
$sudo $PCP_BINADM_DIR/pmlogger_check -T -l $PMCHECK_LOG
_filter < $PMCHECK_LOG
cat $PMCHECK_LOG >>$seq.full

# all OK
status=0
exit
