#!/bin/sh
# PCP QA Test No. 827
# Check log rewriting of Linux kernel PMDA metric metadata
#
# Copyright (c) 2013, 2015, 2016 Red Hat.
#

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

seq=`basename $0`

case $PCP_PLATFORM
in
    linux)
	;;
    *)
	# for other platforms, the Linux PMDA logrewrite rules
	# may not be present ...
	#
	for tree in snmp tcp
	do
	    [ -f "$PCP_VAR_DIR/config/pmlogrewrite/linux_proc_net_${tree}_migrate.conf" ] || _notrun "linux_proc_net_${tree}_migrate.conf: pmlogrewrite rules not installed"
	done
	;;
esac

echo "QA output created by $seq"

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

# real QA test starts here
cat >$tmp.config_snmp <<End-of-File
metric 60.14.* { type -> U32 }
End-of-File

cat >$tmp.config_tcp <<End-of-File
metric network.tcp.rtoalgorithm { sem -> COUNTER units -> 0,0,1,0,0,1 }
metric network.tcp.rtomin { sem -> COUNTER units -> 0,0,1,0,0,1 }
metric network.tcp.rtomax { sem -> COUNTER units -> 0,0,1,0,0,1 }
metric network.tcp.maxconn { sem -> COUNTER units -> 0,0,1,0,0,1 }
metric network.tcp.currestab { sem -> COUNTER units -> 0,0,1,0,0,1 }
End-of-File

for tree in snmp tcp
do
    echo
    echo "=== checking Linux kernel $tree metric rewriting ==="
    echo

    pmlogrewrite -c $tmp.config_$tree -w archives/new_$tree $tmp.old
    pmlogrewrite -c $PCP_VAR_DIR/config/pmlogrewrite/linux_proc_net_${tree}_migrate.conf -w $tmp.old $tmp.new

    pmdumplog -z -dilmst $tmp.old >$tmp.old.dump
    pmdumplog -z -dilmst $tmp.new >$tmp.new.dump
    pmdumplog -z -dilmst archives/new_$tree >$tmp.ref.dump

    echo "old -> new changes"
    diff -u $tmp.old.dump $tmp.new.dump | sed -e '/^---/d' -e '/^+++/d'

    # Note:
    # 	Turns out pmlogrewrite and pmlogger don't quite agree on when
    # 	to update the temporal index for the prologue record, so there
    # 	may be a small diff here in the metadata offset in the temporal
    # 	index ... the differences have been analyzed and are believed to
    # 	be benign.
    #
    echo
    echo "new -> ref changes ... expect at most, metadata offset change for temporal index"
    diff -u $tmp.ref.dump $tmp.new.dump | sed -e '/^---/d' -e '/^+++/d'

    rm $tmp.old.* $tmp.new.* $tmp.ref.*
done

# success, all done
status=0
exit
