#! /bin/sh
# PCP QA Test No. 390
# Test out the proc agents handling of 
# mapping external instance names to internal instance names (pids).
# It was truncating the first number.
# i.e. if one does a "pmval -i "12345678" proc.psinfo.pid
#      it might return a value of "12345" on a 6.2 system
#
# This was written when qa/044 failed because it looked
# for what it thought was an impossible instance id but
# it wasn't because it was truncated.  
#
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`

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

[ $PCP_PLATFORM = darwin ] && _notrun "No proc metrics for darwin"
[ $PCP_PLATFORM = solaris ] && _notrun "No proc metrics for solaris"

# proc metrics may not be available
#
if pminfo proc.nprocs >/dev/null 2>&1
then
    :
else
    _notrun proc PMDA not installed
fi

echo "QA output created by $seq"

status=1	# failure is the default!
trap "rm -f $tmp.*; exit \$status" 0 1 2 3 15

#debug=true
debug=false

_filter()
{
   sed -e "s/0*$mypid/PID/" -e 's/^[ \t]*//'

}

# real QA test starts here

mypid=$$
$debug && echo "mypid = $mypid"

pid_len=`ls /proc | head -1 | wc -c`
pid_len=`expr $pid_len - 1`
$debug && echo "pid_len = $pid_len"

mypid_len=`echo $mypid | wc -c`
mypid_len=`expr $mypid_len - 1`
$debug && echo "mypid_len = $mypid_len"

gap=`expr $pid_len - $mypid_len`
$debug && echo "gap = $gap"

# create a zeroes string for the leading zeroes
# to fill up a pid as seen in /proc
zeroes=""
i=0
while [ $i -lt $gap ]
do
    zeroes="0$zeroes"
    i=`expr $i + 1`
done

# should all succeed
pmval -w 30 -s1 -i "$mypid" proc.psinfo.pid | _filter
pmval -w 30 -s1 -i "${zeroes}$mypid" proc.psinfo.pid | _filter
pmval -w 30 -s1 -i "${zeroes}${mypid}hi" proc.psinfo.pid | _filter

# should now fail - passed previously
pmval -w 30 -s1 -i "${zeroes}${mypid}12345" proc.psinfo.pid | _filter

# success, all done
status=0
exit
