#!/bin/sh
# PCP QA Test No. 1223
# pmlogctl - multiple pmloggers per control file tests
# (migration from an existing config, or hand-edited, as pmlogctl create
# will not set it up this way)
#
# Copyright (c) 2020 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

_cleanup()
{
    echo "_cleanup: ..." >>$seq_full
    cd $here
    $sudo pmlogctl -af -c $seq destroy >>$seq_full 2>&1
    for dir in $seq-localhost $seq-$localhost $seq-local:
    do
	[ -d "$PCP_ARCHIVE_DIR/$dir" ] && $sudo rm -rf "$PCP_ARCHIVE_DIR/$dir"
    done
    $sudo rm -rf $tmp $tmp.*
}

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

localhost=`hostname`
echo "localhost -> \"$localhost\"" >>$seq_full
echo "hostname -f -> \"`hostname -f 2>&1`\"" >>$seq_full
echo "pmhostname -> `pmhostname -Dappl0 2>&1`" >>$seq_full
host localhost >>$seq_full 2>&1
host $localhost >>$seq_full 2>&1
host `hostname -f 2>&1` >>$seq_full 2>&1
host `pmhostname` >>$seq_full 2>&1

_filter()
{
    tee -a $seq_full \
    | sed \
	-e '/^# created by pmlogctl/s/ on .*/ on DATE/' \
	-e "s;$tmp\.;TMP.;g" \
	-e "s;$PCP_BINADM_DIR/;PCP_BINADM_DIR/;g" \
	-e "s;$PCP_ARCHIVE_DIR/;PCP_ARCHIVE_DIR/;g" \
	-e "s;$PCP_TMP_DIR/;PCP_TMP_DIR/;g" \
	-e "s;$PCP_TMPFILE_DIR/pmlogctl\.[^/]*;PCP_TMPFILE_DIR/pmlogctl.XXXXX;g" \
	-e "s;$PCP_ETC_DIR/;PCP_ETC_DIR/;g" \
	-e 's/PID=[0-9][0-9]*/PID=<somepid>/' \
	-e 's/PID [0-9][0-9]*/PID <somepid>/' \
	-e 's/TERM [0-9][0-9]*/TERM <somepid>/' \
    # end
}

# Build filter for any existing non-qa and non-primary pmlogger instances.
# The "pmcd Host" and "Class" fields from the pmlogctl status output
# should suffice to uniquely identify each.
#
pmlogctl status 2>&1 \
| $PCP_AWK_PROG >$tmp.awk '
NR == 1	{ next }
NF >= 5	{ if ($3 == "primary") next
	  print "$1 == \"" $1 "\" && $3 == \"" $3 "\" { next }"
	}
END	{ print "{ print }" }'

# Note:
# 	Status command output order is non-deterministic, hence the sort
#	at the end
# Another note:
#     Strange in CI, some platforms report an instance name like
#	1223-<hostname><space>
#     the trailing space is a total mystery, but just filter it away
#     here (see 2nd ;-$localhost..; sed) as there is no other apparent
#     failure
#
_filter_status()
{
    tee -a $seq_full \
    | $PCP_AWK_PROG -f $tmp.awk \
    | sed >$tmp.tmp \
	-e "/^`hostname` .* primary /d" \
	-e 's/[ 	][ 	]*/ /g' \
	-e 's/2[0-9][0-9][0-9][01][0-9][0-3][0-9]\...\.[^ ]*/<archivename>/' \
	-e "s/^$localhost /LOCALHOSTNAME /" \
	-e "s;/$localhost\([ /]\);/LOCALHOSTNAME\1;" \
	-e "s;-$localhost\$;-LOCALHOSTNAME;" \
	-e "s;-$localhost \$;-LOCALHOSTNAME;" \
	-e "s/ $seq / <seq> /" \
	-e 's/ [0-9][0-9]* / <pid> /' \
	-e 's/ $//' \
    # end
    head -1 $tmp.tmp
    sed -e '1d' $tmp.tmp | LC_COLLATE=POSIX sort
}

cat <<End-of-File >$tmp.policy
[class]
$seq
[ident]
$seq-%h
[control]
\$version=1.1
%h n n PCP_ARCHIVE_DIR/%i -c $tmp.config
End-of-File

cat <<End-of-File >$tmp.config
log mandatory on default { pmcd.pmlogger }
End-of-File

cat <<End-of-File >$tmp.control
# Installed by PCP QA test $seq on `date`
\$class=$seq
\$version=1.1

# eeny
local: n n PCP_ARCHIVE_DIR/$seq-local: -r -c $tmp.config

# meeny
localhost n n PCP_ARCHIVE_DIR/$seq-localhost -r -c $tmp.config

# miney
LOCALHOSTNAME n n PCP_ARCHIVE_DIR/$seq-LOCALHOSTNAME -r -c $tmp.config

# mo
End-of-File

_setup()
{
    $sudo cp $tmp.control $PCP_ETC_DIR/pcp/pmlogger/control.d/$seq
    echo "=== setup" >>$seq_full
    $sudo pmlogctl -V -c $seq restart >>$seq_full 2>&1
    pmlogctl -V status >>$seq_full 2>&1
}

# real QA test starts here
_setup

if false
then
    if echo "$localhost" | grep '^[0-9][0-9a-f]*$' >>/dev/null
    then
	# special for CI ...
	#
	pmlogctl status
	$sudo pmlogctl -c $seq -VV stop LOCALHOSTNAME
	pmlogctl -V status
	$sudo pmlogctl -c $seq -VV start LOCALHOSTNAME
	pmlogctl -V status
    fi
fi

for host in LOCALHOSTNAME local: localhost
do
    echo "== stop $host" | _filter
    $sudo pmlogctl -c $seq stop "$host" 2>&1 | _filter
    diff $tmp.control $PCP_ETC_DIR/pcp/pmlogger/control.d/$seq | _filter
    pmlogctl -V status 2>&1 | _filter_status
done

echo
for host in LOCALHOSTNAME local: localhost
do
    echo "== start $host" | _filter
    $sudo pmlogctl -c $seq start "$host" 2>&1 | _filter
    diff $tmp.control $PCP_ETC_DIR/pcp/pmlogger/control.d/$seq | _filter
    pmlogctl -V status 2>&1 | _filter_status
done

echo
for host in LOCALHOSTNAME local: localhost
do
    echo "== destroy $host" | _filter
    $sudo pmlogctl -f -c $seq destroy "$host" 2>&1 | _filter
    if [ -f $PCP_ETC_DIR/pcp/pmlogger/control.d/$seq ]
    then
	diff $tmp.control $PCP_ETC_DIR/pcp/pmlogger/control.d/$seq | _filter
    else
	echo "PCP_ETC_DIR/pcp/pmlogger/control.d/$seq has gone away"
    fi
    pmlogctl -V status 2>&1 | _filter_status
done

# success, all done
status=0
exit
