#!/bin/sh
# PCP QA Test No. 481
# check pmlogrewrite config parser - global and indom clauses
#
# Copyright (c) 2011 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

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

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

_filter()
{
    sed \
	-e "s;$tmp;TMP;g"
}

# real QA test starts here
cat <<End-of-File >$tmp.conf
global {
    hostname -> whizz-bang.engr.sgi.com
    TZ -> "GMT+10"
    Time -> 10
}

indom 1.5 {
    indom -> 1.10
    iname "15 minute" -> "forever"
    inst 15 -> 9999
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

for t in +3 +3.5 +3. +03 4 4.000005 4. 04 -5 -5.005 -5. -05 \
	+1:00.123456 +2:00 3:00. 04:00 -5:12.345 -6:30 \
	+23:59:59 -0:0:1 +12:34:56.789012
do
    echo
    echo "=== global time -> $t ==="
    echo "global { time -> $t }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

for h in abc node123.localdomain foo-bar 123 123.456 123.456foobar \
	100foo 100.foo 10.0.foo
do
    echo
    echo "=== global hostname -> $h ==="
    echo "global { hostname -> $h }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

for t in 60 123:45 123:0:61.678901
do
    echo
    echo "=== global time -> $t (expect warning) ==="
    echo "global { time -> $t }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

echo
echo "=== global dup hostname clause (expect error) ==="
cat <<End-of-File >$tmp.conf
global {
    hostname -> whizz-bang.engr.sgi.com
    tz -> "GMT+10"
    time -> 10
    hostname -> bogus.com
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== global dup tz clause (expect error) ==="
cat <<End-of-File >$tmp.conf
global {
    hostname -> whizz-bang.engr.sgi.com
    tz -> "GMT+10"
    tz -> "Bogus"
    time -> 10
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== global dup time clause (expect error) ==="
cat <<End-of-File >$tmp.conf
global {
    hostname -> whizz-bang.engr.sgi.com
    tz -> "GMT+10"
    time -> 10
    time -> 11
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== indom dup indom clause (expect error) ==="
cat <<End-of-File >$tmp.conf
indom 1.5 {
    indom -> 1.100
    inst 15 -> 9999
    indom -> 1.200
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== indom dup name clause (expect error) ==="
cat <<End-of-File >$tmp.conf
indom 1.5 {
    inst 1 -> 9999
    iname "1 minute" -> "foo"
    iname "1 minute" -> "bar"
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== indom dup inst clause (expect error) ==="
cat <<End-of-File >$tmp.conf
indom 1.5 {
    inst 1 -> 9999
    inst 1 -> 9999
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

# indom ... { indom ... } checks
#
# 4194304 is 2^22, 512 is 2^9 ... exceed serial and domain fields of a pmInDom
# 513.4194310 => 1.5 if field range checking not done!
for i in 1.5 '' 1.6 1.2.3 foo 1.4194304 512.1 513.4194310 1.*
do
    echo
    echo "=== indom $i { } ==="
    echo "indom $i { }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -w -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
    echo
    echo "=== indom 1.5 { indom -> $i } ==="
    echo "indom 1.5 { indom -> $i }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -w -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

# indom ... { iname | inst ... } exercises
#
for i in 'iname "15" -> "15 mid range"' 'inst 1 -> 3' \
    'inst 5 -> delete' 'iname "5" -> delete' \
    'inst 1 -> 3 iname "1" -> "3 minute"' \
    'iname "15" ->' 'iname "15" -' 'iname "15"'
do
    echo
    echo "=== indom 1.5 { $i } ==="
    echo "indom 1.5 { $i }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -w -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

# indom ... { iname | inst ... } checks
#
for i in 'iname "15" -> "16" iname "5" -> "6" iname "15" -> delete' \
	'iname "5" -> "5 minute"' 'inst 1 -> 1' \
	'iname "15" -> delete iname "5" -> "6" iname "15" -> "deleted"' \
	'inst 5 -> 6 inst 1 -> 2 inst 5 -> delete' \
	'inst 5 -> delete inst 1 -> 2 inst 5 -> 123'\
	'iname "15" -> "16" inst 15 -> delete' \
	'inst 15 -> delete iname "15" -> "16"' \
	'inst 15 -> 16 iname "15" -> delete' \
	'iname "15" -> delete inst 15 -> 16'
do
    echo
    echo "=== indom 1.5 { $i } (expect error) ==="
    echo "indom 1.5 { $i }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -w -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

# instance iname upto space checks
for i in "5 minutes" 1 5 15 "1 does not matter" "15 eek"
do
    echo
    echo "=== indom 1.5 { iname \"$i\" -> \"yippee\" } ==="
    echo "indom 1.5 { iname \"$i\" -> \"yippee\" }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done
for i in 19856 "19856 does not matter" 19857
do
    echo
    echo "=== indom 2.1 { iname \"$i\" -> \"19856 bozo\" } ==="
    echo "indom 2.1 { iname \"$i\" -> \"19856 bozo\" }" >$tmp.conf
    rm -f $tmp.new.*
    pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
done

echo
echo "=== context sensitive lexical scanning ... expect pass ==="
cat <<End-of-File >$tmp.conf
global {
    hostname -> global
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
global {
    hostname -> inst
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
global {
    hostname -> hostname
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
global {
    hostname -> tz
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
metric 1.18.3 {
    name -> hostname
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
metric 1.18.3 {
    name -> indom
}
End-of-File
rm -f $tmp.new.*

echo
echo "=== context sensitive lexical scanning ... expect fail ==="
cat <<End-of-File >$tmp.conf
metric 1.18.3 {
    name -> pmid
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
metric 1.18.3 {
    name -> units
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

echo
echo "=== new conditional if in type clause (expect errors) ==="
cat <<End-of-File >$tmp.conf
metric irix.kernel.all.load {
    type if 32 ->
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter
cat <<End-of-File >$tmp.conf
metric irix.kernel.all.load {
    type if
}
End-of-File
rm -f $tmp.new.*
pmlogrewrite -C -c $tmp.conf archives/src-rattle $tmp.new 2>&1 | _filter

# success, all done
exit
