#! /bin/sh
# PCP QA Test No. 183
# pmnewlog error handling
#
# Copyright (c) 2014 Red Hat.
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

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

. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    _restore_auto_restart pmcd
    _restore_auto_restart pmlogger
    $sudo rm -f $tmp.* /tmp/Latest
}

signal=$PCP_BINADM_DIR/pmsignal
newlog=$PCP_BINADM_DIR/pmnewlog
trap "_cleanup; exit \$status" 0 1 2 3 15
status=1

_stop_auto_restart pmcd
_stop_auto_restart pmlogger

# used to debug qa test (tes:June/2000)
rm -rf $seq.full

hostname=`hostname`

_filter_ls()
{
    sed \
	-e 's/tmp\/[0-9][0-9]*/tmp\/PID/' \
	-e 's/[A-Z][a-z][a-z]  *[0-9][0-9]* [0-9][0-9]:[0-9][0-9]/TIME/' \
    | $PCP_AWK_PROG '
/TIME/	{ $3 = "user"; $4 = "group" }
	{ print }'
}

_filter()
{
    sed \
        -e "s|${PCP_BINADM_DIR}|PCP_BINADM_DIR|" \
	-e 's/process [0-9][0-9]*/process PID/' \
	-e "s/ at $hostname:/ at HOST:/" \
	-e "s/ at local::/ at HOST:/" \
	-e "s/ on port [0-9][0-9]*//" \
	-e "s/ pid [0-9][0-9]*//" \
	-e '/Error \[<stdin>,/d' \
	-e '/Warning \[<stdin>,/d' \
	-e '/Not connected to any/d' \
	-e '/Logging statement ignored/d' \
	-e '/Problem with lookup for metric/d' \
	-e '/Reason: Unknown metric name/d' \
	-e '/Try again. Information not currently available/d' \
	-e '/Unknown or illegal metric identifier/d' \
	-e '/Unknown or illegal instance domain identifier/d' \
	-e '/Operation not permitted/s/line 1: //' \
	-e 's/Address already in use/[TCP\/IP error]/' \
	-e 's/Connection reset by peer/[TCP\/IP error]/' \
	-e 's/Connection refused/[TCP\/IP error]/'
}

cat <<End-of-File >$tmp.config
log mandatory on once hinv.ndisk
End-of-File

# real QA test starts here
echo "=== usage ==="
$newlog

echo
echo "=== archive exists ==="
echo "=== archive exists ===" >>$seq.full
$sudo rm -f $tmp.save.*
_start_up_pmlogger -c $tmp.config -l $tmp.log $tmp.save >$tmp.err 2>&1
_wait_for_pmlogger $pid $tmp.log
$sudo $newlog -p $pid -l $tmp.pmlogger.log $tmp.save >$tmp.out 2>&1
ok=true
for base in $tmp.save $tmp.save-00
do
    for suff in 0 index meta
    do
	[ -f $base.$suff ] && continue
	echo "Failed! expected file $base.$suff not found"
	ok=false
    done
done
if $ok
then
    echo "OK, previous one renamed using \"-00\" suffix"
else
    echo "output from pmnewlog ..."
    cat $tmp.out
fi
$sudo $signal -a -s TERM pmlogger >/dev/null 2>&1
echo "-- new pmlogger log file --" >>$seq.full
cat $tmp.pmlogger.log >>$seq.full

echo
echo "=== no non-primary logger ==="
echo "=== no non-primary logger ===" >>$seq.full
date >/dev/null &
_wait_pmlogger_end $pid
$sudo $newlog -p $! $tmp 2>&1 | tee -a $seq.full | _filter

echo
echo "=== no primary logger ==="
echo "=== no primary logger ===" >>$seq.full
$sudo $signal -a -s TERM pmlogger
pmsleep 1.1
$sudo $newlog $tmp 2>&1 | tee -a $seq.full | _filter
_service pcp restart \
| _filter_pcp_start \
| sed -e '/pmlogger not running/d'
_wait_for_pmcd
_wait_for_pmlogger

echo
echo "=== process is not called pmlogger ==="
echo "=== process is not called pmlogger ===" >>$seq.full
pmsleep 10 &
$sudo $newlog -p $! $tmp 2>&1 | tee -a $seq.full | _filter

echo
echo "=== process is not really pmlogger ==="
echo "=== process is not really pmlogger ===" >>$seq.full
cp `which sleep` $tmp.pmlogger
$tmp.pmlogger 10 &
$sudo $newlog -p $! $tmp 2>&1 | tee -a $seq.full | _filter \
| sed -e "/\.pmlogger/s/.*/-- ps line removed --/" -e "s/$!/PID/"

