#! /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

pmlogger_farm_was_active=false
if which systemctl >/dev/null 2>&1
then
    eval `systemctl show pmlogger.service --property=ActiveState`
    if [ "$ActiveState" = active ]
    then
	pmlogger_farm_was_active=true
    fi
fi

_stop_auto_restart pmcd
_stop_auto_restart pmlogger

_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 2>&1 | _filter_pcp_start
	_restore_auto_restart pmcd
	_restore_auto_restart pmlogger
	_wait_for_pmcd
	_wait_for_pmlogger
	_service pmlogger_farm stop 2>&1 | _filter_pcp_stop
	$pmlogger_farm_was_active && _service pmlogger_farm start >>$seq.full
	_needclean=false
    fi
    $sudo rm -rf $LOGGING_DIR
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    sed \
	-e "/^ln: failed to create hard link.*SaveLogs/d" \
	-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
}

# stop pmcd, primary pmlogger, create the archive directories,
# start pmcd ...
#
_get_pids_by_name -a pmlogger >$tmp.pid
_service pcp stop >$tmp.out 2>&1
cat $tmp.out >$seq.full

# wait for all of the extant pmloggers to exit
#
if [ -s $tmp.pid ]
then
    for pid in `cat $tmp.pid`
    do
	_wait_pmlogger_end $pid
    done
fi

$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

$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 pcp 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
_service pmlogger_farm start
cat $tmp.out >>$seq.full
_wait_for_pmcd
_wait_for_pmlogger
echo "+++ after pcp 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 -u $PCP_USER -g $PCP_GROUP $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 -u $PCP_USER -g $PCP_GROUP $PCP_BINADM_DIR/pmlogger_check -T -l $PMCHECK_LOG
_filter < $PMCHECK_LOG
cat $PMCHECK_LOG >>$seq.full

# all OK
status=0
exit
