Attention: All pages of this wiki depend on the pages that come before it, in order as they are listed on the Main Page. Please check for Dependencies.
Please also look at What You Need to Know Before Using This Wiki

/root/scripts/bwrprt.sh: Difference between revisions

From COCNM
Jump to navigation Jump to search
No edit summary
 
(No difference)

Latest revision as of 23:49, 15 April 2014

BIG FAT NOTE: This script is know to have issues on fast connections, particularly when using 32 bit systems on connections that can exceed 4 GB/hour
This script still works well enough for slower connections, but find a script for faster connections with a few more features here: /root/scripts/bwrpt.pl



#!/bin/bash

## Written by Bob Miller
## Computerisms.ca
## Last updated October 2012
## Please feel free to share this script, but please do not claim it.

## This script depends on xtables-addons, and usage of the ACCOUNT target.

## This will set up the tables we need in iptables to do our accounting.  We need one entry for each lan subnet, and 0/0 is for the external address:
## iptables -t filter -I FORWARD -j ACCOUNT --addr 192.168.25.0/24 --tname lan
## iptables -t mangle -I POSTROUTING -j ACCOUNT --addr 0/0 --tname wan

## Hostname detection currently requires at least the nbtscan program, make sure that and the iptaccount program are in your PATH

## Set this program to run every hour in your crontab.  Note: if you can/will exceed the IPTWRAP or NETWRAP values in the space of one hour,
## you will need to run this script more often, and adjust the TTLNOW value and routine to account for minutes instead of just hours.

#########################################################################################
#########################################################################################
##                                                                                     ##
##                          Configurable Options                                       ##
##                                                                                     ##
#########################################################################################
#########################################################################################

## Next, we need to set up some variables to make configuration and life easy.
## Modify the following variables to your needs

## Make sure your path includes the iptaccount and nbtscan commands

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/root/scripts

## Set a comma-separated list of recipients for the daily report

ADMINML=bob.miller@computerisms.com
MLFRM=porchlian@computerisms.com

## Set the total number of internal interfaces (do not include external interface) - Valid value is any integer higher than zero:

NETNUM=2

## these are the tables you configured your --tname as.  Add more NNM* variables as needed

NNM1=computerisms
NNM2=pubaccess
NNM3=
NNM4=

## For each table in iptaccount, Set the ethx interface of each port to match the NNM number (ie NNM1=IF1=ethx, NNM2=IF2=ethx)

WAN=eth0
IF1=eth1
IF2=eth2
IF3=
IF4=

## For the /sys statistics, the counter will wrap after a certain number of bits.  On a 32-bit system, the counter wraps at
## 32 bits (or (2^32)-1 or 4 GB, so set it as 4294967295.  on a 64-bit system, this number is (2^63)-1 or 18446744073709551615.  
## Set this number for NETWRAP.  For the iptaccount program, the wrap is currently hard coded to 32 bits so use 4294967295.  
## If this changes, it will presumably still need to wrap at some point, so set IPTWRAP to the number it wraps at.
## Set the wrap numbers:

IPTWRAP=4294967295
NETWRAP=4294967295

## Set a base directory to hold all your working stats.  Stat files will be kept here for archival purposes

WRKDIR=/root/scripts/bwfiles

## set how much data is your 100% monthly usage limit in GB (valid value is any integer greater than zero):

USAGE=90

## Set if you want Mega or Mebi (valid values are 1000 or 1024)

DIV=1000

## Totalling and reporting should happen only once per day, at the end of day.  
## This is a way of only running the totals on the last hour of production.  
## Set this value to the last hour of the day the script runs in 24-hour format
## Note that a report can only contain today's data, so you can't get 9-12PM on tomorrow's report
## I put this here for testing, so I could make it run at the top of the next hour,
## But it might be useful if you shut your interface or machine down on a schedule.

TTLNOW=23

#########################################################################################
#########################################################################################
##                                                                                     ##
##                      END Configurable Options                                       ##
##                                                                                     ##
#########################################################################################

## That should be all the configuration items required.  You probably shouldn't modify the rest of this 
## Here are more variables that we will need later.

DOIPT=$(which iptaccount)
DONBT=$(which nbtscan)

## The following variables need to be set to prevent unwanted effects:

