#! /bin/sh
# PCP QA Test No. 022
# proc PMDA exerciser
#
# Copyright (c) 2013-2014 Red Hat.
# 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

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

# see if unix domain sockets are available (permissions)
_get_libpcp_config
target="-h localhost"
$unix_domain_sockets && target="-h unix:"

rm -f $seq.out $seq.full
test -f $seq.out.$PCP_PLATFORM || _notrun "No validated output for $PCP_PLATFORM"
ln $seq.out.$PCP_PLATFORM $seq.out

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`

    echo "+++ start proc_filter $1 +++" >>$seq.full
    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 '/Error: No data available/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 's/inst \[[0-9][0-9]* or ".*/inst [NNN]/' \
	-e '/value /{
s/value [0-9][0-9]*/value INTEGER/
s/value ".*"/value STRING/
s/value \[.*\]/value BINARYDATA/
}' \
	-e '/pmResult/s/ .* numpmid/ ... numpmid/' \
    \
    | tee -a $seq.full \
    | $PCP_AWK_PROG '
    /End Fetch Over Entire Instance Domain/	{ all = 0; print; next; }
    /Fetch Over Entire Instance Domain/	{ all = 1 }
    all == 1 && / [i]nst \[/		{ skip = 1 }
    all == 1 && /] [v]alue /		{ skip = 0; next }
    all == 1 && skip == 1		{ next }
    all == 1 && $3 == "numval:"		{ $4 = "LOTS" }
					{ print }' \
    | _filter_pmids

    echo "+++ end proc_filter $1 +++" >>$seq.full
}

# 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 '/No data available/d' \
    -e '/instance identifier/d' \
    -e '/^[ \t]*$/d' \
    -e '/DISAPPEARED/d' \
    -e '/^proc\.fd\.count$/d' \
    > $tmp.info 2>&1
LC_COLLATE=POSIX sort -n $tmp.info

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 ..."
for metric in proc.nprocs proc.psinfo.sname proc.memory.textrss proc.runq.sleeping
do
    pminfo $target -F 2>$tmp.err $metric \
    | sed \
	-e '/Error: proc_instance: unknown pid:/d' \
	-e '/^  *value /d' \
    | $PCP_AWK_PROG '
/ [i]nst \[/	{ skip = 1 }
/"] [v]alue/	{ skip = 0; next }
skip == 1	{ next }
		{ print }'

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

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

exit 0
