#!/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"
BBRRDS="/usr/local/bb/rrd"
BBHOME="/usr/local/bb"
GRAPHS="/usr/local/bb/www/graphs"
RRDTOOL="/usr/local/rrdtool/bin/rrdtool"


echo "<HTML> <HEAD> <TITLE> Load Average trends for $DYNAMIC_HOSTNAME "
echo "</TITLE> </HEAD> <BODY> <CENTER>"
echo "<H1> Load Average trends for $DYNAMIC_HOSTNAME </H1> "

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

# set -x

# la-graph.sh

# this script will build load average 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`

# 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

for rrd in `ls ${DYNAMIC_HOSTNAME}.la.rrd`
do
        # need some vars again
        HOST=`echo $rrd | $SED 's/\\.la\.rrd//' `

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

        # make the hourly graph
        $RRDTOOL graph $HOURLY -s e-48h \
                --title "$HOST Load Average Last 48 Hours" \
                -z -w 576 -v "Load Average x 100" -a PNG  \
                DEF:avg=$rrd:la:AVERAGE AREA:avg#$COLOR:"5 Minute Average" \
                GPRINT:avg:LAST:'Current\: %5.1lf%S' \
                GPRINT:avg:MIN:'Min\: %5.1lf%S' \
                GPRINT:avg:AVERAGE:'Average\: %5.1lf%S' \
                GPRINT:avg:MAX:'Max\: %5.1lf%S\j' \
                COMMENT:"$DATE\l" >/dev/null


        # make the daily graph
        $RRDTOOL graph $DAILY -s e-12d \
                --title "$HOST Load Average Last 12 Days" \
                -w 576 -z -a PNG -v "Load Average x 100" \
                DEF:avg=$rrd:la:AVERAGE AREA:avg#$COLOR:"30 Minute Average" \
                GPRINT:avg:LAST:'Current\: %5.1lf%S' \
                GPRINT:avg:MIN:'Min\: %5.1lf%S' \
                GPRINT:avg:AVERAGE:'Average\: %5.1lf%S' \
                GPRINT:avg:MAX:'Max\: %5.1lf%S\j' \
                COMMENT:"$DATE\l" >/dev/null

        # make the weekly graph
        $RRDTOOL graph $WEEKLY -s e-48d \
                --title "$HOST Load Average Last 48 Days" \
                -w 576 -z -a PNG -v "Load Average x 100" DEF:avg=$rrd:la:AVERAGE \
                AREA:avg#$COLOR:"2 Hour Average" \
                GPRINT:avg:LAST:'Current\: %5.1lf%S' \
                GPRINT:avg:MIN:'Min\: %5.1lf%S' \
                GPRINT:avg:AVERAGE:'Average\: %5.1lf%S' \
                GPRINT:avg:MAX:'Max\: %5.1lf%S\j' \
                COMMENT:"$DATE\l" >/dev/null

        # make the monthly graph
        $RRDTOOL graph $MONTHLY -s e-576d \
                --title "$HOST Load Average Last 576 Days" \
                -w 576 -z -a PNG -v "Load Average x 100" DEF:avg=$rrd:la:AVERAGE \
                AREA:avg#$COLOR:"1 Day Average" \
                GPRINT:avg:LAST:'Current\: %5.1lf%S' \
                GPRINT:avg:MIN:'Min\: %5.1lf%S' \
                GPRINT:avg:AVERAGE:'Average\: %5.1lf%S' \
                GPRINT:avg:MAX:'Max\: %5.1lf%S\j' \
                COMMENT:"$DATE\l" >/dev/null

done

echo "<center>"

echo "<table>"
echo "<tr> <th>Hourly</th> </tr>"
echo "<tr> <td><img SRC="/bb/graphs/${HOST}.la.hourly.png"><br></td> </tr>"
echo "<tr> <th>Daily</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.la.daily.png\"><br></td> </tr>"
echo "<tr> <th>Weekly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.la.weekly.png\"><br></td> </tr>"
echo "<tr> <th>Monthly</th> </tr>"
echo "<tr> <td><img SRC=\"/bb/graphs/${HOST}.la.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>"
