#!/bin/sh
# PCP QA Test No. 1067
# Exercise the zbxpcp Zabbix integration module.
#
# Copyright (c) 2015 Red Hat.
#

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

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

which zabbix_agentd >/dev/null 2>&1 || \
	_notrun "No zabbix_agentd binary found"
test -f /etc/zabbix/zabbix_agentd.conf || \
	_notrun "No configuration for the Zabbix agent daemon"
grep -q zbxpcp /etc/zabbix/zabbix_agentd.conf || \
	_notrun "No configuration of the Zabbix agent PCP module"

status=1	# failure is the default!
need_control=false
need_derived=false
control=$PCP_PMDAS_DIR/sample/dynamic.indom
derived=/etc/zabbix/zbxpcp-derived-metrics.conf
$sudo rm -rf $tmp $tmp.* $seq.full

_cleanup()
{
    $need_control && _restore_config $control
    $need_derived && _restore_config $derived
    rm -f $tmp.*
}
trap "_cleanup; exit \$status" 0 1 2 3 15
if [ -f $control ]
then
    need_control=true
    _save_config $control
fi
if [ -f $derived ]
then
    need_derived=true
    _save_config $derived
fi

_filter_values()
{
    # expect lines like:
# pcp.sample.dodgey.control    [u|5]
# pcp.sample.dodgey.value[d1]  [u|35]
# pcp.sample.bad.novalues      [m|ZBX_NOTSUPPORTED] [No value available.]
    # d|u|s -> zabbix types;
    # m is error encodings -> allow through

    sed \
	-e 's/\[[dus]|.*\]$/VALUE/g' \
	-e '/sample\.scramble/d' \
    #end
}

# real QA test starts here
echo "== request specific values"

# verify values for single metrics/instances
zabbix_agentd -t pcp.sample.long.one
zabbix_agentd -t 'pcp.sample.long.bin[bin-100]'
zabbix_agentd -t pcp.sample.ulonglong.million
zabbix_agentd -t pcp.sample.float.hundred
zabbix_agentd -t pcp.sample.double.ten

# string metrics
zabbix_agentd -t pcp.sample.string.null
zabbix_agentd -t pcp.sample.string.hullo

# aggregate/event metrics
zabbix_agentd -t pcp.sample.aggregate.hullo
zabbix_agentd -t 'pcp.sample.event.records[fungus]'

# other error cases
zabbix_agentd -t pcp.sample.bad.nosupport
zabbix_agentd -t pcp.sample.bad.novalues

# verify enumeration of all metrics
echo "== enumerated names and filtered values"
$sudo rm -f $control	# control the dynamic indom
echo '1 one' >$tmp.indom
echo '2 two' >>$tmp.indom
$sudo cp $tmp.indom $control
zabbix_agentd -p | fgrep pcp.sample. | _filter_values

# derived metrics
echo "== check derived metric configuration"
echo mem.util.allcache = mem.util.cached + mem.util.slab | tee $tmp.derived
echo sample.derived.hundredten = sample.float.hundred + sample.double.ten | tee -a $tmp.derived
$sudo cp $tmp.derived $derived
zabbix_agentd -t pcp.mem.util.allcache | _filter_values
zabbix_agentd -t pcp.sample.derived.hundredten | _filter_values

$sudo rm -f $control $derived

# success, all done
status=0
exit
