#! /bin/sh
# PCP QA Test No. 280
# pmlogger_check basic sanity check
#
# 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

if [ -d "$PCP_ARCHIVE_DIR" ]
then
    LOGGING_DIR=$PCP_ARCHIVE_DIR
elif [ -d $PCP_LOG_DIR/pmlogger ]
then
    LOGGING_DIR=$PCP_LOG_DIR/pmlogger
else
    LOGGING_DIR=$PCP_LOG_DIR
fi

_filter()
{
    [ -z "$PCP_PMLOGGERCONTROL_PATH" ] && \
	PCP_PMLOGGERCONTROL_PATH="$PCP_SYSCONF_DIR/pmlogger/control"
    PCP_PMLOGGERCONTROL_PATH=`dirname "$PCP_PMLOGGERCONTROL_PATH"`
    sed \
	-e 's/\.\.\.*/[dots]/' \
	-e 's/control.d\/.*:[0-9][0-9]*]/control:LINE]/' \
	-e 's/control:[0-9][0-9]*]/control:LINE]/' \
	-e 's/process [0-9][0-9]*/process PID/' \
	-e 's;/usr/var;/var;g' \
	-e "s/`hostname`/LOCALHOST/g" \
	-e "s/$LOCALHOST/LOCALHOST/g" \
	-e "s/localhost/LOCALHOST/g" \
	-e "s/local:/LOCALHOST/g" \
	-e '/^Reconfigured: .*pmlogconf./d' \
	-e 's;/private/;/;g' \
	-e "s;$LOGGING_DIR;\$PCP_ARCHIVE_DIR;" \
	-e "s;$PCP_PMLOGGERCONTROL_PATH;\$PCP_PMLOGGERCONTROL_PATH;" \
	-e 's/\.* done/. done/' \
	-e 's/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]/CHECK/' \
	-e 's/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.[0-9][0-9]\.[0-9][0-9]-[0-9][0-9]/CHECK/' \
	-e 's/-rw-r--r--\./-rw-r--r--/' \
    | _filter_cron_scripts
}

status=1	# failure is the default!
signal=$PCP_BINADM_DIR/pmsignal
LOCALHOST=`hostname`
trap "_cleanup; $sudo rm -f $tmp.* $LOGGING_DIR/$LOCALHOST/lock; exit \$status" 0 1 2 3 15

_stop_auto_restart pmcd
_stop_auto_restart pmlogger

_cleanup()
{
    _restore_config $PCP_ETC_DIR/pcp/pmlogger
    _service pmcd restart 2>&1 | tee -a $seq.full | _filter_pcp_start
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _service pmlogger restart 2>&1 | tee -a $seq.full | _filter_pcp_start
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
}

rm -f $seq.full

export PMLOGGER_CHECK_SKIP_LOGCONF=true

# real QA test starts here
_save_config $PCP_ETC_DIR/pcp/pmlogger
_restore_pmlogger_control
umask 022

# start from a known state
#
_service pcp stop >/dev/null 2>&1
_wait_pmcd_end
_service pcp start 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger -P $LOGGING_DIR/$LOCALHOST/pmlogger.log

