#! /bin/sh
# PCP QA Test No. 427
# bogus pmda entries in pmcd.conf
# #564017
#
# 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 [ -z "$PCP_PMCDCONF_PATH" ] ; then 
	echo eek; exit 1;
fi

if [ -z "$PCP_PMCDLOG_PATH" ] ; then 
	echo yuk; exit 1;
fi
status=1	# failure is the default!
needclean=true

[ -z "$PCP_PMLOGGERCONTROL_PATH" ] && \
	PCP_PMLOGGERCONTROL_PATH="$PCP_SYSCONF_DIR/pmlogger/control"
PCP_PMDAROOT_LOG="$PCP_LOG_DIR/pmcd/root.log"

trap "_cleanup" 0 1 2 3 15

_cleanup()
{
    if $needclean
    then
	_restore_config $PCP_PMCDCONF_PATH
	_restore_config $PCP_PMLOGGERCONTROL_PATH
	needclean=false
	_service pmcd restart 2>&1 | _filter_pcp_restart
	_wait_for_pmcd
	_service pmlogger restart 2>&1 | _filter_pcp_restart
	_wait_for_pmlogger

	# make sure bogus has gone away
	echo
	echo "Anything below here may indicate that bogus has not gone away:"
	grep bogus $PCP_PMCDCONF_PATH
	pminfo -f pmcd.agent | grep bogus
    fi
    $sudo rm -f $tmp.*
    exit $status
}

_filter()
{
    grep bogus | \
    sed \
	-e 's/^pmdaroot:/PMCD:/g' \
	-e 's/^pmcd:/PMCD:/g' \
	-e "s#$tmp\.##" \
    #end
}

# real QA test starts here

# disable all pmloggers ...
_save_config $PCP_PMLOGGERCONTROL_PATH
cat <<End-of-File >$tmp.tmp
# Installed by PCP QA test $seq on `date`
# the goal here is to have a controlled primary logger that does
# not make requests to pmcd!
\$version=1.1
LOCALHOSTNAME y n $PCP_LOG_DIR/pmlogger/LOCALHOSTNAME -c /dev/null
End-of-File
$sudo cp $tmp.tmp $PCP_PMLOGGERCONTROL_PATH

# backup $PCP_PMCDCONF_PATH
_save_config $PCP_PMCDCONF_PATH

# add bogus entry
$PCP_AWK_PROG < $PCP_PMCDCONF_PATH > $tmp.pmcd.new -v seq="$seq" -v path="$tmp.bogus.pmda" '
function printline()
{
    printf("# Added by QA test %s\n", seq);
    printf("bogus	169	pipe	binary	%s\n", path);
}

BEGIN			{ flag = 0 }
$1 == "[access]"	{ printline(); print; flag = 1; next }
			{ print }
END			{ if (flag == 0) printline(); }'

# copy the new config
$sudo cp $tmp.pmcd.new $PCP_PMCDCONF_PATH

# restart pmcd
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1

echo == $PCP_PMCDCONF_PATH >> $seq_full
cat $PCP_PMCDCONF_PATH >> $seq_full
echo == $PCP_PMDAROOT_LOG >> $seq_full
cat $PCP_PMDAROOT_LOG >> $seq_full
echo == $PCP_PMCDLOG_PATH >> $seq_full
cat $PCP_PMCDLOG_PATH >> $seq_full

# check that pmcd reported the agent as an error
echo
echo "errors for bogus in pmcd related log file(s):"
[ -f $PCP_PMDAROOT_LOG ] && \
grep -E "^pmdaroot:" $PCP_PMDAROOT_LOG | _filter
grep -E "^pmcd:" $PCP_PMCDLOG_PATH | _filter

# check that the pmcd.agent.status metric is correct
echo
echo "pmcd agent metrics for bogus:"
pminfo -f pmcd.agent.type | grep -E '^pmcd|bogus'
pminfo -f pmcd.agent.status | grep -E '^pmcd|bogus'
echo

# success, all done
status=0
exit

