#!/bin/sh
# PCP QA Test No. 525
# Units from summary botched after avg_host added to metric declaration.
#
# From a report by Jun Wang, junwang123@gmail.com
#
# Copyright (c) 2013 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2015 Red Hat.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.product
. ./common.filter
. ./common.check

[ -f $PCP_PMDAS_DIR/summary/pmdasummary ] || _notrun "summary pmda not installed"

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

# check if summary PMDA already installed
#
eval `pmprobe summary 2>&1 | awk '
BEGIN	{ sts = "false" }
$2 > 0	{ sts = "true"; exit }
END	{ print "reinstall=" sts }'`

_cleanup()
{
    cd $PCP_VAR_DIR/pmdas/summary
    for file in pmns expr.pmie help
    do
	[ -f $file.$seq ] && $sudo mv $file.$seq $file
    done
    if $reinstall
    then
	$sudo ./Install </dev/null >>$here/$seq.full 2>&1
    else
	$sudo ./Remove >>$here/$seq.full 2>&1
    fi
    cd $here
    rm -rf $tmp.*
}

cat >$tmp.pmns <<End-of-File
/* for QA $seq */
summary {
    mem		SYSSUMMARY:0:0
    avg_mem	SYSSUMMARY:0:1
    qa
}
summary.qa {
    t01		SYSSUMMARY:1:1
    t02		SYSSUMMARY:1:2
    t03		SYSSUMMARY:1:3
    t04		SYSSUMMARY:1:4
    t05		SYSSUMMARY:1:5
    t06		SYSSUMMARY:1:6
}
End-of-File

cat >$tmp.expr.pmie <<End-of-File
summary.mem = sample.kbyte_ctr;
summary.avg_mem = avg_host sample.kbyte_ctr :'localhost' :'127.0.0.1';
// now some extra tests after pmie has been fixed
summary.qa.t01 = sample.kbyte_ctr + 1;
summary.qa.t02 = sample.kbyte_ctr + 1 Kbyte / sec;
summary.qa.t03 = some_inst sample.long.bin_ctr > 0;
summary.qa.t04 = all_inst sample.long.bin_ctr > 0;
summary.qa.t05 = min_inst sample.long.bin_ctr;
summary.qa.t06 = max_inst sample.long.bin_ctr;
End-of-File

echo >$tmp.help

cd $PCP_VAR_DIR/pmdas/summary

for file in pmns expr.pmie help
do
    $sudo mv $file $file.$seq
    $sudo mv $tmp.$file $file
done
$sudo ./Install </dev/null | _filter_pmda_install

# real QA test starts here
pminfo -d summary | tee -a $here/$seq.full
pminfo -f summary >>$here/$seq.full

cat $PCP_LOG_DIR/pmcd/summary.log >>$here/$seq.full

echo >>$here/$seq.full
echo "--- pmie directly ---" >>$here/$seq.full
pmie -v -t 1 -T +2 -c expr.pmie >>$here/$seq.full 2>&1

# success, all done
status=0

exit