TTLIN=0
TTLOUT=0
TTLWAN=0
STIPTIN=0
STIPTOUT=0
TMTDIPTWAN=0
TMTDRAWWANIN=0
TMTDRAWWANOUT=0
TMTDRAWWAN=0
TMTDRAWWANIN=0
TMTDRAWWANOUT=0
TMTDIF=0
TMTDIFIN=0
TMTDIFOUT=0
TMTDLAN=0
TMTDLANIN=0
TMTDLANOUT=0

## These variables will give us a way to deal with IP addresses.

LAN1=$(/sbin/ifconfig $IF1 | sed -n -e'/inet addr:/s/^.*inet addr:\([0-9.]*\).*$/\1/p' | cut -f 1,2,3 -d .)
LAN2=$(/sbin/ifconfig $IF2 | sed -n -e'/inet addr:/s/^.*inet addr:\([0-9.]*\).*$/\1/p' | cut -f 1,2,3 -d .)
LAN3=$(/sbin/ifconfig $IF3 | sed -n -e'/inet addr:/s/^.*inet addr:\([0-9.]*\).*$/\1/p' | cut -f 1,2,3 -d .)
LAN4=$(/sbin/ifconfig $IF4 | sed -n -e'/inet addr:/s/^.*inet addr:\([0-9.]*\).*$/\1/p' | cut -f 1,2,3 -d .)

## We need to create time variables for setting up our directory system and other things

LOGDT=$(date "+%F %T")
CURHOUR=$(date +%H)
YR=$(date +%Y)
MNTH=$(date +%m)
DAY=$(date +%d)
LSTYR=$(date +%Y --date="-1 day")
LSTMNTH=$(date +%m --date="-1 day")
LSTDAY=$(date +%d --date="-1 day")

## Let's set up our directory system:

if [ ! -d $WRKDIR ]; then
        mkdir $WRKDIR;
fi
if [ ! -d $WRKDIR/$YR ]; then
        mkdir $WRKDIR/$YR;
fi
if [ ! -d $WRKDIR/$YR/$MNTH ]; then
        mkdir $WRKDIR/$YR/$MNTH;
fi
if [ ! -d $WRKDIR/$YR/$MNTH ]; then
        mkdir $WRKDIR/$YR/$MNTH;
fi
if [ ! -d $WRKDIR/$YR/$MNTH/$DAY ]; then
        mkdir $WRKDIR/$YR/$MNTH/$DAY;
fi

## Lets make a working dir variable for yesterday and today

CURWRKDIR=$WRKDIR/$YR/$MNTH/$DAY
LSTWRKDIR=$WRKDIR/$LSTYR/$LSTMNTH/$LSTDAY

## Now we need to put some data in these directories.  This will populate all the LAN IPT data:

