#!/bin/sh
# PCP QA Test No. 1727
# Test duplicate instname labels in /metrics webapi when a context
# level label such as "hostname" is explicitly specified.
# Test invalid OpenMetrics metric names also.
#
# Copyright (c) 2021 Red Hat.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

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

_check_series   # ensure pmproxy makes a REST API available
_pmdaopenmetrics_check || _notrun "openmetrics pmda not installed"
which curl >/dev/null 2>&1 || _notrun curl not installed

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

# only stop pmproxy if it was not running before the QA test starts
if [ -n "`_get_pids_by_name pmproxy`" ]
then
    pmproxy_was_running=true
else
    pmproxy_was_running=false
    _service pmproxy start >>$seq.full 2>&1
fi
echo "pmproxy_was_running=$pmproxy_was_running" >>$here/$seq.full

_cleanup()
{
    cd $here
    if $need_restore
    then
        $sudo rm -rf $PCP_ETC_DIR/pcp/labels/*
        _restore_config $PCP_ETC_DIR/pcp/labels
	$pmproxy_was_running || _service pmproxy stop >>$seq.full 2>&1
        need_restore=false
    fi
    _pmdaopenmetrics_cleanup
    $sudo rm -rf $tmp $tmp.*
}

_filter_openmetrics_labels()
{
    sed \
    -e "s;$PCP_PMDAS_DIR;PCP_PMDAS_DIR;g" \
    -e 's;machineid="[a-z0-9]*";machineid=MACHINEID;g' \
    -e 's;hostname="[a-zA-Z0-9_\.\-]*";hostname=HOSTNAME;g' \
    -e 's;hostname:[a-zA-Z0-9_\.\-]*";hostname:HOSTNAME";g' \
    -e 's;domainname="[a-zA-Z0-9_\.\-]*";domainname=DOMAINNAME;g' \
    -e 's;144\.[0-9]*\.[0-9]*;NUMERIC_PMID;g' \
    -e 's;144\.[0-9]*;NUMERIC_INDOM;g' \
    # end
}

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

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

# set up a scripted config
MYHOST=`hostname`
cat <<EOF >$tmp.script
#! /bin/bash

echo '# HELP somemetric metric to test duplicate instname labels'
echo '# TYPE somemetric gauge'
echo 'somemetric{hostname="$MYHOST"} 1234'
EOF

cat <<EOF >$tmp.badxml
#! /bin/bash

cat $here/sadist/891688-dash-time.xml
echo '# TYPE somemetric gauge'
echo 'somemetric{hostname="$MYHOST"} 1234'
EOF

chmod 755 $tmp.script $tmp.badxml
$sudo mv $tmp.script $PCP_PMDAS_DIR/openmetrics/config.d/duplicate_instname_label.sh
$sudo mv $tmp.badxml $PCP_PMDAS_DIR/openmetrics/config.d/invalid_metrics_badinput.sh

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

echo; echo === /metrics webapi listing. The instname label should appear only once.
curl -Gs 'http://localhost:44322/metrics?names=openmetrics.duplicate_instname_label.somemetric' \
| _filter_openmetrics_labels

echo; echo === verify metric name validity using pminfo
pminfo -v openmetrics

# squash errors for a clean uninstall
$sudo rm $PCP_PMDAS_DIR/openmetrics/config.d/invalid_metrics_badinput.sh
# capture openmetrics log for posterity
cat $PCP_LOG_DIR/pmcd/openmetrics.log >> $here/$seq.full

_pmdaopenmetrics_remove

# success, all done
status=0
exit
