#!/bin/sh
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA


# Once the "rules" directory is populated, extract all the PCP metric
# names and make sure they are available on the current host or the
# host specified by -h hostname
#

_usage()
{
    echo "Usage: $0 [-h hostname]"
    exit 1
}

source=''
while getopts "h:?" c
do
    case $c
    in
	h)
	    source="-h $OPTARG"
	    ;;
	?)
	    _usage
	    # NOTREACHED
	    ;;
    esac
done
shift `expr $OPTIND - 1`
if [ $# -ne 0 ]
then
    _usage
    # NOTREACHED
fi

if [ ! -d rules ]
then
    echo "$0: cannot find \"rules\" directory!"
    exit 1
fi

find rules -follow -type f -print \
| LC_COLLATE=POSIX sort \
| while read rule
do
    badrule=false
    ./xtractnames <$rule \
    | while read metric
    do

	probe=`pmprobe $source $metric`
	numval=`echo $probe | awk '{print $2}'`
	if [ $numval -lt 0 ]
	then
	    if $badrule
	    then
		:
	    else
		echo
		echo "$rule"
		echo "#"
		echo "# rule: `basename $rule`"
		badrule=true
	    fi
	    echo "#   $probe"
	fi
    done
done
