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

seq=`basename $0`

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

pminfo proc.nprocs >/dev/null 2>&1 || _notrun "proc PMDA not installed"

# see if unix domain sockets are available (permissions)
unix_domain_sockets=false
eval `pmconfig -L 2>/dev/null`
target="-h localhost"
$unix_domain_sockets && target="-h unix:"

rm -f $seq.out $seq.full
if [ $PCP_VER -ge 3805 -a -f $seq.$PCP_PLATFORM.4 ]
then
    ln $seq.$PCP_PLATFORM.4 $seq.out
elif [ $PCP_VER -ge 3801 -a -f $seq.$PCP_PLATFORM.3 ]
then
    ln $seq.$PCP_PLATFORM.3 $seq.out
elif [ $PCP_VER -ge 2704 -a -f $seq.$PCP_PLATFORM.2 ]
then
    ln $seq.$PCP_PLATFORM.2 $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,$PCP_RUN_DIR/pmcd.socket,localhost," \
	-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 $target -F proc \
| tee -a $seq.full \
| _inst_value_filter \
| sed -e '/value/d' \
    -e '/not supported/d' \
    -e '/instance identifier/d' \
    -e '/^[ \t]*$/d' \
    -e '/DISAPPEARED/d' \
    -e '/^proc\.fd\.count$/d'

src/proc_test $target \
	proc.psinfo.utime \
	proc.psinfo.stime \
	proc.memory.rss \
	proc.memory.size \
	> $tmp.out 2>&1
code=$?

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 ..."
pminfo $target -F 2>$tmp.err \
	proc.nprocs \
	proc.psinfo.sname \
	proc.memory.textrss \
	proc.runq.sleeping \
| 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

exit 0