# these sometimes get left around and are owned by root
$sudo rm -f $PCP_VAR_DIR/config/pmlogger/*.{tmp,saved} 2>/dev/null

echo
echo "Ping primary logger ..." | tee -a $seq.full
echo 'connect primary' | pmlc

echo
echo "Expect this should do nothing ..." | tee -a $seq.full
touch $tmp.log
$sudo chown $PCP_USER:$PCP_GROUP $tmp.log
$sudo -u $PCP_USER -g $PCP_GROUP sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_check -V -l $tmp.log"
[ -f $tmp.log ] && $sudo cat $tmp.log | tee -a $seq.full | _filter
echo 'connect primary' | pmlc

pid=`_get_primary_logger_pid`
if [ -z "$pid" ]
then
    echo "Error: cannot find PID for primary logger"
    LC_TIME=POSIX ls -l $PCP_TMP_DIR/pmlogger
    pminfo -f pmcd.pmlogger
    exit
fi

echo
echo "Kill off primary logger ..." | tee -a $here/$seq.full
_service pmlogger stop 2>&1 | _filter_pcp_stop
_wait_pmlogger_end $pid
echo 'connect primary' | pmlc 2>&1 | tee -a $here/$seq.full | _filter
pmafm $LOGGING_DIR/$LOCALHOST/Latest remove >$tmp.cmd 2>&1 \
&& $sudo sh $tmp.cmd

# delay to minimize risk of archive name clash
#
sleep 2

echo
echo "Expect this to re-start the primary logger ..." | tee -a $seq.full
$sudo -u $PCP_USER -g $PCP_USER sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_check -V -l $tmp.log"
[ -f $tmp.log ] && $sudo cat $tmp.log | tee -a $seq.full | _filter
echo 'connect primary' | pmlc

# if it is present, get systemctl on the same page ...
#
_service pmlogger start 2>&1 | _filter_pcp_start
_wait_for_pmlogger -P $LOGGING_DIR/$LOCALHOST/pmlogger.log

pid=`_get_primary_logger_pid`
if [ -z "$pid" ]
then
    echo "Error: cannot find PID for primary logger"
    LC_TIME=POSIX ls -l $PCP_TMP_DIR/pmlogger
    pminfo -f pmcd.pmlogger
    exit
fi

echo
echo "Kill off primary logger ..." | tee -a $here/$seq.full
_service pmlogger stop 2>&1 | _filter_pcp_stop
_wait_pmlogger_end $pid
echo 'connect primary' | pmlc 2>&1 | tee -a $here/$seq.full | _filter
pmafm $LOGGING_DIR/$LOCALHOST/Latest remove >$tmp.cmd 2>&1 \
&& $sudo sh $tmp.cmd

# delay to minimize risk of archive name clash
#
sleep 2

echo
echo "Expect this to quietly re-start the primary logger ..." | tee -a $seq.full
$sudo -u $PCP_USER -g $PCP_USER sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_check -l $tmp.log"
[ -f $tmp.log ] && $sudo cat $tmp.log | tee -a $seq.full | _filter
echo 'connect primary' | pmlc

echo
echo "Expect this to get a mutex failure ..." | tee -a $seq.full
$sudo touch $LOGGING_DIR/$LOCALHOST/lock
$sudo -u $PCP_USER -g $PCP_USER sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_check -l $tmp.log"
if [ -f $tmp.log ]
then
    $sudo cat $tmp.log | tee -a $seq.full | _filter \
    | $PCP_AWK_PROG '
/-r.-r.-r.-/	{ $3 = "USER"; $4 = "GROUP"; $6 = "Mmm"; $7 = "D"; $8 = "YYYY" }
		{ print }'
fi

echo
echo "Expect this to cleanup an old lock file ..." | tee -a $seq.full
$sudo rm -f $LOGGING_DIR/$LOCALHOST/lock
$sudo touch -t 199701020304.00 $LOGGING_DIR/$LOCALHOST/lock
$sudo -u $PCP_USER -g $PCP_USER sh -c "umask 022; $PCP_BINADM_DIR/pmlogger_check -l $tmp.log"
for i in 1 2 3 4 5 6 7 8 9 10
do
    if [ -f $tmp.log ]
    then
	$sudo cat $tmp.log \
	| tee -a $seq.full \
	| _filter \
	| $PCP_AWK_PROG '
/-r.-r.-r.-/	{ $3 = "USER"; $4 = "GROUP" }
		{ print }'
	break
    fi
    sleep 1
done

if [ ! -f $tmp.log ]
then
    echo "Hmm, $tmp.log not created after 10 seconds ..."
    ls -l $tmp.*
    ls -l $LOGGING_DIR $LOGGING_DIR/$LOCALHOST/lock
fi

$sudo rm -f $LOGGING_DIR/$LOCALHOST/lock

# success, all done
status=0
exit
