#!/bin/sh                                                                                              
#                                                                                                      
# Plugin to graph apache response time of a specific page                                              
#                                                                                                      
# Parameters:                                                                                          
#                                                                                                      
#       target      - overrides the default 'index.html' page                                          
#                                                                                                      
# $Log$                                                                                                
#                                                                                                      
# Revision 1.0  2006/07/11 08:49:43 cipixul@gmail.com
# Initial version
#
#
#%# family=auto
#%# capabilities=autoconf

default_url="http://localhost/"
time_bin=`which time`
wget_bin=`which wget`

if [ "$1" = "autoconf" ]; then
        result="yes"
        [ "x$time_bin" = "x" ] && result=1
        [ "x$wget_bin" = "x" ] && result=2
        if [ "$result" != "yes" ]; then
            echo "no"
            exit $result
        fi
        echo $result
        exit 0
fi

[ "$urls"  ] || urls=$default_url

if [ "$1" = "config" ]; then

        echo "graph_title wget loadtime of a page and requisites"
        echo "graph_args --base 1000 -l 0"
	echo "graph_scale no"
        echo "graph_vlabel Load time in seconds"
        echo "graph_category http"
	echo "graph_info This graph shows load time in seconds of one or more urls"
	I=1
	for url in $urls  ; do 
            echo "loadtime$I.label url$I"
	    echo "loadtime$I.info Load time for $url"
	    echo "loadtime$I.max 60"
	    let I+=1
	done
        exit 0
fi

if [ ! -d /tmp/wget_loadtime/ ];then mkdir /tmp/wget_loadtime/ ; fi
cd /tmp/wget_loadtime/

I=1
for url in $urls  ; do

    $time_bin -p $wget_bin --no-directories --no-cache --delete-after --page-requisites $url 3>&2 2>/tmp/wget_loadtime.tmp
    loadtime=`tail -n 3 /tmp/wget_loadtime.tmp|head -n 1|cut -d ' ' -f 2`
    echo "loadtime$I.value $loadtime"
    let I+=1
done

cd ..
rm -rf /tmp/wget_loadtime
rm -f  /tmp/wget_loadtime.tmp

