#!/bin/sh
# PCP QA Test No. 1745
# Basic checkout of pmlogger double-sampling case.
#
# Copyright (c) 2020 Red Hat.  All Rights Reserved.
#

seq=`basename $0`
if [ $# -eq 0 ]
then
    echo "QA output created by $seq"
else
    echo "QA output created by $seq $*"
fi

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

[ -d $PCP_PMDAS_DIR/simple ] || _notrun "simple PMDA directory is not installed"

_cleanup()
{
    cd $here
    _restore_pmda_install simple
    $sudo rm -rf $tmp $tmp.*
}

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

# real QA test starts here
_prepare_pmda_install simple

echo "=== $iam agent installation ==="
$sudo ./Install -e </dev/null >$tmp.out 2>&1
# Check simple metrics have appeared ... X metrics and Y values
_filter_pmda_install <$tmp.out >> $here/$seq.full

echo "=== create pmlogger archive ==="
cat << End-of-File > $tmp.conf
log advisory on default {
	kernel.all.pswitch
	sample
	simple.color
	kernel.all.pswitch
}
End-of-File
pmlogger -t 1 -T6 -c $tmp.conf -l $tmp.log $tmp.archive
cat $tmp.log >> $here/$seq.full
pmdumplog -a $tmp.archive >> $here/$seq.full

echo "=== check archive contents ==="
_filter_pmval()
{
    sed \
	-e '/^$/d' \
	-e '/^metric:/d' \
	-e '/^archive:/d' \
	-e '/^host:/d' \
	-e '/^start:/d' \
	-e '/^end:/d' \
	-e '/^semantics:/d' \
	-e '/^units:/d' \
	-e '/^samples:/d' \
    #end
}
pmval -U $tmp.archive kernel.all.pswitch \
	| tee -a $here/$seq.full \
	| _filter_pmval > $tmp.values
lines=`wc -l < $tmp.values`
if [ $lines -ge 4 -a $lines -le 7 ]
then
    echo "Raw sample count in expected range"
else
    echo "Bad sample count: $lines"
    cat $tmp.values
    status=1
fi

exit
