#! /bin/sh
# PCP QA Test No. 232
# exercise instance API functions for PM_CONTEXT_LOCAL
#
# 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

_filter_dodgey_nfs_indom()
{
    sed \
	-e '/number of instances .* (0) != that for pmGetInDom (18)/d' \
	-e '/number of instances .* (0) != that for pmGetInDom (22)/d' \
	-e '/number of instances .* (0) != that for pmGetInDom (53)/d' \
	-e '/number of instances .* (0) != that for pmGetInDom (59)/d'
}

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

# real QA test starts here
PMDA_LOCAL_SAMPLE=yes
export PMDA_LOCAL_SAMPLE

cat <<End-of-File >$tmp.metrics
#InDom	Name
 60.00	hinv.map.cpu_num
 60.00	kernel.percpu.cpu.idle
 60.01	disk.dev.read
 60.02	kernel.all.load
 60.03	network.interface.in.bytes
# may fail due to removeable device like DVD or CD-ROM ... in instance
# domain, but not until probed in fetch method do we discover there are
# no filesys.* metrics for that instance
#fail ok 60.05	filesys.used
 60.06	swapdev.length
 60.07	nfs.client.reqs
 60.07	nfs.server.reqs
 60.08	nfs3.client.reqs
 60.08	nfs3.server.reqs
 60.10	disk.partitions.total
 60.11	hinv.map.scsi
 60.12	mem.slabinfo.objects.total
 60.14	nfs4.client.reqs
 60.15	nfs4.server.reqs
# don't use network.interface.inet_addr as this may fail because indom
# comes from /proc/net/dev but some of these are skipped in the fetch if
# we can't get the inet addr
 60.17	network.interface.total.bytes
 60.18	tmpfs.capacity
 60.19	kernel.pernode.cpu.nice
# will fail because indom is hard-wired at 256 elements, but fetch may
# return a subset of these
#fail ok 60.23	network.icmpmsg.intype
 60.24	hinv.map.dmname
 30.01	sampledso.colour
 30.02	sampledso.bucket
 30.03	sampledso.mirage
 30.04	sampledso.needprofile
 30.05	sampledso.hordes.one
 30.06	sampledso.dodgey.value
 30.07	sampledso.dynamic.counter
 30.08	sampledso.many.int
# the indom is re-evaluated for each pdu request ...
#fail ok 30.09	sampledso.scramble.bin
 30.10	sampledso.event.records
# this will always end in tears ...
#fail ok 02.03	sampledso.secret.foo.bar.max.redirect
End-of-File

sed -e '/^#/d' <$tmp.metrics | while read indom metric
do
    echo $metric:
    if [ $PCP_PLATFORM = darwin ]
    then
	# some of the metrics just aren't there on Mac OS X
	case $metric
	in
	    swapdev.length|nfs.client.reqs|nfs.server.reqs)
		continue
		;;
	esac
    elif [ $PCP_PLATFORM = solaris ]
    then
	# some of the metrics just aren't there on Solaris, or have
	# equivalents with different names
	case $metric
	in
	    swapdev.length|nfs.*|nfs3.*)
		continue
		;;
	    filesys.used)
		metric=zfs.used.total
		;;
	esac
    elif [ $PCP_PLATFORM = freebsd ]
    then
	# some of the metrics just aren't there on FreeBSD
	case $metric
	in
	    hinv.map.cpu_num|hinv.map.dmname|disk.partitions.total|hinv.map.scsi|mem.slabinfo.objects.total|filesys.used|swapdev.length|nfs.*|nfs3.*|nfs4.*|tmpfs.capacity|kernel.pernode.cpu.nice)
		continue
		;;
	esac
    fi
    if [ $PCP_PLATFORM = solaris -o $PCP_PLATFORM = freebsd ]
    then
	# need to be root to open /dev/kmem
	$sudo $here/src/torture_indom $metric >$tmp.out 2>&1
	status=$?
    else
	$here/src/torture_indom $metric >$tmp.out 2>&1
	status=$?
    fi
    sed <$tmp.out \
	-e '/using .* kmem interface/d' \
    | _filter_pmcd_log \
    | _filter_dodgey_nfs_indom
    if [ $status != 0 ]
    then
	case "$metric"
	in
	    nfs*reqs)
	    	;;
	    *)
		echo torture_indom failed for $metric
		;;
	esac
    fi
done

# all done
exit
