#!/bin/sh
# PCP QA Test No. 1041
# Exercise the libvirt PMDA - install, remove and values.
#
# Copyright (c) 2016 Red Hat.
#
# Expectations:
#   1) libvirtd installed and running
#     - typically from libvirt package
#   2) libvirt Python API available
#     - typically from libvirt-python
#   3) optionally one or more VMs up
#   4) libvirt.hv.* metrics always expected
#   5) with VMs available at least:
#     - libvirt.dominfo.uuid
#     - libvirt.dominfo.name
#     - libvirt.dominfo.memory.{boot,current}
#     - libvirt.dominfo.vcpu.*
#     - libvirt.dominfo.type
#     - libvirt.dominfo.os.type
#     - libvirt.domstats.mem.*
#

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

. ./common.python

[ -d $PCP_PMDAS_DIR/libvirt ] || _notrun "libvirt PMDA directory is not installed"
$python -c "from pcp import pmda" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python pcp pmda module not installed"
$python -c "import libvirt" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python libvirt module not installed"
$python -c "import lxml" >/dev/null 2>&1
[ $? -eq 0 ] || _notrun "python lxml module not installed"
[ -e /var/run/libvirt/libvirt-sock-ro ] || _notrun "no socket, libvirtd not running?"

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

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

pmdalibvirt_install()
{
    # start from known starting points
    cd $PCP_PMDAS_DIR/libvirt
    $sudo ./Remove >/dev/null 2>&1

    cat <<EOF >$tmp.config
[pmda]
user = root
uri = qemu:///system
EOF
    echo "pmdalibvirt config:" >> $here/$seq.full
    cat $tmp.config >> $here/$seq.full

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

    echo
    echo "=== libvirt agent installation ==="
    export PCPQA_CHECK_DELAY=10
    $sudo ./Install </dev/null >$tmp.out 2>&1
    cat $tmp.out >>$here/$seq.full
    # Check metrics have appeared ... X metrics and Y values
    # skip lines like ...
    # [Fri Mar 29 21:09:33] pmdalibvirt(21564) Error: pmdaCacheOp: $PCP_VAR_DIR/config/pmda/140.0: empty file?
    _filter_pmda_install <$tmp.out \
    | sed \
        -e '/^Waiting for pmcd/s/\.\.\.[. ]*$/DOTS/' \
	-e "\\;Error: pmdaCacheOp: \$PCP_VAR_DIR/config/pmda/.*: empty file?;d" \
    | tee $tmp.tmp \
    | $PCP_AWK_PROG '
/warnings,/                            { print; next }
/Check libvirt metrics have appeared/  { if ($7 >= 120) $7 = "X"
                                         if ($10 >= 4) $10 = "Y"
                                       }
                                       { print }'
    # on bozo, the install sometimes reports all the metrics as warnings
    # and no values ... dig a bit deeper when this happens
    #
    if grep 'warnings,' $tmp.tmp >/dev/null
    then
	echo "Arrgh: Install failed, see $seq.full"
	pminfo -f pmcd.agent.status >>$here/$seq.full
	pminfo -f libvirt >>$here/$seq.full
	echo "--- pmcd.log ---" >>$here/$seq.full
	cat $PCP_LOG_DIR/pmcd/pmcd.log >>$here/$seq.full
	echo "--- libvirt.log ---" >>$here/$seq.full
	cat $PCP_LOG_DIR/pmcd/libvirt.log >>$here/$seq.full
    fi
}

pmdalibvirt_cleanup()
{
    if [ -f $tmp.backup ]; then
	$sudo cp $tmp.backup $PCP_PMDAS_DIR/libvirt/libvirt.conf
	$sudo rm $tmp.backup
    else
	$sudo rm -f $PCP_PMDAS_DIR/libvirt/libvirt.conf
    fi
    # note: _restore_auto_restart pmcd done in _cleanup_pmda()
    _cleanup_pmda libvirt
    [ -n "$timeout" ] && pmstore pmcd.control.timeout $timeout >>$here/$seq.full 2>&1
}

_prepare_pmda libvirt
trap "pmdalibvirt_cleanup; exit \$status" 0 1 2 3 15

_stop_auto_restart pmcd

_filter()
{
    sed \
	-e '/No value(s) available/d' \
    #end
}

# increase pmcd-PMDA timeout because this one is sometimes slow to
# get going
#
timeout=`pmprobe -v pmcd.control.timeout | $PCP_AWK_PROG '{ print $3 }'`
pmstore pmcd.control.timeout 20 >>$here/$seq.full 2>&1

# real QA test starts here
pmdalibvirt_install

echo
echo "=== verify metric values ==="
echo "from pminfo:" >>$here/$seq.full
pminfo -v libvirt 2>&1 \
| tee -a $here/$seq.full \
| _filter

pmdalibvirt_remove 2>&1 \
| sed \
    -e '/pmdalibvirt([0-9]*) Error: pmdaCacheOp: .* empty file?/d' \
# end
status=0
exit
