#!/bin/bash

#
# YAWS server hits
#
# Needs following minimal configuration in plugin-conf.d/munin-node:
# [yaws*]
# user root
#
# Magick markers
#%# family=auto
#%# capabilities=autoconf


ID=`basename $0 | sed 's/^yaws_hits_//g'`
TMPFILE=.yaws_hits_$ID
STATS=`yaws --stats --id $ID | tail --lines=+2 > $TMPFILE`

if [ "$1" = "autoconf" ]; then
	if which yaws > /dev/null; then
                echo yes
                exit 0
        else
                echo no
                exit 1
        fi
fi

if [ "$1" = "config" ]; then
        echo graph_title Hits for server $ID
        echo graph_vlabel Hits
        echo graph_category Yaws
        echo graph_args --base 1024 -l 0

	while read line; do
	    HOST=`echo $line | awk '{ print $1 }'`
	    FIELD=`echo $HOST | sed 's/[\.:]/_/g'`

	    echo $FIELD.label $HOST
	    echo $FIELD.draw LINE2
	    echo $FIELD.type DERIVE
	    echo $FIELD.min 0
	done < $TMPFILE

	rm -f $TMPFILE

        exit 0
fi

while read line; do
    HOST=`echo $line | awk '{ print $1 }'`
    FIELD=`echo $HOST | sed 's/[\.:]/_/g'`
    HITS=`echo $line | awk '{ print $4 }'`

    echo $FIELD.value $HITS
done < $TMPFILE

rm -f $TMPFILE

exit 0
