#! /bin/sh
# PCP QA Test No. 187
# for bug #327288 - $PCP_RC_DIR/pcp blows away $PCP_TMP_DIR/pmlogger entries
#
# Note ... this will be a "sometimes" failure ... requires special
# combinations of pids such that sort != sort -n and comm gets confused
#  $PCP_RC_DIR/pcp
#
# 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

_cleanup()
{
    _restore_auto_restart pmcd
    $sudo rm -f $tmp.*
}

sts=0
trap "_cleanup; exit \$sts" 0 1 2 3 15
_stop_auto_restart pmcd

signal=$PCP_BINADM_DIR/pmsignal

rm -f $seq.full

_burn_pids()
{
    # burn off 1024 pids
    #
    for i in 1 2 3 4 5 6 7 8
    do
	for j in 1 2 3 4 5 6 7 8
	do
	    for k in 1 2 3 4 5 6 7 8
	    do
		for l in 1 2
		do
		    true
		done
	    done
	done
    done
}

_check()
{
    ( echo "this" $this_pid;
      echo "other" $other_pid;
      echo "other-fqdn" $otherfqdn_pid; ) \
    | while read tag pid
    do
	$PCP_ECHO_PROG $PCP_ECHO_N "map file for $tag host""$PCP_ECHO_C"
	if [ -f $PCP_TMP_DIR/pmlogger/$pid ]
	then
	    $PCP_ECHO_PROG " present"
	else
	    $PCP_ECHO_PROG $PCP_ECHO_N " missing ... ""$PCP_ECHO_C"

	    if [ "$tag" = this ]
	    then
		$PCP_ECHO_PROG "expected for this host when PMCD killed"
	    else
		$PCP_ECHO_PROG "NOT EXPECTED"
		echo "pid=$pid, $PCP_TMP_DIR/pmlogger contains"
		ls -l $PCP_TMP_DIR/pmlogger
		echo "pmlogger processes ..."
		ps $PCP_PS_ALL_FLAGS | grep pmlogger
		echo "pmlogger log file for missing one ..."
		cat $tmp.$pid
	    fi
	fi
    done
}

otherhost=`./getpmcdhosts -L -n 1`
[ -z "$otherhost" ] && _notrun "Cannot find a remote host running pmcd"
echo "otherhost=$otherhost" >>$seq.full
otherip=`_host_to_ipaddr $otherhost`
if [ -z "$otherip" ]
then
    echo "Arrgh ... cannot get IP addr for otherhost=$otherhost"
    sts=1
    exit
fi
echo "otherip=$otherip" >>$seq.full

# real QA test starts here
_start_up_pmlogger -L -c /dev/null -l $tmp.log1 -s1 $tmp.this >/dev/null 2>&1
this_pid=$pid
echo "this logger pid=$this_pid" >>$seq.full
_wait_for_pmlogger $this_pid $tmp.log1
$sudo mv $tmp.log1 $tmp.$this_pid

trap "$sudo $signal $this_pid $other_pid $otherfqdn_pid >/dev/null 2>&1; wait; $sudo rm -fr $tmp.*; exit \$sts" 0 1 2 3 15

_burn_pids

_start_up_pmlogger -h $otherhost -L -c /dev/null -l $tmp.log2 -s1 $tmp.other >/dev/null 2>&1
other_pid=$pid
echo "other host=$otherhost logger pid=$other_pid" >>$seq.full
_wait_for_pmlogger $other_pid $tmp.log2
$sudo mv $tmp.log2 $tmp.$other_pid

_burn_pids

_start_up_pmlogger -h $otherip -L -c /dev/null -l $tmp.log3 -s1 $tmp.other.fqdn >/dev/null 2>&1
otherfqdn_pid=$pid
echo "other fqdnhost=$otherip logger pid=$otherfqdn_pid" >>$seq.full
_wait_for_pmlogger $otherfqdn_pid $tmp.log3
$sudo mv $tmp.log3 $tmp.$otherfqdn_pid

# check entries
_check

_service pcp restart 2>&1 | _filter_pcp_start
_wait_for_pmcd
_wait_for_pmlogger

# check again
_check

echo >>$seq.full
echo "=== this log ===" >>$seq.full
cat $tmp.$this_pid >>$seq.full


echo >>$seq.full
echo "=== other log ===" >>$seq.full
cat $tmp.$other_pid >>$seq.full


echo >>$seq.full
echo "=== otherfqdn log ===" >>$seq.full
cat $tmp.$otherfqdn_pid >>$seq.full

