#!/bin/sh
# PCP QA Test No. 793
# pmlogger_check and pmlogger_daily with shell components in the dir field
# of the control file
#
# Copyright (c) 2015 Ken McDonell.  All Rights Reserved.
#

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

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full
trap "cd $here; $sudo rm -rf $tmp $tmp.*; _cleanup; exit \$status" 0 1 2 3 15
signal=$PCP_BINADM_DIR/pmsignal

_stop_auto_restart pmlogger

_filter()
{
    sed \
	-e 's/\.\.\.*/[dots]/' \
	-e "s;$PCP_BINADM_DIR/pmlogger;pmlogger;g" \
	-e "s/$FULLLOCALHOST/LOCALHOST/g" \
	-e "s/$LOCALHOST/LOCALHOST/g" \
	-e "s/localhost/LOCALHOST/g" \
	-e "s/local:/LOCALHOST/g" \
	-e 's/control:[0-9][0-9]*]/control:LINE]/' \
	-e 's;/usr/var;/var;g' \
	-e '/^Roll .*\/NOTICES/d' \
	-e '/^Start .*\/NOTICES/d' \
	-e "s;$tmp;TMP;g" \
	-e '/Duplicate/d' \
	-e '/Roll [^ ]*\/NOTICES /d' \
	-e '/Start new [^ ]*\/NOTICES$/d' \
	-e '/Restarting/s/process [0-9][0-9]*/process PID/' \
	-e '/Looking for pmlogger/s/process [0-9][0-9]*/process PID/' \
	-e '/Terminating pmlogger/s/process [0-9][0-9]*/process PID/' \
	-e '/^pmlogger \[/{
s/\[[0-9][0-9]*/[PID/
s/from host .*/from host .../
}' \
	-e '/pmlogger -m/s/ [0-9][0-9][0-9][0-9][01][0-9][0-3][0-9]\.[0-5][0-9]\.[0-5][0-9]/ DATE.TIME/' \
	-e '/^pcp /s/ .*/ .../' \
	-e '/^PMCD host /s/host .*/host .../' \
	-e '/^log started /s/started .*/started .../' \
	-e '/^last log entry /s/entry .*/entry .../' \
	-e '/^current time /s/time .*/time .../' \
	-e '/^log size /s/size .*/size .../' \
    | _filter_cron_scripts
}

_stop_loggers()
{
    $sudo $signal -a -s TERM pmlogger
}

_cleanup()
{
    echo
    echo "Cleaning up"

    _stop_loggers >/dev/null 2>&1

    _service pmlogger restart 2>&1 \
    | _filter_pcp_start \
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
}

_setup()
{
    _service pmlogger stop | _filter_pcp_stop
    _stop_loggers >/dev/null 2>&1
    cd
}

_check_loggers()
{
    # Need to make sure all the pmloggers have really started ...
    # wait (up to 30 secs) for each
    #
    for x in A B C
    do
	z=0
	rm -f $tmp.found
	while [ "$z" -lt 30 ]
	do
	    # Cannot use $qatmp as the path prefix, because on some systems
	    # /var/tmp (what qatmp is set to) and /tmp are symlinked, and
	    # pmlogger reports the path in the control file using the real
	    # name (/tmp) not the symlink name (/var/tmp)
	    # ... spotted on vm37 (OpenBSD 6.7)
	    # Fortunately /$x/ is sufficient to identify the desired
	    # pmlogger control file.
	    #
	    pid=`grep -l "/$x/" $PCP_TMP_DIR/pmlogger/[0-9]* | sed -e 's/.*pmlogger.//'`
	    if [ -n "$pid" ]
	    then
		echo "Found pid $pid for $qatmp/$x after $z iterations" >>$here/$seq.full
		echo "status" | pmlc $pid >>$here/$seq.full
		touch $tmp.found
		break
	    fi
	    z=`expr $z + 1`
	    sleep 1
	done
	if [ ! -f $tmp.found ]
	then
	    echo "Arrgh ... pmlogger_* failed to keep pmlogger running for $qatmp/$x" | tee -a $here/$seq.full
	    $PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '[P]ID|[p]mlogger' >>$here/$seq.full
	    for state in $PCP_TMP_DIR/pmlogger/[0-9]*
	    do
		echo "=== $state ===" | tee -a $here/$seq.full
		cat $state | tee -a $here/$seq.full
	    done
	    echo "See $seq.full for details"
	    status=0
	    exit
	fi
    done
}

cat >$tmp.config <<End-of-File
log mandatory on 1 sec {
    hinv.ndisk
    hinv.ncpu
}
End-of-File

# real QA test starts here

_setup

mkdir $tmp

# this one is set via the environment
#
export qatmp=$tmp

# control file
#
cat >$tmp/control <<End-of-File
\$version=1.1
\$A=A
#
# $qatmp from the caller's environment
# $A from the control line above
LOCALHOSTNAME	n   n	\$qatmp/\$A -c $tmp.config
LOCALHOSTNAME	n   n	\$(echo $qatmp/B) -c $tmp.config
LOCALHOSTNAME	n   n	"$qatmp/\`echo c | tr '[a-z]' '[A-Z]'\`" -c $tmp.config
End-of-File

cat $tmp/control >>$here/$seq.full
echo "qatmp=$qatmp" >>$here/$seq.full
echo "PCP_TMP_DIR=$PCP_TMP_DIR" >>$here/$seq.full

echo "pmlogger_check #1" | tee -a $here/$seq.full
$sudo sh -c "qatmp=$qatmp $PCP_BINADM_DIR/pmlogger_check -V -c $tmp/control -l $tmp/log"
[ -s $tmp/log ] && $sudo cat $tmp/log | tee -a $here/$seq.full | _filter
_check_loggers
for x in A B C
do
    ls -l $tmp/$x >>$here/$seq.full
    echo `ls $tmp/$x | grep "\.meta" | wc -l | sed -e 's/ //g'` archives in TMP/$x
done

sleep 2
_stop_loggers >/dev/null 2>&1

echo "pmlogger_check #2" | tee -a $here/$seq.full
$sudo sh -c "qatmp=$qatmp $PCP_BINADM_DIR/pmlogger_check -V -c $tmp/control -l $tmp/log"
[ -s $tmp/log ] && $sudo cat $tmp/log | tee -a $here/$seq.full | _filter
_check_loggers
for x in A B C
do
    ls -l $tmp/$x >>$here/$seq.full
    echo `ls $tmp/$x | grep "\.meta" | wc -l | sed -e 's/ //g'` archives in TMP/$x
done

echo "pmlogger processes ..." >>$here/$seq.full
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | egrep '[P]ID|[p]mlogger' >>$here/$seq.full

echo
echo "pmlogger_daily"
$sudo sh -c "qatmp=$qatmp $PCP_BINADM_DIR/pmlogger_daily -V -x never -c $tmp/control -l $tmp/log"
[ -s $tmp/log ] && $sudo cat $tmp/log | tee -a $here/$seq.full | _filter
_check_loggers
for x in A B C
do
    ls -l $tmp/$x >>$here/$seq.full
    echo `ls $tmp/$x | grep "\.meta" | wc -l | sed -e 's/ //g'` archives in TMP/$x
done

# success, all done
status=0
exit
