#!/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> Disk Usage trends for $DYNAMIC_HOSTNAME "
echo "</TITLE> </HEAD> <BODY> <CENTER>"
echo "<H1> Disk Usage trends for $DYNAMIC_HOSTNAME </H1> "

#  need to add disk used 'absolute'

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

# Revision: 06/05/2000 mcd - add in ability to limit number of plots per graph
#   wil have multiple disk-graphs generated per host with more viewable graphs

if [ -z "$BBHOME" ]; then
        echo "$0: BBHOME is not set"
        exit 1
fi

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

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

DATE=`date`                     # get current date
MAX_LINE=6                  # maximum number of plots per graph

MAX_LINE=`expr ${MAX_LINE} - 1`         # starting from 0 for counter not 1
#--------------------------------------------------------------------------------------
# functions
#--------------------------------------------------------------------------------------
make_graphs() 
{
    # define the graphs for each time period
    HOURLY=$GRAPHS/$HOST.diskpct${CNT}.hourly.png
    DAILY=$GRAPHS/$HOST.diskpct${CNT}.daily.png
    WEEKLY=$GRAPHS/$HOST.diskpct${CNT}.weekly.png
    MONTHLY=$GRAPHS/$HOST.diskpct${CNT}.monthly.png

    # make the hourly graph
    $RRDTOOL graph $HOURLY -s e-48h --title="$HOST Disk Utilization last 48 Hours" \
    -w 576 -a PNG -z -v "% Full" -u 100 -l 0 $DEFS $LINES \
    COMMENT:"\s" COMMENT:"$DATE\r" >/dev/null
    
    # make the daily graph
    $RRDTOOL graph $DAILY -s e-12d --title="$HOST Disk Utilization last 12 Days" \
    -w 576 -a PNG -z -v "% Full" -u 100 -l 0 $DEFS $LINES \
    COMMENT:"\s" COMMENT:"$DATE\r" >/dev/null

    # make the weekly graph
    $RRDTOOL graph $WEEKLY -s e-48d --title="$HOST Disk Utilization last 48 Days" \
    -w 576 -a PNG -z -v "% Full" -u 100 -l 0 $DEFS $LINES COMMENT:"\s" \
    COMMENT:"$DATE\r" >/dev/null

    # make the monthly 
    $RRDTOOL graph $MONTHLY -s e-576d --title="$HOST Disk Utilization last 576 Days" \
    -w 576 -a PNG -z -v "% Full" -u 100 -l 0 $DEFS $LINES COMMENT:"\s" \
    COMMENT:"$DATE\r" >/dev/null

    CNT=`expr $CNT + 1`
}

nail_obsolete_png() 
{
    # the current CNT value points to set beyond last current set

    # rather then run for ever, put a practical limit on the loop
    # while :
    while [ $CNT -lt 100 ]
    do
        HOURLY=$GRAPHS/$HOST.diskpct${CNT}.hourly.png
        DAILY=$GRAPHS/$HOST.diskpct${CNT}.daily.png
        WEEKLY=$GRAPHS/$HOST.diskpct${CNT}.weekly.png
        MONTHLY=$GRAPHS/$HOST.diskpct${CNT}.monthly.png

        if [ -f ${HOURLY} -o -f ${DAILY} -o -f ${WEEKLY} -o -f ${MONTHLY} ];
        then
            rm -f ${HOURLY} ${DAILY} ${WEEKLY} ${MONTHLY} 2>/dev/null
            CNT=`expr ${CNT} + 1`
        else
            return 
        fi
    done
}

#--------------------------------------------------------------------------------------
# main logic loop
#--------------------------------------------------------------------------------------
# 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 disk info
for HOST in `ls ${DYNAMIC_HOSTNAME}.disk* | $SED 's/\.disk.*.rrd//' | uniq ` 
do
    # variable reset for each host
    DEFS=""
    LINES=""
    c=0
    p=0
    CNT=0
    
    # get list of parition RRD's and build RRD DEF's and LINES
    for rrd in `ls $HOST.disk,*.rrd` 
    do

        # gosh i miss perl
#        PART=`echo $rrd | $SED 's/^.*disk//' | $SED 's/\.rrd//' `
        PART=`echo $rrd | $SED 's/^.*disk,/,/' | $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' `
        if [ $PARTCOMM = "/root" ]
        then
            PARTCOMM="/"
        fi      
        
        # build the DEFs var
        DEFS="$DEFS DEF:$PARTRRD=$rrd:pct: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

        # build the LINES var
        LINES="$LINES LINE2:$PARTRRD#$COL:$PARTCOMM"

        if [ $c -eq ${MAX_LINE} ]; then
            make_graphs
            c=0
            LINES=""
            DEFS=""
        else
            c=`expr $c + 1`
        fi
    done

    # handle remainder
    if [ -n "${LINES}" ]; then
        make_graphs
    fi

    # handle possible stale pngs left over from previous runs
    nail_obsolete_png
done


cd /usr/local/bb/www/graphs
echo "<center>"

echo "<table>"
echo "<tr> <th>Hourly</th> </tr>"
#     $GRAPHS/$HOST.diskpct${CNT}.hourly.png

for png in `ls ${HOST}.diskpct*.hourly.png`
do
	echo "<tr> <td><img SRC="/graphs/${png}"><br></td> </tr>"
done

echo "<tr> <th>Daily</th> </tr>"
for png in `ls ${HOST}.diskpct*.daily.png`
do
	echo "<tr> <td><img SRC="/graphs/${png}"><br></td> </tr>"
done


echo "<tr> <th>Weekly</th> </tr>"
for png in `ls ${HOST}.diskpct*.weekly.png`
do
	echo "<tr> <td><img SRC="/graphs/${png}"><br></td> </tr>"
done


echo "<tr> <th>Monthly</th> </tr>"
for png in `ls ${HOST}.diskpct*.monthly.png`
do
	echo "<tr> <td><img SRC="/graphs/${png}"><br></td> </tr>"
done

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>"
