#!/bin/sh
# PCP QA Test No. 1306
# Test pmdaopenmetrics label filtering, including optional labels
#
# Copyright (c) 2018 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

# get standard environment, filters and checks
. ./common.openmetrics

_pmdaopenmetrics_check || _notrun "openmetrics pmda and/or load generator not installed"
_have_python266 && _notrun "python load generator does not currently work with python 2.6.6"

status=1	# failure is the default!
$sudo rm -rf $tmp $tmp.* $seq.full

_cleanup()
{
    cd $here
    $sudo rm -rf $PCP_ETC_DIR/pcp/labels/*
    _restore_config $PCP_ETC_DIR/pcp/labels
    _sighup_pmcd
    _pmdaopenmetrics_cleanup
    $sudo rm -rf $tmp $tmp.*
}

_prepare_pmda openmetrics
trap "_cleanup; exit \$status" 0 1 2 3 15
_stop_auto_restart pmcd

_filter()
{
    # Filter a bunch of labels that we dont want for QA purposes
    sed \
    -e 's/"domainname":"[^"]*"//' \
    -e 's/"groupid":[0-9]*//' \
    -e 's/"userid":[0-9]*//' \
    -e 's/"hostname":"[^"]*"//' \
    -e 's/"machineid":"[^"]*"//' \
    -e 's/"url":"[^"]*"//' \
    -e 's/,[,]*/,/g' -e 's/,}/}/'
}

# real QA test starts here
_pmdaopenmetrics_save_config
_save_config $PCP_ETC_DIR/pcp/labels
$sudo rm -rf $PCP_ETC_DIR/pcp/labels/*

# add the URL for this test
file=$here/openmetrics/samples/labelfiltering.txt
urlbase=`basename $file .txt | tr .- _`
echo 'file://'$file > $PCP_PMDAS_DIR/openmetrics/config.d/$urlbase.url

# add a bunch of label filters to exercise various filtering options
cat <<EOF >>$PCP_PMDAS_DIR/openmetrics/config.d/$urlbase.url

# Filter to remove the "uninteresting" label
FILTER: EXCLUDE LABEL uninteresting

# Filter to remove all labels in metric2, thus it becomes singular
FILTER: EXCLUDE LABEL removeme

# Filter to exclude label "def" hence metric3 has duplicate instances
FILTER: EXCLUDE LABEL def

#
# *** NOTE: optional labels are not implemented yet
# Filter to flag some_optional_label as optional in metric4
# (an optional label is not present in instance name, but still included as a label for the metric)
# FILTER: OPTIONAL LABEL some_optional_label

# implicit final rule includes all labels not matching anything above
# (this implicit rule is listed here just for testing purposes)
FILTER: INCLUDE .*

EOF

_pmdaopenmetrics_install

if ! _pmdaopenmetrics_wait_for_metric openmetrics.control.calls
then
    status=1
    exit
fi

echo; echo === URL configuration file contains
sed -e "s|$here|QA_DIR|g" < $PCP_PMDAS_DIR/openmetrics/config.d/$urlbase.url

echo; echo === pminfo listing. Note some labels in the pminfo listing have been removed, see _filter
names=`pminfo openmetrics | fgrep -v openmetrics.control | LC_COLLATE=POSIX sort` 2>$tmp.err
if [ -z "$names" ]; then
    echo "FAILED - no openmetrics metrics to show"
else
    if pminfo -fl `pminfo openmetrics | fgrep -v openmetrics.control | LC_COLLATE=POSIX sort` > $tmp.info 2>$tmp.err
    then
	:
    else
	echo "... failed! ... here is the Install log ..."
	cat $tmp.out
    fi
    cat $tmp.info $tmp.err | _filter
fi

echo == Note: check $seq.full for details
echo == pmdaopenmetrics LOG == >>$here/$seq.full
cat $PCP_LOG_DIR/pmcd/openmetrics.log >>$here/$seq.full

_pmdaopenmetrics_remove

# success, all done
status=0
exit
