#!/bin/bash

# Generate a host-based disk report, in HTML format
# Written by Jeff Stoner <jeff@jeffstoner.com>

# Can be used as a CGI script or on command-line (redirect STDOUT to a file)

# Set appropriately
BBVAR="/opt/bbvar"

# Start our HTML report
echo "Content-type: text/html

"

echo '<HTML><HEAD><TITLE>Host-based disk report</TITLE></HEAD><BODY><CENTER>'

# From the BBVAR/logs directory, we'll process *.disk
ls -1 $BBVAR/logs/*.disk | sort | while read FILE
do
   FILE=`basename $FILE`
   HOST=`basename $FILE .disk`

   echo "<TABLE BORDER='1'>"
   echo "<TR><TH COLSPAN='7' ALIGN='center'>$HOST</TD></TR>"
   echo '<TR><TD>Stauts</TD><TD>Device</TD><TD>Size</TD><TD>Used</TD><TD>Available</TD><TD>% Used</TD><TD>Mount point</TD>'

   # the lines we want are of the form <device> <allocated> <used> <available> <pct used> <mount>
   cat $BBVAR/logs/$FILE | while read DEVICE
   do
      set bogus $DEVICE
      shift
      # check our data
      if test $# -ne 6
      then
         continue
      fi
      if test "$1" = "Status" || test "$1" = "Filesystem"
      then
         continue
      fi
      # check the pct used to see how full the mount point is
      PCT=`echo $5 | sed -e "s/%//"`
      if test $PCT -gt 95
      then
         COLOR="red"
      elif test $PCT -gt 85
      then
         COLOR="yellow"
      else
         COLOR="green"
      fi

      # start spitting out HTML
      echo "<TR><TD BGCOLOR='$COLOR'>&nbsp;</TD><TD>$1</TD><TD>$2</TD><TD>$3</TD><TD>$4</TD><TD>$5</TD><TD>$6</TD></TR>"
   done
   echo "</TABLE><BR>"
done

echo "</CENTER></BODY></HTML>"

exit 0

