#!/bin/sh
echo "Content-type: text/html"
echo

if test ! "$1"
then
        echo "$0: We need a hostname!"
        echo "Usage: $0 hostname"
        exit 1
fi

DYNAMIC_HOSTNAME="$1"
BBHOME="/usr/local/bb"
BBRRDS=$BBHOME/rrd
GRAPHS="/usr/local/bb/www/graphs"
RRDTOOL="/usr/local/rrdtool/bin/rrdtool"


echo "<HTML> <HEAD> <TITLE> TCP connection time trends for $DYNAMIC_HOSTNAME "
echo "</TITLE> </HEAD> <BODY> <CENTER>"
echo "<H1> TCP connection time trends for $DYNAMIC_HOSTNAME </H1> "


# $Id: bbnet-graph.sh,v 1.4 2000/05/05 06:48:19 pearcec Exp $

#set -x

# bbnet-graph.sh

# this script will build tcp PNG's
# using $RRDTOOL graph with RRD's built via
# the BB ext script larrd.sh

if test ! "$BBHOME"
then
        echo "$0: BBHOME is not set"
        exit 1
fi

if test ! -d "$BBHOME"
then
        echo "$0: BBHOME is invalid"
        exit 1
fi

# source the conf
. $BBHOME/etc/bbdef.sh
. $BBHOME/ext/larrd/larrd.conf

# get current date
DATE=`date`
# initialize color counter
c=0

# let's keep the graphs seperate from the RRD's
if [ ! -d $GRAPHS ]
then
	mkdir $GRAPHS	
	echo "$0 did not find $GRAPHS, created." 
fi

# get to work
cd $BBRRDS

# figure out which hosts have tcp info
for HOST in `ls ${DYNAMIC_HOSTNAME}.tcp* | $SED 's/\.tcp.*.rrd//' | uniq ` 
do
	# variable reset for each host
	DEFS=""
	LINES=""
	c=0
	p=0
	
	# get list of parition RRD's and build RRD DEF's and LINES
	for rrd in `ls $HOST.tcp.*.rrd` 
	do
		# gosh i miss perl
		PART=`echo $rrd | $SED 's/^.*tcp\.//' | $SED 's/\.rrd//' `
		# the PARTRRD is arbitrary, so trying to use
		# partition name was senseless cause rrdtool
		# is very sensitive to non-alphanumeric characters
		PARTRRD=$p
		p=`expr $p + 1`

		# the comment should try and look nice
		PARTCOMM=`echo $PART | $SED 's/,/\\//g' `
		
		# for rrd's with : we need to clean up
		# <Jeff@mdli.com>
		rrd=`echo $rrd | $SED 's/\:/\\\:/g' `
		PARTCOMM=`echo $PARTCOMM | $SED 's/\:/\\\:/g' `
				
		# build the DEFs var
		DEFS="$DEFS DEF:$PARTRRD=$rrd:sec:AVERAGE"

		# get the color to use
		# this is soooooo bad but it was the best 
		# i could come up with
		i=0
		for col in $COLORS
		do
			if [ $c -eq $i ]
			then
				COL=$col
			fi
			i=`expr $i + 1`
		done
		if [ $c -eq 12 ]
		then
			c=0
		else
			c=`expr $c + 1`
		fi
				
		# build the LINES var
		LINES="$LINES LINE2:$PARTRRD#$COL:$PARTCOMM" 
	done

	# now start building graphs
	# define the graphs for each time period
	HOURLY=$GRAPHS/$HOST.tcp.hourly.png
	DAILY=$GRAPHS/$HOST.tcp.daily.png
	WEEKLY=$GRAPHS/$HOST.tcp.weekly.png
	MONTHLY=$GRAPHS/$HOST.tcp.monthly.png

	# make the hourly graph
	$RRDTOOL graph $HOURLY -s e-48h --title="$HOST tcp connect \
	time last 48 Hours" \
	-w 576 -a PNG -z -v "Seconds"  $DEFS $LINES \
	COMMENT:"\s" COMMENT:"$DATE\r" >/dev/null
	
	# make the daily graph
	$RRDTOOL graph $DAILY -s e-12d --title="$HOST tcp connect \
	time last 12 Days" \
	-w 576 -a PNG -z -v "Seconds" $DEFS $LINES \
	COMMENT:"\s" COMMENT:"$DATE\r" >/dev/null

	# make the weekly graph
 	$RRDTOOL graph $WEEKLY -s e-48d --title="$HOST tcp connect \
	time last  48 Days" \
	-w 576 -a PNG -z -v "Seconds" $DEFS $LINES COMMENT:"\s" \
	COMMENT:"$DATE\r" >/dev/null

	# make the monthly 
	$RRDTOOL graph $MONTHLY -s e-576d --title="$HOST tcp connect \
	time last 576 Days" \
	-w 576 -a PNG -z -v "Seconds" $DEFS $LINES COMMENT:"\s" \
	COMMENT:"$DATE\r" >/dev/null

done

echo "<center>"

echo "<table>"
echo "<tr> <th>Hourly</th> </tr>"
echo "<tr> <td><img SRC="/bb/graphs/${HOST}.tcp.hourly.png"><br></td> </tr>"
echo "<tr> <th>Daily</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.tcp.daily.png\"><br></td> </tr>"
echo "<tr> <th>Weekly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.tcp.weekly.png\"><br></td> </tr>"
echo "<tr> <th>Monthly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.tcp.monthly.png\"></td> </tr>"
echo "</table> </center>"

echo "<BR> larrd is proud to be:"
echo "<A HREF=\"http://ee-staff.ethz.ch/~oetiker/webtools/rrdtool/\">"
echo "<IMG SRC="/bb/gifs/rrdtool.gif"></A> "
echo "</body> </html>"



