#!/bin/sh
# PCP QA Test No. 1069
# Exercise pmrep configuration and various reporting modes.
#
# Copyright (c) 2015-2016 Red Hat.
#

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

. ./common.python

$python -c "from pcp import pmapi" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmapi module not installed"
$python -c "from collections import OrderedDict" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python collections OrderedDict module not installed"

which pmrep >/dev/null 2>&1 || _notrun "pmrep not installed"

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

_live_filter()
{
    sed \
	-e "s/[0-9][0-9]:[0-9][0-9]:[0-9][0-9]/HH:MM:SS/g" \
    #end
}

_path_filter()
{
    sed \
	-e "s#$here#QAPATH#g" \
    #end
}

#  timezone: :Australia/Melbourne (creation, current is AEDT-11)
#  this is not deterministic .......................... ^^^^^^^
#  and
#  timezone: AEDT-11
#            ^^^^ ... ditto
#
#  we also need to filter for AEST-10 during standard timezone
#  as it appears that pmrep will report the change in -11 to -10
_archive_filter()
{
    sed \
	-e '/timezone/s/current is .*)/current is ...)/' \
        -e '/timezone/s/ (reporting.*)//' \
	-e '/timezone:/s/[A-Z][A-Z]*-11/...-11/' \
	-e '/timezone:/s/[A-Z][A-Z]*-10/...-11/' \
    #end
}

log="--archive $here/archives/sample-secs -z"
log2="--archive $here/archives/20130706 -z -O 30m"

# real QA test starts here
echo "== basic live mode reporting"
pmrep -p -s 2 -t 0.25 sample.long.one | _live_filter
pmrep -p -T 2sec -t 1 sample.long.one | _live_filter
pmrep -p -T 2sec -t 0.8 sample.long.one | _live_filter

echo "== basic archive mode reporting"
pmrep -z -p -s 5 -t 2 -P 2 --archive $here/archives/sample-secs \
	sample.seconds sample.milliseconds

echo "== basic configuration file handling"
echo > $tmp.config <<EOF
[options]
version = 1
source = www.example.com
samples = 1
interval = 1
EOF
pmrep -s 5 -t 2 $log -c $tmp.config sample

echo "== exercise various output options"
pmrep -s 5 -t 2 $log -c $tmp.config -o stdout -p -w 20 sample
echo "---"
pmrep -s 5 -t 2 $log -c $tmp.config -C -x sample | _path_filter \
| _archive_filter
echo "---"
pmrep -s 5 -t 2 $log -c $tmp.config -G -H -U sample

echo "== exercise raw counter mode"
pmrep -s 5 -t 2 $log -c $tmp.config -r sample

echo "== exercise timezone setting"
pmrep -s 5 -t 2 --archive $here/archives/sample-secs -p -Z UTC sample

echo "== exercise CSV and alternate delimiters"
pmrep -s 5 -t 2 $log -c $tmp.config -o csv -p -P 1 sample
pmrep -s 5 -t 2 $log -c $tmp.config -o csv --delimiter '|' sample | \
    tee -a $tmp.out1
echo "== compare output-to-file versus stdout"
pmrep -s 5 -t 2 $log -c $tmp.config -o csv --delimiter '|' -F $tmp.out2 sample
diff $tmp.out1 $tmp.out2
test $? -eq 0 && echo ok

echo "== exercise repeated headers option"
pmrep -s 5 -t 2 $log -E 2 sample.seconds

echo "== exercise units conversion options"
pmrep -s 5 -t 2 $log2 -p -b MB -P 0 mem.util.free mem.util.used
pmrep -s 5 -t 2 $log2 -p -q "count x 10^-2" -P 0 -w 15 disk.dev.read
pmrep -s 5 -t 2 $log  -p -y microsec sample

echo "== derived metrics configuration file"
cat > $tmp.derived <<EOF
sample.combo = sample.seconds + (sample.milliseconds / 1000)
EOF
pmrep -s 5 -t 2 $log -e $tmp.derived sample.combo

echo "== derived metrics directly on command line"
combo="sample.combo = sample.seconds + (sample.milliseconds / 1000)"
pmrep -s 5 -t 2 $log -e "$combo" sample.combo

echo "== extended existing sample configuration"
cp $tmp.config $tmp.dconfig
cat >> $tmp.dconfig <<EOF
[global]
combo = sample.combo
EOF
pmrep -s 5 -t 2 $log -e $tmp.derived -c $tmp.dconfig sample

echo "== exercise good config version"
echo '[options]' > $tmp.vconfig
echo 'version = 1' >> $tmp.vconfig
cat $tmp.config >> $tmp.vconfig 
pmrep -s 5 -t 2 $log -c $tmp.vconfig sample
echo "== exercise bad config version"
echo '[options]' > $tmp.vconfig
echo 'version = 99' >> $tmp.vconfig
cat $tmp.config >> $tmp.vconfig
pmrep -s 5 -t 2 $log -c $tmp.vconfig sample

echo "== un/interpolated archive mode reporting"
pmrep -s 5 -t 2 $log2 -p    mem.util.free
pmrep -s 5 -t 2 $log2 -p -u mem.util.free

echo "== basic string value reporting"
# sets variable named $pcp_version
eval `pmprobe -v pmcd.version | sed -e 's/ 1 /=/g' -e 's/pmcd./pcp_/g'`
pmrep -s 1 pmcd.version | sed -e "s/$pcp_version/PCP_VERSION/g"

echo "== divide-by-zero error handling"
pmrep -t .01 -s 1 -e 'fail = sample.long.one / 0' fail

# success, all done
echo "== done"
status=0
exit
