#!/bin/sh
# PCP QA Test No. 374
# pmlogger (assorted) and pmlc (PCP 2.0) version compatibility
#
# 64-bit host version ... see 322 for 32-bit host version ... this
# one is optionally not run if you can't find a 64-bit host running
# pcp
#
# 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


# based on version 2 or 3 archive remotely ($1)
#
_filter()
{
    tee -a $seq_full \
    | _filter_pmdumplog \
    | sed \
	-e '/^pmlogger .* on host .* is logging metrics from host .*/d' \
	-e '/^PMCD host/d' \
	-e '/^log started/d' \
    | $PCP_AWK_PROG '
BEGIN				{ if ("'"$1"'" >= 3) labelsize=808
				  else labelsize=132
				}
/datax/				{ skip = 3 }
skip > 0			{ skip--; next }
$1 == "log" && $2 == "size"	{ if ($3 == labelsize) $3 = "[label]"
				  else if ($3 < labelsize+100) $3 = "[label] plus less than 100"
				  else if ($3 <= labelsize+200) $3 = "[label] plus more than 100"
				  else $3 = "[label] plus more than 200"
				}
				{ print }'
}

_remote_signal()
{
    host="$1"
    pid="$2"
    base="$3"

    cat <<End-of-File >$tmp.cmd
. \$PCP_DIR/etc/pcp.env
ls -l \$PCP_TMP_DIR/pmlogger >&2
\$PCP_BINADM_DIR/pmsignal -s TERM $pid
sleep 1
\$PCP_BINADM_DIR/pmsignal -s KILL $pid
sleep 1
ls -l \$PCP_TMP_DIR/pmlogger >&2
[ -k \$PCP_TMP_DIR/pmlogger ] || sudo chmod o-w \$PCP_TMP_DIR/pmlogger 2>/dev/null
ls -ld \$PCP_TMP_DIR/pmlogger >&2
rm -f /tmp/$base.*
End-of-File
    scp -q $tmp.cmd pcpqa@$host:$seq.cmd
    ssh -q pcpqa@$host sh ./$seq.cmd >$tmp.out 2>$tmp.err
    ssh -q pcpqa@$host "rm -f $seq.cmd"

    echo >>$seq_full
    echo "_remote_signal: ssh to $host ..." >>$seq_full
    echo "--- stdout ---" >>$seq_full
    cat $tmp.out >>$seq_full
    echo "--- stderr ---" >>$seq_full
    cat $tmp.err >>$seq_full
}

