#!/bin/sh
# PCP QA Test No. 368
#
# pmlogconf - handle changes in installed group files
#
# Copyright (c) 2014,2020 Red Hat.
# Copyright (c) 2010 Ken McDonell.  All Rights Reserved.
#

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

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

postgres=`pmprobe postgresql.stat.all_tables.seq_scan | awk '{ print $2 }'`
[ "$postgres" -ge 0 ] && _notrun "Test $seq sensitive to local postgres install"

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

# need to handle diff-c lines like this
# *** TMP.conf	2010-06-17 06:36:00.000000000 +1000
# --- /var/tmp/pcp.[a0Sg619P4/]ctl  2012-11-16 13:37:29.000000000 +0000
#
_filter()
{
    sed \
	-e "s;$tmp;TMP;g" \
	-e '/^\-\-\- TMP\.conf\.new/s/\.conf\.new.*/.conf.new .../' \
	-e '/^\*\*\* TMP\.conf/s/\.conf.*/.orig .../' \
    #end
}

cat <<End-of-File >$tmp/01
#pmlogconf-setup 2.0
probe	sample.long.ten == 10 ? include : exclude
ident	qa group one
	sample.long.ten
	sample.long.hundred
	sample.long.million
End-of-File

cat <<End-of-File >$tmp/02
#pmlogconf-setup 2.0
probe	sample.ulong.ten != 10 ? include : exclude
ident	qa group two
	sample.ulong.ten
End-of-File

cat <<End-of-File >$tmp/03
#pmlogconf-setup 2.0
probe	sample.longlong.ten >= 10 ? include : available
ident	qa group three
	sample.longlong.ten
End-of-File

cat <<End-of-File >$tmp/04
#pmlogconf-setup 2.0
probe	sample.ulonglong.ten < 10 ? include : available
ident	qa group four
	sample.ulonglong.ten
End-of-File

# real QA test starts here

pmlogconf -d $tmp $tmp.conf 2>&1 \
| _filter

echo "--- start initial config file ---"
cat $tmp.conf | _filter
echo "--- end initial config file ---"

cat <<End-of-File >$tmp/00
#pmlogconf-setup 2.0
force	include
ident	qa group zero
	sample.float.ten
End-of-File

cat <<End-of-File >$tmp/05
#pmlogconf-setup 2.0
force	available
ident	qa group five
	sample.double.ten
End-of-File

rm $tmp/03

( echo q ; echo '') | pmlogconf -d $tmp $tmp.conf 2>&1 \
| _filter

# reverse the condition
#
cat <<End-of-File >$tmp/02
#pmlogconf-setup 2.0
probe	sample.ulong.ten <= 10 ? include : exclude
ident	qa group two
	sample.ulong.ten
End-of-File

# reverse the condition
#
cat <<End-of-File >$tmp/03
#pmlogconf-setup 2.0
probe	sample.longlong.ten > 10 ? include : available
ident	qa group three
	sample.longlong.ten
End-of-File

( echo q ; echo '') | pmlogconf -r -d $tmp $tmp.conf 2>&1 \
| _filter

# drop a metric
#
cat <<End-of-File >$tmp/01
#pmlogconf-setup 2.0
probe	sample.long.ten ~ ^10$ ? include : exclude
ident	qa group one
	sample.long.ten
	sample.long.million
End-of-File

# add a metric
#
cat <<End-of-File >$tmp/02
#pmlogconf-setup 2.0
probe	sample.ulong.ten ~ 10 ? include : exclude
ident	qa group two
	sample.ulong.ten
	sample.ulong.million
End-of-File

cat <<'End-of-File' >$tmp/98
#pmlogconf-setup 2.0
ident   postgresql summary information
probe   postgresql.stat.all_tables.seq_scan ? include : available
        postgresql.stat.database
	postgresql.stat.all_tables
	postgresql.stat.all_indexes
	postgresql.statio.all_tables
	postgresql.statio.all_indexes
End-of-File

cat <<'End-of-File' >$tmp/99
#pmlogconf-setup 2.0
ident	Example from pmlogconf(1) man page
ident	... more descripton
delta	1 minute
probe	sample.secret.foo.one values ? include : exclude
	sample.secret.foo.one
	sample.secret.foo.bar	# non-leaf in the PMNS
	sample.colour [ red green ]
End-of-File

( echo q ; echo '') | pmlogconf -d $tmp $tmp.conf 2>&1 \
| _filter

echo "--- start final config file ---"
cat $tmp.conf | _filter
echo "--- end final config file ---"

# success, all done
exit
