#!/bin/sh
# PCP QA Test No. 1230
# pmiectl - mixed class and hostname tests - restart actions
#
# see qa/1208 for the pmlogctl variant of this test
#
# 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

if pmiectl -c default status 2>/dev/null | grep ' default ' >/dev/null
then
    _notrun "at least one pmlogger already defined for \"default\" class"
fi

_cleanup()
{
    echo "_cleanup: ..." >>$seq_full
    cd $here
    $sudo pmiectl -f -c default destroy localhost >>$seq_full 2>&1
    $sudo pmiectl -af -c $seq destroy >>$seq_full 2>&1
    $sudo rm -rf $tmp $tmp.*
    for dir in $seq-localhost $seq-LOCALHOSTNAME localhost $seq-`hostname`
    do
	[ -d "$PCP_LOG_DIR/pmie/$dir" ] && $sudo rm -rf "$PCP_LOG_DIR/pmie/$dir"
    done
}

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

localhost=`hostname`

_filter()
{
    tee -a $seq_full \
    | sed \
	-e '/^# created by pmiectl/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/pmiectl\.[^/]*;PCP_TMPFILE_DIR/pmiectl.XXXXX;g" \
	-e "s;$PCP_ETC_DIR/pcp;PCP_ETC_DIR/pcp;g" \
    # end
}

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

# Note status command output order is non-deterministic, hence the sort
# at the end
#
_filter_status()
{
    tee -a $seq_full \
    | $PCP_AWK_PROG -f $tmp.awk \
    | sed \
	-e "s;$PCP_ETC_DIR/pcp;PCP_ETC_DIR/pcp;g" \
	-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 /" \
    | $PCP_AWK_PROG '
$2 ~ /^[0-9][0-9]*$/	{ $2 = "<nrule>" }
$3 ~ /^[0-9][0-9]*$/	{ $3 = "<neval>" }
$5 ~ /^[0-9][0-9]*$/	{ $5 = "<pid>" }
			{ print }' >$tmp.tmp
    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]
%h n n PCP_LOG_DIR/pmie/%i/pmie.log -c $tmp.config
End-of-File

cat <<End-of-File >$tmp.config
delta = 1 sec;
some_inst pmcd.pmie.eval.actual > 0 ->
    print "pmie is alive: rule evals:" " [%i] %v";
End-of-File

# real QA test starts here
echo '== create running pmlogger instances' | tee -a $seq_full
$sudo pmiectl -p $tmp.policy -c $seq create localhost | _filter
$sudo pmiectl -p $tmp.policy -c $seq create LOCALHOSTNAME | _filter
$sudo pmiectl create localhost | _filter
pmiectl status | _filter_status

# exercise all the variants for "restart"
echo '== -a restart localhost' | tee -a $seq_full
$sudo pmiectl -a restart localhost | _filter
pmiectl status | _filter_status
echo '== -a restart localhost `hostname`' | tee -a $seq_full
$sudo pmiectl -a restart localhost `hostname` 2>$tmp.err | _filter_status
_filter <$tmp.err
pmiectl status | _filter_status
echo '== -a -c restart' | tee -a $seq_full
$sudo pmiectl -a -c $seq restart | _filter_status
pmiectl status | _filter_status
echo '== -a -c restart localhost' | tee -a $seq_full
$sudo pmiectl -a -c $seq restart localhost | _filter_status
pmiectl status | _filter_status
echo '== -a -c restart localhost `hostname`' | tee -a $seq_full
$sudo pmiectl -a -c $seq restart localhost `hostname` | _filter_status
pmiectl status | _filter_status

status=0
exit
