#! /bin/sh
# PCP QA Test No. 022
# proc PMDA exerciser
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

if [ $PCP_PLATFORM = darwin -o $PCP_PLATFORM = solaris ]
then
    _notrun No proc metrics for $PCP_PLATFORM
else
    # proc metrics may not be available
    #
    if pminfo proc.nprocs >/dev/null 2>&1
    then
	:
    else
	_notrun proc PMDA not installed
    fi
fi

rm -f $seq.out $seq.full
if [ $PCP_PLATFORM = linux -a $PCP_EOE_VER -ge 2704 ]
then
    ln $seq.linux.2 $seq.out
elif [ $PCP_PLATFORM = linux -a $PCP_EOE_VER -ge 2300 ]
then
    ln $seq.linux.1 $seq.out
elif [ $PCP_PLATFORM = linux ]
then
    ln $seq.linux $seq.out
elif [ $PCP_PLATFORM = irix -a $PCP_EOE_VER -lt 6514 ]
then
    ln $seq.irix.0 $seq.out
elif [ $PCP_PLATFORM = irix -a $PCP_EOE_VER -ge 6514 ]
then
    ln $seq.irix.1 $seq.out
else
    echo "Warning: no validated output for $PCP_PLATFORM"
fi

echo "QA output created by $seq"

trap "rm -f $tmp.*; exit" 0 1 2 3 15

_filter_pmids()
{
    sed \
	-e 's/indom=[0-9][0-9]* \[/indom=INDOM [/g' \
	-e 's/3\.[0-9][0-9]*\.[0-9][0-9]*/PMID/g' \
	-e 's/\[3\.9]/[INDOM]/g' \
	-e 's/60\.[0-9][0-9]*\.[0-9][0-9]*/PMID/g' \
	-e 's/\[60\.9]/[INDOM]/g'
}

_proc_filter() 
{
    input=$1

    # Set variables pid, ppid, cpid for process ids
    eval `grep '^pid=' $input`
    eval `grep '^cpid=' $input`

    sed < $input \
	-e '/proc.memory.*.txt/s/valfmt: [01]/valfmt: 0-or-1/' \
	-e '/proc\.psinfo\.[us]time/s/valfmt: [01]/valfmt: 0-or-1/' \
	-e '/unknown pid/d' \
	-e 's/^/ /' \
	-e 's/$/ /' \
	-e "s/\([^0-9]\)0*$pid\([^0-9]\)/\1PID\2/g" \
	-e "s/\([^0-9]\)0*$ppid\([^0-9]\)/\1PPID\2/g" \
	-e "s/\([^0-9]\)0*$cpid\([^0-9]\)/\1CPID\2/g" \
	-e 's/^ //' \
	-e 's/ $//' \
	-e 's/fd=[0-9][0-9]*/fd=N/g' \
	-e 's/refcnt=[0-9]/refcnt=N/g' \
	-e 's/inst \[.*\] value/inst [NNN] value/' \
	-e '/value /{
    s/value [0-9][0-9]*/value INTEGER/
    s/value ".*"/value STRING/
    s/value \[.*\]/value BINARYDATA/
    }' \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \
    \
    | $PCP_AWK_PROG '
    /End Fetch Over Entire Instance Domain/	{ all = 0; print; next; }
    /Fetch Over Entire Instance Domain/	{ all = 1 }
    all == 1 && / inst \[/		{ next }
    all == 1 && /] value /		{ next }
    all == 1 && $3 == "numval:"		{ $4 = "LOTS" }
					{ print }' \
    | _filter_pmids

}

# real QA test starts here

echo "=== pminfo -F output ===" >$seq.full

# try fetching all proc metrics
# cull out the not supported msg component
pminfo -F proc \
| tee -a $seq.full \
| _inst_value_filter \
| sed -e '/value/d' \
    -e '/not supported/d' \
    -e '/^[ \t]*$/d' \
    -e '/DISAPPEARED/d' \
    -e '/^proc\.fd\.count$/d'

if [ $PCP_PLATFORM = linux ]
then
    src/proc_test \
	proc.psinfo.utime \
	proc.psinfo.stime \
	proc.memory.rss \
	proc.memory.size \
	> $tmp.out 2>&1
    code=$?
else
    src/proc_test \
	proc.psinfo.time \
	proc.pstatus.utime \
	proc.psusage.utime \
	proc.pscred.suid \
	proc.memory.virtual.txt \
	proc.memory.physical.txt \
	proc.psinfo.uname \
	proc.psinfo.gname \
	proc.psinfo.ttyname \
	> $tmp.out 2>&1
    code=$?
fi

echo >>$seq.full
echo "=== src/proc_test output ===" >>$seq.full
cat $tmp.out >>$seq.full

_proc_filter $tmp.out

if [ $code != 0 ]
then
    echo $0: FAILED proc_test exit status = $code
    echo $0: see $seq.full
    exit 1
fi


#
# fetch all instances for one metric in each cluster 
# if new clusters are added, add more here
#
echo ""
echo "All instances for selected metrics ..."
if [ $PCP_PLATFORM = linux ]
then
    pminfo -F 2>$tmp.err \
	proc.nprocs \
	proc.psinfo.sname \
	proc.memory.textrss \
	proc.runq.sleeping
else
    pminfo -F 2>$tmp.err \
	proc.nprocs \
	proc.psinfo.time \
	proc.pstatus.pid \
	proc.pscred.suid \
	proc.psusage.sysc
fi \
| sed \
    -e '/Error: proc_instance: unknown pid:/d' \
    -e '/^  *value /d' \
    -e '/^  *inst /d' \
    -e '/^"] value/d'

sed -e '/unknown pid/d' $tmp.err > $tmp.err1

if [ -s $tmp.err1 ]
then
	echo Following is unexpected stderr output from pminfo \| sed
	echo "--------"
	cat $tmp.err1
	echo "--------"
fi

if [ $PCP_PLATFORM = irix ]
then
    #
    # Check hp-mib test program
    # (this uses pcp_lite | PM_CONTEXT_LOCAL)
    #
    echo ""
    echo "Exercise the stuff for the SNMP HP-UX MIB ..."
    PMDA_LOCAL_PROC=yes; export PMDA_LOCAL_PROC
    $sudo src/hp-mib 2>$tmp.err.hpmib \
    | _inst_value_filter \
    | _filter_pmids \
    | sed \
	-e '/Error: proc_instance: unknown pid:/d' \
	-e '/^[ ]*value/d' \
	-e '/^[ ]*inst/d' \
	-e '/unknown pid/d' \
	-e '/numval:/s/numval: .*/numval: LOTS/' \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \

    _filter_pmcd_log <$tmp.err.hpmib \
    | sed -e '/unknown pid/d' >$tmp.err2

    if [ -s $tmp.err2 ]
    then
	    echo Following is unexpected stderr output from hp-mib \| sed
	    echo "--------"
	    cat $tmp.err2
	    echo "--------"
	    
    fi
fi

exit 0
