#!/bin/sh

NTOP=20

IP_DIR=$1
DATE=$2

#  Set default IP_DIR to $HOME
[ -z $IP_DIR ] && IP_DIR=$HOME

BIN=$IP_DIR/bin

#  Set default date to Sunday before last Sunday
if [ -z $DATE   ]
then
   DATE=`$BIN/pdate -f%Y-%m-%d -a-1d`
fi

#  Read defaults
. $IP_DIR/ipaudit-web.conf

#  Find awk executatble
if [ .$AWK = "." ]
then
   AWK=`which awk`
fi

#  Clean existing output file
OUTPUT=html/$DATE-server2.html
rm -f $OUTPUT



#-----------------------------------------------------------------------
#  Clients for services with single ports
#-----------------------------------------------------------------------

#  Write first level table enclosing all client reports
cat<<EOM >> $OUTPUT
<br><br>
<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
<tr><td>
EOM

cat<<EOM | 
25 Mail
22 SSH
23 Telnet
80 HTTP
443 HTTPS
EOM
while read PORT SERVICE
do

#  Starting time
START_SEC=`$BIN/pdate -f%s`;

#  Write table header
cat<<EOM >> $OUTPUT
   <br><br>
   <table align="center" width="640" border="0" cellspacing="1"
   cellpadding="2" bgcolor="#818181">
   <tr bgcolor="#EDEDED">
       <td colspan="6" align="center"><b>Local $SERVICE Clients</b></td>
   </tr>
    <tr bgcolor="#C9D5E5">
       <td>IP</td>
       <td>Host Name</td>
       <td>Connections</td>
       <td align="right">Incoming<br>
       (bytes)</td>
       <td align="right">Outgoing<br>
       (bytes)</td>
       <td align="right">Total<br>
       (bytes)</td>
    </tr>
EOM


#  Doing ZCAT from within following loop avoids problems 
#  when some of the data files are broken
(for dfile in $IP_DIR/data/30min/$DATE*; do $ZCAT $dfile 2> /dev/null; done) | 
   $AWK '{if ($3==6 && $5=='$PORT') print $1 " " $6 " " $7}' | 
   $BIN/total -f$NTOP -s1r 1 n,2,3 - |
   $BIN/table2html -T -s% \
   -c /ffffff/f5f5dc \
   - 'l("'$CGI_BIN'/SearchIpauditData?date='$DATE'&remote_port='$PORT'&ip=$1","$1")' \
        'd($1)' 'c($2)%r' 'c($3)%r' 'c($4)%r' 'c($3+$4)%r' \
   >> $OUTPUT

#  Calculate elapsed time
END_SEC=`$BIN/pdate -f%s`
ELAPSED=`expr $END_SEC - $START_SEC`

#  Close second level table
cat<<EOM >> $OUTPUT
<tr bgcolor="#C9D5E5"><td align="left" colspan="6">
<i>Elapsed time is $ELAPSED seconds.</i>
</td></tr>
</table>
EOM

done

#  Close first level table for clients
cat<<EOM >> $OUTPUT
</td></tr></table>
<!-- End of Client Reports -->
EOM



#-----------------------------------------------------------------------
#  Servers for services with single ports
#-----------------------------------------------------------------------

#  Write first level table enclosing all service reports
cat<<EOM >> $OUTPUT
<!--  Start of Server Reports -->
<br><br>
<table width="100%" border="0" cellspacing="0" cellpadding="10" align="center">
<tr><td>&nbsp;</td></tr>
<tr><td>
EOM

cat<<EOM | 
25 Mail
22 SSH
23 Telnet
80 HTTP
443 HTTPS
EOM
while read PORT SERVICE
do

#  Starting time
START_SEC=`$BIN/pdate -f%s`;

#  Write second level table enclosing individual service report
cat<<EOM >> $OUTPUT
   <br><br>
   <table align="center" width="640" border="0" cellspacing="1"
      cellpadding="2" bgcolor="#818181">
   <tr bgcolor="#EDEDED">
       <td colspan="6" align="center"><b>Local $SERVICE Servers</b></td>
   </tr>
   <tr bgcolor="#C9D5E5">
       <td>IP</td>
       <td>Host Name</td>
       <td>Connections</td>
       <td align="right">Incoming<br>
       (bytes)</td>
       <td align="right">Outgoing<br>
       (bytes)</td>
       <td align="right">Total<br>
       (bytes)</td>
   </tr>
EOM

#  Doing ZCAT from within following loop avoids problems 
#  when some of the data files are broken
(for dfile in $IP_DIR/data/30min/$DATE*; do $ZCAT $dfile 2> /dev/null; done) | 
   $AWK '{if ($3==6 && $4=='$PORT') print $1 " " $6 " " $7}' | 
   $BIN/total -f$NTOP -s1r 1 n,2,3 - |
   $BIN/table2html -T -s% \
   -c /ffffff/f5f5dc \
   - 'l("'$CGI_BIN'/SearchIpauditData?date='$DATE'&local_port='$PORT'&ip=$1","$1")' \
        'd($1)' 'c($2)%r' 'c($3)%r' 'c($4)%r' 'c($3+$4)%r' \
   >> $OUTPUT

#  Calculate elapsed time
END_SEC=`$BIN/pdate -f%s`
ELAPSED=`expr $END_SEC - $START_SEC`

#  Close second level table
cat<<EOM >> $OUTPUT
<tr bgcolor="#C9D5E5"><td align="left" colspan="6">
<i>Elapsed time is $ELAPSED seconds.</i>
</td></tr>
</table>
EOM

done

#  Close first level table
cat<<EOM >> $OUTPUT
</td></tr></table>
EOM
