#!/bin/sh
# PCP QA Test No. 835
# Exercise the memcache PMDA - install, remove and values.
#
# Copyright (c) 2014-2015 Red Hat.
# Copyright (c) 2014 Ken McDonell.  All Rights Reserved.
# Copyright (c) 2008 Aconex.  All Rights Reserved. (based on qa/348)
#

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

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

[ -d $PCP_PMDAS_DIR/memcache ] || _notrun "memcache PMDA directory is not installed"

port=11211
$PCP_BINADM_DIR/telnet-probe -c localhost $port || \
    _notrun "Noones home on the default memcached port $port"

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

pmdamemcache_remove()
{
    echo
    echo "=== remove memcache agent ==="
    $sudo ./Remove >$tmp.out 2>&1
    _filter_pmda_remove <$tmp.out
}

pmdamemcache_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/memcache
    $sudo ./Remove >/dev/null 2>&1
    $sudo $PCP_RC_DIR/pmcd stop

    cat <<EOF >$tmp.config
\$memcache_delay = 1;
@memcache_instances = ( 0 => '127.0.0.1:11211' );
EOF
    echo "pmdamemcache config:" >> $here/$seq.full
    cat $tmp.config >> $here/$seq.full

    [ -f $PCP_PMDAS_DIR/memcache/memcache.conf ] && \
    $sudo cp $PCP_PMDAS_DIR/memcache/memcache.conf $tmp.backup
    $sudo cp $tmp.config $PCP_PMDAS_DIR/memcache/memcache.conf

    echo
    echo "=== memcache agent installation ==="
    $sudo ./Install </dev/null >$tmp.out 2>&1
    # Check metrics have appeared ... X metrics and Y values
    _filter_pmda_install <$tmp.out \
    | sed \
        -e '/^Waiting for pmcd/s/\.\.\.[. ]*$/DOTS/' \
    | $PCP_AWK_PROG '
/Check memcache metrics have appeared/  { if ($7 >= 16 && $7 <= 50) $7 = "X"
                                          if ($10 >= 16 && $10 <= 50) $10 = "Y"
                                        }
                                        { print }'
}

pmdamemcache_cleanup()
{
    if [ -f $tmp.backup ]; then
	$sudo cp $tmp.backup $PCP_PMDAS_DIR/memcache/memcache.conf
	$sudo rm $tmp.backup
    else
	$sudo rm -f $PCP_PMDAS_DIR/memcache/memcache.conf
    fi
    _cleanup_pmda memcache
}

_prepare_pmda memcache
trap "pmdamemcache_cleanup; exit \$status" 0 1 2 3 15

# make pmprobe output comparable to memcachestats
rewrite_memcache_metric_names()
{
    sed \
	-e 's/^memcache.//g' \
	-e 's/^gets /cmd_get /g' \
	-e 's/^sets /cmd_set /g' \
	-e 's/^hits /get_hits /g' \
	-e 's/^misses /get_misses /g' \
	-e 's/^current_items/curr_items/g' \
	-e 's/^current_connections/curr_connections/g' \
    #end#
}

# real QA test starts here
pmdamemcache_install

echo
echo "=== extract metric values ==="
pmprobe -v memcache \
| rewrite_memcache_metric_names \
| LC_COLLATE=POSIX sort >$tmp.probe
echo "from pmprobe ..." >>$here/$seq.full

$here/src/memcachestats.pl \
| sed -e 's/ / 1 /' \
| LC_COLLATE=POSIX sort >$tmp.values
echo "from src/memcachestats.pl ..." >>$here/$seq.full

cat $tmp.values >>$here/$seq.full
LC_COLLATE=POSIX join $tmp.probe $tmp.values >$tmp.all
echo >>$here/$seq.full
cat $tmp.all >>$here/$seq.full

echo
echo "=== check values ==="
cat $tmp.all \
| while read metric n1 vpcp n2 vmemcache
do
    if [ "$n1" = 1 -a "$n2" = 1 ]
    then
        case $metric
        in
	    bytes_read|bytes_written)
		# unstable, skip these ones
		;;
	    curr_connections|connection_structures)
		# +/- 2
                _within_tolerance $metric $vpcp $vmemcache 2 -v
                ;;
            total_connections)
		# +/- 5
                _within_tolerance $metric $vpcp $vmemcache 5 -v
                ;;
            *)
		# +/- 10%
                _within_tolerance $metric $vpcp $vmemcache 10% -v
                ;;
        esac
    else
        echo "$metric: number of values not 1 as expected: pcp $n1 / memcache $n2"
    fi
done | tee -a $here/$seq.full

pmdamemcache_remove
status=0
exit