echo
echo "=== non-primary logger busy ==="
echo "=== non-primary logger busy ===" >>$seq.full
$sudo rm -f $tmp.save.*
_start_up_pmlogger -L -c /dev/null -l $tmp.log $tmp.save >$tmp.err 2>&1
_wait_for_pmlogger $pid $tmp.log
(( echo "connect $pid"; pmsleep 3.0 ) | pmlc ) &
$sudo $newlog -p $pid $tmp 2>&1 | tee -a $seq.full | _filter \
| sed -e "/\.save/s/.*/-- ps line removed --/" -e "s/$pid/PID/"
$sudo $signal -s TERM $pid

echo
echo "=== primary logger busy ==="
echo "=== primary logger busy ===" >>$seq.full
(( echo "connect primary"; sleep 3 ) | pmlc ) &
$sudo $newlog $tmp 2>&1 | tee -a $seq.full | _filter \
| sed -e "/\/pmlogger/s/.*/-- ps line removed --/"

echo
echo "=== no extracted configuration ==="
echo "=== no extracted configuration ===" >>$seq.full
$sudo rm -f $tmp.save.*
_start_up_pmlogger -L -c /dev/null -l $tmp.log $tmp.save >$tmp.err 2>&1
_wait_for_pmlogger $pid $tmp.log

echo "pid=$pid" >>$seq.full
ps $PCP_PS_ALL_FLAGS | grep pmlogger >>$seq.full

pmsleep 1.1
$sudo $newlog -p $pid $tmp 2>&1 \
| tee -a $seq.full \
| _filter \
| sed \
    -e "/\.save/s/.*/-- ps line removed --/" \
    -e "s/$pid/PID/" \
    -e '/to get logging state/d' \
    -e '/errors from talking/d' \
    -e '/continuing/d' \
    -e '/Reason: Metric not supported by this version/d' \
    -e '/Reason: IPC protocol failure/d' \
    -e '/^$/d'

$sudo $signal -s TERM $pid

_wait_pmlogger_end $pid
echo "-- tmp.log --" >>$seq.full
cat $tmp.log  >>$seq.full
echo "-- tmp.err --" >>$seq.full
cat $tmp.err >>$seq.full
echo "-- pmnewlog --" >>$seq.full

echo
echo "=== wrong uid to kill pmlogger ==="
echo "=== wrong uid to kill pmlogger ===" >>$seq.full

echo "=== initial pmloggers ===" >>$seq.full
# root     29697     1  0 10:45 pts/1    00:00:00 pmlogger -P -c config.default 20101021.10.45
ps $PCP_PS_ALL_FLAGS \
| grep '[p]mlogger' \
| tee -a $seq.full \
| grep -v sudo \
| $PCP_AWK_PROG '
$1 == "root"	{ print $2 }' >$tmp.old
cat $tmp.old >>$seq.full

$sudo rm -f $tmp.save.*
_start_up_pmlogger -L -c $tmp.config -l $tmp.log $tmp.save >$tmp.err 2>&1
pmsleep 1.1

echo "=== final pmloggers ===" >>$seq.full
ps $PCP_PS_ALL_FLAGS \
| grep '[p]mlogger' \
| tee -a $seq.full \
| grep -v sudo \
| $PCP_AWK_PROG '
$1 == "root"	{ print $2 }' >$tmp.new
cat $tmp.new >>$seq.full

#for pid in `cat $tmp.new`
#do
#    if grep "^$pid"'$' $tmp.old >/dev/null
#    then
#	:
#    else
#	break
#    fi
#done

echo "target pmlogger pid=$pid" >>$seq.full
pmsleep 1.1

$newlog -p $pid $tmp 2>&1 \
| tee -a $seq.full \
| _filter \
| sed \
    -e "/\.save/s/.*/-- ps line removed --/" \
    -e '/^kill:.*Not owner/s/Not owner/Operation not permitted/' \
    -e '/.*: kill:.*Not owner/s/Not owner/Operation not permitted/' \
    -e 's/.*kill:.*Operation not permitted/KILL: Operation not permitted/' \
    -e 's/.*kill:.*permission denied/KILL: Operation not permitted/' \
    -e '/to get logging state/d' \
    -e '/errors from talking/d' \
    -e '/continuing/d' \
    -e '/Reason: Metric not supported by this version/d' \
    -e '/Reason: Broken pipe/d' \
    -e '/^$/d'

$sudo $signal -s TERM $pid

status=0
exit
