#! /bin/sh
# PCP QA Test No. 654
# checks basic pmdagfs2 functionality
# Requires GFS2 support in the kernel, so may need a "modprobe gfs2",
# depending on how you like to roll your kernels.
#
# Copyright (c) 2013 Red Hat, Inc.  All Rights Reserved.
#
seq=`basename $0`
echo "QA output created by $seq"

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

[ $PCP_VER -ge 3611 ] || _notrun "Installed pcp version is too old"
[ $PCP_PLATFORM = linux ] || _notrun "GFS2 test, only works with Linux"
[ -d /sys/kernel/debug/gfs2 ] || _notrun "No kernel support for GFS2 stats"
[ -x /sbin/mkfs.gfs2 ] || _notrun "No userspace support for GFS2 filesystems"

# check for any loopback devices, cowardly abort this test if any exist
loopcount=`$sudo losetup -a | wc -l`
[ $loopcount -eq 0 ] || _notrun "System has loop devices already, bailing"


_filter_gfs2()
{
    sed -e 's/ [0-9[0-9]* metrics/ N metrics/g'
}

_install_pmda()
{
    # install the PMDA
    $sudo ./Remove < /dev/null >/dev/null 2>&1
    $sudo ./Install < /dev/null >$tmp.out 2>&1
    cat $tmp.out | _filter_pmda_install | _filter_gfs2
}

_setup_mounts()
{
    # create a couple of filesystems on sparse files, mount.
    for fs in 1 0
    do
	echo "creating pseudo device $fs"
	dd of=$tmp.loop$fs if=/dev/zero bs=1048576 seek=1024 count=1 2>/dev/null
	echo "creating a mount point $fs"
	mkfs.gfs2 -O -p lock_nolock -j 1 $tmp.loop$fs >/dev/null
	echo "creating device file $fs"
	$sudo losetup /dev/loop$fs $tmp.loop$fs
	echo "creating a mount point $fs"
	mkdir -p $tmp.mount$fs.dir
	echo "mounting pseudo device $fs"
	$sudo mount -t gfs2 /dev/loop$fs $tmp.mount$fs.dir
    done
}

_cleanup()
{
    for fs in 0 1
    do
	$sudo umount $tmp.mount$fs.dir
	$sudo losetup -d /dev/loop$fs
    done

    _restore_pmda_install $iam
    $sudo rm -fr $tmp.*.dir
    $sudo rm -f $tmp.*
    exit $status
}

_filter_pminfo()
{
    tee -a $here/$seq.full | sed \
	-e 's/value [0-9][0-9]*/value NUMBER/'
}

iam=gfs2
status=1	# failure is the default!
trap "_cleanup" 0 1 2 3 15

$sudo rm -fr $tmp.*.dir
$sudo rm -f $tmp.* $seq.full
touch $here/$seq.full

# real QA test starts here
_prepare_pmda_install $iam
_install_pmda
_setup_mounts || _notrun "No kernel support for GFS2 sbstats"

echo
echo "=== Basic glock stats from all kernels ===" | tee -a $here/$seq.full
pminfo -fdm gfs2.glocks | _filter_pminfo

echo
echo "=== Additional sbstats (dynamic) ===" | tee -a $here/$seq.full
pminfo -fdm gfs2.sbstats | _filter_pminfo

status=0
exit
