#!/bin/bash

# Script to output the current status - as "real time" as it gets
# Written by Jeff Stoner <jeff@jeffstoner.com>

# Config
BBHOME="/opt/bb"
. $BBHOME/etc/bbdef.sh
if test ! "$BBVAR"
then
   BBVAR="$BBHOME/www"
fi

WEBLOCATION="${BBWEBHOST}${CGIBINURL}"
# We need a temp directory, one that exists and that the web process can write to
#WEBTMP="$BBTMP/web"   # if your web server runs as the same UID/GID as Big Brother
WEBTMP="/tmp/web"     # everyone can write here!  :-)

test ! -d $WEBTMP && mkdir $WEBTMP
if test ! -w $WEBTMP
then
echo "Content-type: text/html

<HTML><HEAD><TITLE>Error!</TITLE></HEAD><BODY><P>ERROR: Cannot write to temporary directory [$WEBTMP]</P></BODY></HTML>"
   exit 1
fi

# Build a list of red, yellow, purple and blue clients

REDTMP="$WEBTMP/red.$$"
YELLOWTMP="$WEBTMP/yellow.$$"
BLUETMP="$WEBTMP/blue.$$"
PURPLETMP="$WEBTMP/purple.$$"

CWD=`pwd`
cd $BBVAR/logs
$GREP '^red ' * | $CUT -d: -f1 >> $REDTMP
$GREP '^yellow ' * | $CUT -d: -f1 >> $YELLOWTMP
$GREP '^blue ' * | $CUT -d: -f1 >> $BLUETMP
$GREP '^purple ' * | $CUT -d: -f1 >> $PURPLETMP
cd $CWD

# Start generating our HTML

$CAT << ENDTOP
Content-type: text/html


<HTML><HEAD><TITLE>Real-time Big Brother Status</TITLE>
<META HTTP-EQUIV="REFRESH" CONTENT="60"><META HTTP-EQUIV="EXPIRES" CONTENT="Thu, 01 Jan 1970 00:00:00 GMT"></HEAD><BODY>
<CENTER><H2>Updated: `date`</H2><BR>
<TABLE><TR><TD><IMG SRC="$BBSKIN/red.gif" WIDTH="$DOTWIDTH" HEIGHT="$DOTHEIGHT" ALT="red" BORDER="0"></TD><TD><FONT SIZE="+3">Red Alerts</FONT></TD></TR>
ENDTOP

# Each row will be an individual alert

if test -s $REDTMP
then
   $CAT $REDTMP | while read
   do
      echo "<TR><TD COLSPAN='2' ALIGN='center'><A HREF='$WEBLOCATION/bb-hostsvc.sh?HOSTSVC=$REPLY' TARGET="_self">$REPLY</A></TD></TR>"
   done
else
   echo "<TR><TD COLSPAN='2' ALIGN='center'>No Red Alerts</TD></TR>"
fi

# More HTML

$CAT << ENDHTML2
</TABLE>
<P>&nbsp;</P>
<TABLE><TR><TD><IMG SRC="$BBSKIN/yellow.gif" WIDTH="$DOTWIDTH" HEIGHT="$DOTHEIGHT" ALT="yellow" BORDER="0"></TD><TD><FONT SIZE="+3">Yellow Alerts</FONT></TD></TR>
ENDHTML2

# Each row will be an individual alert

if test -s $YELLOWTMP
then
   $CAT $YELLOWTMP | while read
   do
      echo "<TR><TD COLSPAN='2' ALIGN='center'><A HREF='$WEBLOCATION/bb-hostsvc.sh?HOSTSVC=$REPLY' TARGET='_self'>$REPLY</A></TD></TR>"
   done
else
   echo "<TR><TD COLSPAN='2' ALIGN='center'>No Yellow Alerts</TD></TR>"
fi

# Display all purples

$CAT << ENDHTML3
</TABLE>
<P>&nbsp;</P>
<TABLE><TR><TD><IMG SRC="$BBSKIN/purple.gif" WIDTH="$DOTWIDTH" HEIGHT="$DOTHEIGHT" ALT="purple" BORDER="0"></TD><TD><FONT SIZE="+3">Purple Alerts</FONT></TD></TR>
ENDHTML3

if test -s $PURPLETMP
then
   $CAT $PURPLETMP | while read
   do
      echo "<TR><TD COLSPAN='2' ALIGN='center'><A HREF='$WEBLOCATION/bb-hostsvc.sh?HOSTSVC=$REPLY' TARGET='_self'>$REPLY</A></TD></TR>"
   done
else
   echo "<TR><TD COLSPAN='2' ALIGN='center'>No Purple Alerts</TD></TR>"
fi

# And finally, disabled services

$CAT << ENDHTML4
</TABLE>
<P>&nbsp;</P>
<TABLE><TR><TD><IMG SRC="$BBSKIN/blue.gif" WIDTH="$DOTWIDTH" HEIGHT="$DOTHEIGHT" ALT="blue" BORDER="0"></TD><TD><FONT SIZE="+3">Disabled Hosts/Services</FONT></TD></TR>
ENDHTML4

# Each row will be an individual alert

if test -s $BLUETMP
then
   $CAT $BLUETMP | while read
   do
      echo "<TR><TD COLSPAN='2' ALIGN='center'><A HREF='$WEBLOCATION/bb-hostsvc.sh?HOSTSVC=$REPLY' TARGET='_self'>$REPLY</A></TD></TR>"
   done
else
   echo "<TR><TD COLSPAN='2' ALIGN='center'>No Disabled Services</TD></TR>"
fi

# Finish it all off

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

$RM -f $REDTMP $YELLOWTMP $BLUETMP $PURPLETMP

exit 0