_speak_to_me()
{
    host=$1
    pid=''
    base=''
    cat <<End-of-File >$tmp.cmd
if [ -r \$PCP_DIR/etc/pcp.env ]
then
    . \$PCP_DIR/etc/pcp.env
else
    echo "Error: unable to read \$PCP_DIR/etc/pcp.env!" >&2
    exit 1
fi
rm -f /tmp/\$\$.*
[ -k \$PCP_TMP_DIR/pmlogger ] || sudo chmod o+w \$PCP_TMP_DIR/pmlogger
ls -ld \$PCP_TMP_DIR/pmlogger >&2
if [ -w \$PCP_TMP_DIR/pmlogger ]
then
    :
else
    echo "Error: unable to make \$PCP_TMP_DIR/pmlogger writeable" >&2
    exit 1
fi
# force one pmResult into the archive to trigger log label
# and preamble writing
#
echo "log mandatory on once sampledso.long.ten" >/tmp/\$\$.config
( pmlogger -T5min -L -c /tmp/\$\$.config -l /tmp/\$\$.log /tmp/\$\$ </dev/null >/dev/null 2>&1 ) &
sleep 1
ls -l \$PCP_TMP_DIR/pmlogger >&2
echo pid=\$! base=\$\$
End-of-File
    scp -q $tmp.cmd pcpqa@$host:$seq.cmd
    ssh -q pcpqa@$host sh ./$seq.cmd >$tmp.out 2>$tmp.err
    ssh -q pcpqa@$host "rm -f $seq.cmd"

    eval `ssh -q pcpqa@$host grep PCP_ARCHIVE_VERSION /etc/pcp.conf`

    echo >>$seq_full
    echo "_speak_to_me: ssh to $host ..." >>$seq_full
    echo "--- stdout ---" >>$seq_full
    cat $tmp.out >>$seq_full
    echo "--- stderr ---" >>$seq_full
    cat $tmp.err >>$seq_full

    eval `cat $tmp.out`

    if [ -z "$pid" ]
    then
	echo "Arrgh ... failed to get remote pmlogger pid for host $host"
	exit
    fi

    if [ -z "$base" ]
    then
	echo "Arrgh ... failed to get remote archive basename for host $host"
	exit
    fi

    for i in 1 2 3 4 5 6 7 8 9 10
    do
	if echo quit | pmlc -h $host $pid 2>&1 | grep 'Connected to' >/dev/null
	then
	    break
	else
	    if [ $i = 10 ]
	    then
		echo "Arrgh ... pmlogger (pid=$pid) on host $host failed to start after 20 seconds"
		ssh -q pcpqa@$host ps -ef | grep pmlogger
		ssh -q pcpqa@$host ls -l /tmp/$base.*
		ssh -q pcpqa@$host pmdumplog -dilmst /tmp/$base
		ssh -q pcpqa@$host cat /tmp/$base.log
		sleep 60
		exit 1
	    fi
	fi
	sleep 2
    done

    # the success cases
    #
    cat <<End-of-File | pmlc 2>&1 | _filter $PCP_ARCHIVE_VERSION
connect $pid@$host
status
new volume
status
flush
# singular, all instances
query { pmcd.simabi pmcd.control.register }
# some instances
query pmcd.agent.type ["sample" "pmcd" "sampledso"]
# non-leaf
query pmcd.pdu_in
# logging
log mandatory on once pmcd.agent.type ["sample" "pmcd"]
End-of-File

    # stopping and starting again should be enough to allow pmlogger
    # to have logged the metrics from the last request ... except the
    # pmlc control port socket may not have been ripped down, so
    # sleep some
    #
    sleep 2

    cat <<End-of-File | pmlc 2>&1 | _filter $PCP_ARCHIVE_VERSION
connect $pid@$host
log mandatory off pmcd.agent.type ["sampledso"]
query pmcd.agent.type ["sample" "pmcd" "sampledso"]
End-of-File

    # the failures
    #
    echo "connect 0@$host" | pmlc 2>&1 | _filter $PCP_ARCHIVE_VERSION

    # cleanup
    #
    if [ ! -z "$host" ]
    then
	_remote_signal $host $pid $base
	ssh -q pcpqa@$host rm -f /tmp/$base.*
	pid=''
	base=''
    fi
}

_cleanup()
{
    if $need_clean
    then
	if [ ! -z "$pid" -a ! -z "$base" -a ! -z "$host" ]
	then
	    _remote_signal $host $pid $base
	    pid=''
	    base=''
	fi
	need_clean=false
    fi
    $sudo rm -f $tmp.*
}

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

host=''
pid=''
base=''

# the remote host may be on the other side of the planet
#
PMCD_CONNECT_TIMEOUT=30
PMCD_REQUEST_TIMEOUT=30
export PMCD_CONNECT_TIMEOUT PMCD_REQUEST_TIMEOUT

# real QA test starts here

# strickly speaking need a remote host runnun pcp>=2, but with old
# PCP 2.7.8 and PCP 4.0.0 hosts in the QA farm, we might pick one of
# them, and there are fewer metrics below pmcd.pdu_in in the PMNS,
# which causes slightly different output for the
#	query pmcd.pdu_in
# below ... so best to avoid that.
#
for type in '-b 64 -v pcp\>=5.3.5'
do
    cmd="./getpmcdhosts -P -L -n 1 $type 2>/dev/null"
    host=`eval $cmd`
    if [ -z "$host" ]
    then
	need_clean=false
	_notrun "Cannot find a 64-bit host running PCP"
	# NOTREACHED
    else
	echo
	echo "=== pmlogger host type: $type ===" | sed -e 's/\\//g'
	_speak_to_me $host
    fi
done

# success, all done
status=0
exit