for NUM in $(seq $NETNUM); do
LAN=$(eval echo \$LAN$NUM)
NNM=$(eval echo \$NNM$NUM)
	
	## If this is the first run of the day, this stanza will create our directory and bring forward totals from the previous day.

       	if [ ! -d $CURWRKDIR/$LAN ]; then 
		mkdir $CURWRKDIR/$LAN
		if [[ $(ls $CURWRKDIR/$LAN/*.in) == "" ]]; then
			if [[ $(ls $LSTWRKDIR/$LAN/*.in) != "" ]]; then
				cp $LSTWRKDIR/$LAN/*.in $CURWRKDIR/$LAN/
			fi
		fi
		if [[ $(ls $CURWRKDIR/$LAN/*.out) == "" ]]; then
			if [[ $(ls $LSTWRKDIR/$LAN/*.out) != "" ]]; then
				cp $LSTWRKDIR/$LAN/*.out $CURWRKDIR/$LAN/
			fi
		fi
	fi

	## Move last run's data into the .old files

	if [[ $(ls $CURWRKDIR/$LAN/*.in) != "" ]]; then
		for MVIN in $(ls $CURWRKDIR/$LAN/*.in); do 
			mv $MVIN $MVIN.old
		done
	fi
	if [[ $(ls $CURWRKDIR/$LAN/*.out) != "" ]]; then
		for MVOUT in $(ls $CURWRKDIR/$LAN/*.out); do 
			mv $MVOUT $MVOUT.old
		done
	fi

	## Populate this run's data.

       	for DATA in $($DOIPT -l $NNM -s | grep $LAN); do
               	FN=$(echo $DATA | cut -f 1 -d \;)
               	echo $DATA | cut -f 3 -d \; > $CURWRKDIR/$LAN/$FN.in
               	echo $DATA | cut -f 5 -d \; > $CURWRKDIR/$LAN/$FN.out
       	done
	for RT in $(ls $CURWRKDIR/$LAN/*.in); do
		HOST=$(basename $RT .in)
		NEWIN=$(cat $CURWRKDIR/$LAN/$HOST.in)
		if [ ! -e $CURWRKDIR/$LAN/$HOST.in.old ]; then
			echo 0 > $CURWRKDIR/$LAN/$HOST.in.old
		fi
		OLDIN=$(cat $CURWRKDIR/$LAN/$HOST.in.old)
		if [[ $NEWIN -lt $OLDIN ]]; then
			NEWIN=$(( $NEWIN + $IPTWRAP ))
		fi
		NEWOUT=$(cat $CURWRKDIR/$LAN/$HOST.out)
		if [ ! -e $CURWRKDIR/$LAN/$HOST.out.old ]; then
			echo 0 > $CURWRKDIR/$LAN/$HOST.out.old
		fi
		OLDOUT=$(cat $CURWRKDIR/$LAN/$HOST.out.old)
		if [[ $NEWOUT -lt $OLDOUT ]]; then
			NEWOUT=$(( $NEWOUT + $IPTWRAP ))
		fi
		RUNIN=$(( $NEWIN - $OLDIN ))
		RUNOUT=$(( $NEWOUT - $OLDOUT ))
		echo "$LOGDT;$RUNIN;$RUNOUT" >> $CURWRKDIR/$LAN/$HOST.rt
	done 
done

## This will give IPT data for external interface
## First, check if this is first run today and if it is set up directory and bring last data forward

if [ ! -d $CURWRKDIR/WAN ]; then
	mkdir $CURWRKDIR/WAN
	if [ ! -e $CURWRKDIR/WAN/wan ]; then
		if [ -e $LSTWRKDIR/WAN/wan ]; then
			cp $LSTWRKDIR/WAN/wan $CURWRKDIR/WAN/
		fi
	fi
fi
if [ -e $CURWRKDIR/WAN/wan ]; then
	mv $CURWRKDIR/WAN/wan $CURWRKDIR/WAN/wan.old
else
	echo 0 > $CURWRKDIR/WAN/wan.old
fi

## Now populate the current data:

$DOIPT -l wan | grep bytes | cut -f 4 -d : | cut -f 2 -d " " > $CURWRKDIR/WAN/wan
NEWWAN=$(cat $CURWRKDIR/WAN/wan)
OLDWAN=$(cat $CURWRKDIR/WAN/wan.old)
if [[ $NEWWAN -lt $OLDWAN ]]; then
	NEWWAN=$(( $NEWWAN + $IPTWRAP ))
fi
RUNWAN=$(echo "$NEWWAN - $OLDWAN" | bc)
echo "$LOGDT;$NEWWAN;$OLDWAN;$RUNWAN" >> $CURWRKDIR/WAN/wan.rt

## This is to give raw egress/ingress data on internal interfaces

for NUM in $(seq $NETNUM); do
	IF=$(eval echo \$IF$NUM)

	## Test if this is first run, if it is, make the directory and bring last day's data forward.

	if [ ! -d $CURWRKDIR/IFACE ]; then
		mkdir $CURWRKDIR/IFACE
	fi
	if [ ! -e $CURWRKDIR/IFACE/$IF.in ]; then
		if [ -e $LSTWRKDIR/IFACE/$IF.in ]; then
			cp $LSTWRKDIR/IFACE/$IF.in $CURWRKDIR/IFACE/
		fi
	fi
	if [ ! -e $CURWRKDIR/IFACE/$IF.out ]; then
		if [ -e $LSTWRKDIR/IFACE/$IF.out ]; then
			cp $LSTWRKDIR/IFACE/$IF.out $CURWRKDIR/IFACE/
		fi
	fi
	if [ -e $CURWRKDIR/IFACE/$IF.in ]; then
		mv $CURWRKDIR/IFACE/$IF.in $CURWRKDIR/IFACE/$IF.in.old
	else
		echo 0 > $CURWRKDIR/IFACE/$IF.in.old
	fi
	if [ -e $CURWRKDIR/IFACE/$IF.out ]; then
		mv $CURWRKDIR/IFACE/$IF.out $CURWRKDIR/IFACE/$IF.out.old
	else
		echo 0 > $CURWRKDIR/IFACE/$IF.out.old
	fi
	cat /sys/class/net/$IF/statistics/rx_bytes > $CURWRKDIR/IFACE/$IF.in
	cat /sys/class/net/$IF/statistics/tx_bytes > $CURWRKDIR/IFACE/$IF.out
	NEWRX=$(cat $CURWRKDIR/IFACE/$IF.in)
	NEWTX=$(cat $CURWRKDIR/IFACE/$IF.out)
	OLDRX=$(cat $CURWRKDIR/IFACE/$IF.in.old)
	OLDTX=$(cat $CURWRKDIR/IFACE/$IF.out.old)
	if [[ $NEWRX -lt $OLDRX ]]; then
		NEWRX=$(( $NEWRX + $NETWRAP ))
	fi
	if [[ $NEWTX -lt $OLDTX ]]; then
		NEWTX=$(( $NEWTX + $NETWRAP ))
	fi
	RUNRX=$(( $NEWRX - $OLDRX ))
	RUNTX=$(( $NEWTX - $OLDTX ))
	echo "$LOGDT;$RUNRX;$RUNTX" >> $CURWRKDIR/IFACE/$IF.rt
done

## Now we need egress/ingress for the wan

if [ ! -e $CURWRKDIR/IFACE/$WAN.in ]; then
	if [ -e $LSTWRKDIR/IFACE/$WAN.in ]; then
		cp $LSTWRKDIR/IFACE/$WAN.in $CURWRKDIR/IFACE/
	fi
fi
if [ ! -e $CURWRKDIR/IFACE/$WAN.out ]; then
	if [ -e $LSTWRKDIR/IFACE/$WAN.out ]; then
		cp $LSTWRKDIR/IFACE/$WAN.out $CURWRKDIR/IFACE/
	fi
fi
if [ -e $CURWRKDIR/IFACE/$WAN.in ]; then
	mv $CURWRKDIR/IFACE/$WAN.in $CURWRKDIR/IFACE/$WAN.in.old
else
	echo 0 > $CURWRKDIR/IFACE/$WAN.in.old
fi
if [ -e $CURWRKDIR/IFACE/$WAN.out ]; then
	mv $CURWRKDIR/IFACE/$WAN.out $CURWRKDIR/IFACE/$WAN.out.old
else
	echo 0 > $CURWRKDIR/IFACE/$WAN.out.old
fi
cat /sys/class/net/$WAN/statistics/rx_bytes > $CURWRKDIR/IFACE/$WAN.in
cat /sys/class/net/$WAN/statistics/tx_bytes > $CURWRKDIR/IFACE/$WAN.out
NEWRX=$(cat $CURWRKDIR/IFACE/$WAN.in)
NEWTX=$(cat $CURWRKDIR/IFACE/$WAN.out)
OLDRX=$(cat $CURWRKDIR/IFACE/$WAN.in.old)
OLDTX=$(cat $CURWRKDIR/IFACE/$WAN.out.old)
if [[ $NEWRX -lt $OLDRX ]]; then
	NEWRX=$(( $NEWRX + $NETWRAP ))
fi
if [[ $NEWTX -lt $OLDTX ]]; then
	NEWTX=$(( $NEWTX + $NETWRAP ))
fi
RUNRX=$(( $NEWRX - $OLDRX ))
RUNTX=$(( $NEWTX - $OLDTX ))
echo "$LOGDT;$RUNRX;$RUNTX" >> $CURWRKDIR/IFACE/$WAN.rt

## Whew!! that should provide us all the raw data we need to generate some totals and put a report together.
## We want all of the above to run frequently enough that neither of the *WRAP variables is exceeded
## From this point, we start calculating daily totals and generating reports

## We only want to generate totals once per day.  In this case, when CURHOUR is now.
## Make sure to not delete the matching fi statement at the end of the script:

if [[ $CURHOUR == $TTLNOW ]]; then

## First lets generate totals from our interface data.

for TIF in $(ls $CURWRKDIR/IFACE/*.rt); do
	while read ETH; do
		RAWIN=$(echo $ETH | cut -f 2 -d \;)
		RAWOUT=$(echo $ETH | cut -f 3 -d \;)
		TTLIN=$(( $TTLIN + $RAWIN ))
		TTLOUT=$(( $TTLOUT + $RAWOUT ))
	done < $TIF
	DTTL=$(( $TTLIN + $TTLOUT ))
	IFN=$(basename $TIF .rt)
	echo "$IFN;$TTLIN;$TTLOUT;$DTTL" >> $CURWRKDIR/ttl.iface
done

## This will total up the ipt count for the external interface

while read TWAN; do
	CNTWAN=$(echo $TWAN | cut -f 4 -d \;)
	TTLWAN=$(echo "$TTLWAN + $CNTWAN" | bc)
done < $CURWRKDIR/WAN/wan.rt
echo $TTLWAN > $CURWRKDIR/ttl.wan

## Now we need to bring everythign together for the internal interface

for NUM in $(seq $NETNUM); do
	STIPTOUT=0
	STIPTIN=0
	STIPT=0
	LAN=$(eval echo \$LAN$NUM)
	NNM=$(eval echo \$NNM$NUM)
	for TLAN in $(ls $CURWRKDIR/$LAN/*.rt); do
		TIPTIN=0
		TIPTOUT=0
		while read IPT; do
			IPTIN=$(echo $IPT | cut -f 2 -d \;)
			IPTOUT=$(echo $IPT | cut -f 3 -d \;)
			TIPTIN=$(( $TIPTIN + $IPTIN ))
			TIPTOUT=$(( $TIPTOUT + $IPTOUT ))
		done < $TLAN
		TIPT=$(( $TIPTIN + $TIPTOUT ))
		TFN=$(basename $TLAN .rt)
		echo "$TFN;$TIPTIN;$TIPTOUT;$TIPT" >> $CURWRKDIR/rpt.$NNM
		STIPTIN=$(( $STIPTIN + $TIPTIN ))
		STIPTOUT=$(( $STIPTOUT + $TIPTOUT ))
	done
	STIPT=$(( $STIPTIN + $STIPTOUT ))
	echo "$LAN;$STIPTIN;$STIPTOUT;$STIPT" >> $CURWRKDIR/ttl.lan
done

## Now we need to create some month-to-date stats

for MTD in $(ls $WRKDIR/$YR/$MNTH); do
	MTDIPTWAN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.wan)
	TMTDIPTWAN=$(echo "$TMTDIPTWAN + $MTDIPTWAN" | bc)
	MTDRAWWANIN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $WAN | cut -f 2 -d \;)
	TMTDRAWWANIN=$(echo "$TMTDRAWWANIN + $MTDRAWWANIN" | bc)
	MTDRAWWANOUT=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $WAN | cut -f 3 -d \;)
	TMTDRAWWANOUT=$(echo "$TMTDRAWWANOUT + $MTDRAWWANOUT" | bc)
	MTDRAWWAN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $WAN | cut -f 4 -d \;)
	TMTDRAWWAN=$(echo "$TMTDRAWWAN + $MTDRAWWAN" | bc)
	echo "IPT;$TMTDIPTWAN;RAW;$TMTDRAWWANIN;$TMTDRAWWANOUT;$TMTDRAWWAN" > $CURWRKDIR/mtd.$WAN
	for NUM in $(seq $NETNUM); do
		IF=$(eval echo \$IF$NUM)
		MTDIFIN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $IF | cut -f 2 -d \;)
		TMTDIFIN=$(echo "$TMTDIFIN + $MTDIFIN" | bc)
		MTDIFOUT=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $IF | cut -f 3 -d \;)
		TMTDIFOUT=$(echo "$TMTDIFOUT + $MTDIFOUT" | bc)
		MTDIF=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.iface | grep $IF | cut -f 4 -d \;)
		TMTDIF=$(echo "$TMTDIF + $MTDIF" | bc)
		echo "$TMTDIFIN;$TMTDIFOUT;$TMTDIF" > $CURWRKDIR/mtd.$IF
	done
	for NUM in $(seq $NETNUM); do
		LAN=$(eval echo \$LAN$NUM)
		MTDLANIN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.lan | grep $LAN | cut -f 2 -d \;)
		TMTDLANIN=$(echo "$TMTDLANIN + $MTDLANIN" | bc)
		MTDLANOUT=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.lan | grep $LAN | cut -f 3 -d \;)
		TMTDLANOUT=$(echo "$TMTDLANOUT + $MTDLANOUT" | bc)
		MTDLAN=$(cat $WRKDIR/$YR/$MNTH/$MTD/ttl.lan | grep $LAN | cut -f 4 -d \;)
		TMTDLAN=$(echo "$TMTDLAN + $MTDLAN" | bc)
		echo "$TMTDLANIN;$TMTDLANOUT;$TMTDLAN" > $CURWRKDIR/mtd.$LAN
	done
done

## Now that we have totals, we will make a few final calculations to put in our report.
## The lan parts of the report will be calculated and reported on the fly so they can be in loops
## First thing; Percentage of allowable bandwidth consumed this month

ABITS=$(( $USAGE * $DIV * $DIV * $DIV ))
UBITS=$(cat $CURWRKDIR/mtd.$WAN | cut -f 6 -d \;)
PBITS=$(echo "scale=2; $UBITS * 100 / $ABITS" | bc)

## Convert daily WAN stats to MB:

IWANRAW=$(cat $CURWRKDIR/ttl.iface | grep $WAN | cut -f 2 -d \;)
IWANRAWMB=$(echo "$IWANRAW / $DIV / $DIV" | bc)
OWANRAW=$(cat $CURWRKDIR/ttl.iface | grep $WAN | cut -f 3 -d \;)
OWANRAWMB=$(echo "$OWANRAW / $DIV / $DIV" | bc)
TWANRAW=$(cat $CURWRKDIR/ttl.iface | grep $WAN | cut -f 4 -d \;)
TWANRAWMB=$(echo "$TWANRAW / $DIV / $DIV" | bc)
WANIPT=$(cat $CURWRKDIR/ttl.wan)
WANIPTMB=$(echo "$WANIPT / $DIV / $DIV" | bc)

## Convert mtd WAN stats to MB

IMTDWANRAW=$(cat $CURWRKDIR/mtd.$WAN | cut -f 4 -d \;)
IMTDWANRAWMB=$(echo "$IMTDWANRAW / $DIV / $DIV" | bc)
OMTDWANRAW=$(cat $CURWRKDIR/mtd.$WAN | cut -f 5 -d \;)
OMTDWANRAWMB=$(echo "$OMTDWANRAW / $DIV / $DIV" | bc)
TMTDWANRAW=$(cat $CURWRKDIR/mtd.$WAN | cut -f 6 -d \;)
TMTDWANRAWMB=$(echo "$TMTDWANRAW / $DIV / $DIV" | bc)
MTDWANIPT=$(cat $CURWRKDIR/mtd.$WAN | cut -f 2 -d \;)
MTDWANIPTMB=$(echo "$MTDWANIPT / $DIV / $DIV" | bc)

## Let's start the report by generating a header and appending it to today's report file:

echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "Bandwidth Report generated at end of this day:		$YR-$MNTH-$DAY" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "SUMMARY" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
echo "Percentage of allowable bandwidth usage this month :	$PBITS %" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
echo "Egress/Ingress(1) external usage today in MegaBytes:	$TWANRAWMB MB" >> $CURWRKDIR/daily.rpt
echo "Egress/Ingress(1) external usage this Month To Date:	$TMTDWANRAWMB MB" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
for NUM in $(seq $NETNUM); do
	LAN=$(eval echo \$LAN$NUM)
	NNM=$(eval echo \$NNM$NUM)
	LANIPT=$(cat $CURWRKDIR/ttl.lan | grep $LAN | cut -f 4 -d \;)
	LANIPTMB=$(echo "$LANIPT / $DIV / $DIV" | bc)
	MTDLANIPT=$(cat $CURWRKDIR/mtd.$LAN | cut -f 3 -d \;)
	MTDLANIPTMB=$(echo "$MTDLANIPT / $DIV / $DIV" | bc)
	echo "Accounted(2) $NNM usage today in MegaBytes:		$LANIPTMB MB" >> $CURWRKDIR/daily.rpt
	echo "Accounted(2) $NNM usage this Month To Date:		$MTDLANIPTMB MB" >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt
done
echo "" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "External Network - Upload/Download(3) Detailed Report" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s %-20s\n" "Detail" "DOWN" "UP" "TOTAL" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s %-20s\n" "Egress/Ingress(1) Today (MB): " "$IWANRAWMB" "$OWANRAWMB" "$TWANRAWMB" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $IWANRAW $OWANRAW $TWANRAW >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s %-20s\n" "Egress/Ingress(1) This Month: " "$IMTDWANRAWMB" "$OMTDWANRAWMB" "$TMTDWANRAWMB" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $IMTDWANRAW $OMTDWANRAW $TMTDWANRAW >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
echo "" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s\n" "Detail(4)" "BYTES:" "MEGABYTES:" >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s\n" "Accounted(2)         Today: " $WANIPT $WANIPTMB >> $CURWRKDIR/daily.rpt
printf "%-33s %-16s %-16s\n" "Accounted(2)    This Month: " $MTDWANIPT $MTDWANIPTMB >> $CURWRKDIR/daily.rpt

## set up a loop so each network segment can be reported:

for NUM in $(seq $NETNUM); do
	LAN=$(eval echo \$LAN$NUM)
	IF=$(eval echo \$IF$NUM)
	NNM=$(eval echo \$NNM$NUM)

	## This pile of variables will grab the numbers from the data for each network

	ILANRAW=$(cat $CURWRKDIR/ttl.iface | grep $IF | cut -f 2 -d \;)
	ILANRAWMB=$(echo "$ILANRAW / $DIV / $DIV" | bc)
	OLANRAW=$(cat $CURWRKDIR/ttl.iface | grep $IF | cut -f 3 -d \;)
	OLANRAWMB=$(echo "$OLANRAW / $DIV / $DIV" | bc)
	TLANRAW=$(cat $CURWRKDIR/ttl.iface | grep $IF | cut -f 4 -d \;)
	TLANRAWMB=$(echo "$TLANRAW / $DIV / $DIV" | bc)
	ILANIPT=$(cat $CURWRKDIR/ttl.lan | grep $LAN | cut -f 2 -d \;)
	ILANIPTMB=$(echo "$ILANIPT / $DIV / $DIV" | bc)
	OLANIPT=$(cat $CURWRKDIR/ttl.lan | grep $LAN | cut -f 3 -d \;)
	OLANIPTMB=$(echo "$OLANIPT / $DIV / $DIV" | bc)
	TLANIPT=$(cat $CURWRKDIR/ttl.lan | grep $LAN | cut -f 4 -d \;)
	TLANIPTMB=$(echo "$TLANIPT / $DIV / $DIV" | bc)
	IMTDLANRAW=$(cat $CURWRKDIR/mtd.$IF | cut -f 1 -d \;)
	IMTDLANRAWMB=$(echo "$IMTDLANRAW / $DIV / $DIV" | bc)
	OMTDLANRAW=$(cat $CURWRKDIR/mtd.$IF | cut -f 2 -d \;)
	OMTDLANRAWMB=$(echo "$OMTDLANRAW / $DIV / $DIV" | bc)
	TMTDLANRAW=$(cat $CURWRKDIR/mtd.$IF | cut -f 3 -d \;)
	TMTDLANRAWMB=$(echo "$TMTDLANRAW / $DIV / $DIV" | bc)
	IMTDLANIPT=$(cat $CURWRKDIR/mtd.$LAN | cut -f 1 -d \;)
	IMTDLANIPTMB=$(echo "$IMTDLANIPT / $DIV / $DIV" | bc)
	OMTDLANIPT=$(cat $CURWRKDIR/mtd.$LAN | cut -f 2 -d \;)
	OMTDLANIPTMB=$(echo "$OMTDLANIPT / $DIV / $DIV" | bc)
	TMTDLANIPT=$(cat $CURWRKDIR/mtd.$LAN | cut -f 3 -d \;)
	TMTDLANIPTMB=$(echo "$TMTDLANIPT / $DIV / $DIV" | bc)

	## And now we can present that in a nice little table for each network

	echo "" >> $CURWRKDIR/daily.rpt
	echo "========================================================================" >> $CURWRKDIR/daily.rpt
	echo "$NNM Network - Upload/Download(3) Detailed Report" >> $CURWRKDIR/daily.rpt
	echo "========================================================================" >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "Detail" "DOWN" "UP" "TOTAL" >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "Egress/Ingress(1) Today (MB): " $OLANRAWMB $ILANRAWMB $TLANRAWMB >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $OLANRAW $ILANRAW $TLANRAW >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "Egress/Ingress(1) This Month: " $OMTDLANRAWMB $IMTDLANRAWMB $TMTDLANRAWMB >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $OMTDLANRAW $IMTDLANRAW $TMTDLANRAW >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "Accounted(2)      Today (MB): " $OLANIPTMB $ILANIPTMB $TLANIPTMB >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $OLANIPT  $ILANIPT $TLANIPT >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "Accounted(2)      This Month: " $OMTDLANIPTMB $IMTDLANIPTMB $TMTDLANIPTMB >> $CURWRKDIR/daily.rpt
	printf "%-33s %-16s %-16s %-20s\n" "                    In Bytes: " $OMTDLANIPT $IMTDLANIPT $TMTDLANIPT >> $CURWRKDIR/daily.rpt	
	echo "" >> $CURWRKDIR/daily.rpt
	echo "" >> $CURWRKDIR/daily.rpt

	## Now we have to chop up the rpt.* files and present them in the report.

	printf "%-20s %-20s %-10s %-10s %-15s\n" "Host:" "IP:" "DOWN:" "UP:" "TOTAL:" >> $CURWRKDIR/daily.rpt
#	echo "Host:			IP:			DOWN:		UP:			TOTAL:" >> $CURWRKDIR/daily.rpt
	while read RPT; do
		IP=$(echo $RPT | cut -f 1 -d \;)
		IPIN=$(echo $RPT | cut -f 2 -d \;)
		IPINMB=$(echo "$IPIN / $DIV / $DIV" | bc)
		IPOUT=$(echo $RPT | cut -f 3 -d \;)
		IPOUTMB=$(echo "$IPOUT / $DIV / $DIV" | bc)
		IPTOT=$(echo $RPT | cut -f 4 -d \;)
		IPTOTMB=$(echo "$IPTOT / $DIV / $DIV" | bc)
		
		## Try some hostname detection

		if [[ $IPTOTMB != "0" ]]; then
			HST=$($DONBT -s \! $IP | cut -f 2 -d \!)
			if [[ $HST == "" ]]; then
				HST="Not Available"
			fi
#			echo "$HST		$IP		$IPOUTMB		$IPINMB			$IPTOTMB" >> $CURWRKDIR/daily.rpt
			printf "%-20s %-20s %-10s %-10s %-15s\n" "$HST" $IP "$IPOUTMB MB" "$IPINMB MB" "$IPTOTMB MB" >> $CURWRKDIR/daily.rpt
			echo "" >> $CURWRKDIR/daily.rpt
		fi
	done < $CURWRKDIR/rpt.$NNM
done
echo "" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "Foot Notes" >> $CURWRKDIR/daily.rpt
echo "========================================================================" >> $CURWRKDIR/daily.rpt
echo "(1) Egress is outgoing, Ingress is incoming" >> $CURWRKDIR/daily.rpt
echo "    This is a measure of bits passed between the firewall and the cable" >> $CURWRKDIR/daily.rpt
echo "    This number should match your ISP count" >> $CURWRKDIR/daily.rpt
echo "(2) Accounted traffic passes into or out of the firewall packet filter, and is" >> $CURWRKDIR/daily.rpt
echo "    expected to always be a little bit less than Egress/Ingress.  Unlike" >> $CURWRKDIR/daily.rpt
echo "    Egress and Ingress, this traffic can be filtered and therefor used to" >> $CURWRKDIR/daily.rpt
echo "    track usage by individuals on the LANs" >> $CURWRKDIR/daily.rpt
echo "(3) Upload/Download for this report relate the end user and are not from" >> $CURWRKDIR/daily.rpt
echo "    the point of view of the firewall.  In other words, Upload from the" >> $CURWRKDIR/daily.rpt
echo "    firewall to a LAN user will be reported as Download" >> $CURWRKDIR/daily.rpt
echo "(4) Accounted traffic on the external port does not differentiate direction." >> $CURWRKDIR/daily.rpt
echo "    Any packet from any IP address will be counted on the external port" >> $CURWRKDIR/daily.rpt

## At last, all we have gathered data all day, generated some interesting information, and created a report.  
## No good leaving the report on the server, though, let's mail it to someone, just for fun...

env MAILRC=/dev/null from=$MLFRM mailx -n -s "Daily Bandwidth Report - $PBITS% Used this Month" $ADMINML < $CURWRKDIR/daily.rpt

## This closes the if statement that determines if the daily totals should be calculated.  DO NOT DELETE THIS LINE:

fi