#!/bin/bash
#
# netusage - This script gathers the stats for MRTG.
#
# Curt Rebelein, Junior
# fw@rebby.com
#

################################
### set the script variables ###
################################

IPT=/sbin/iptables	# where iptables lives
OUT=/root/fwtables.out	# where you want to the temporary output data to live
IN=/root/fwtables.in	# where you want to the temporary input data to live
OUTTABLE="FWD-OUT"	# name of your outgoing table
INTABLE="FWD-IN"	# name of your incoming table

##########################
### now run the script ###
##########################

case "$1" in
   -update)		# dump the chain stats to $OUT and $IN
      $IPT -nxvL $OUTTABLE > $OUT
      $IPT -nxvL $INTABLE > $IN
      chmod 0600 $IN $OUT
      ;;
   -packets)		# dump the packet count
      /bin/grep $2 $OUT | /bin/awk '{print $1}'	# incoming traffic
      /bin/grep $2 $IN | /bin/awk '{print $1}'	# outgoing traffic
      /bin/awk '/cpu /{printf("%.1f days\n",($2+$3+$4+$5)/8640000)}' < /proc/stat # uptime
      /bin/hostname	# hostname
      ;;
   -bytes)		# dump the byte count
      /bin/grep $2 $OUT | /bin/awk '{print $2}'	# incoming traffic
      /bin/grep $2 $IN | /bin/awk '{print $2}'	# outgoing traffic
      /bin/awk '/cpu /{printf("%.1f days\n",($2+$3+$4+$5)/8640000)}' < /proc/stat # uptime
      /bin/hostname	# hostname
      ;;
   -help)
      /bin/echo -e "Usage: netusage [OPTION [ARG]...]\nGather MRTG stats for perhost/network bandwidth monitoring.\n"
      /bin/echo "   -help               display this help file"
      /bin/echo "   -update             keep data up to date (run this from cron"
      /bin/echo "                        prior to running query w/MRTG)"
      /bin/echo "   -bytes <ip/net>     get byte count for host/net at <ip/net>"
      /bin/echo -e "   -packets <ip/net>   get packet count for host/net at <ip/net>\n"
      /bin/echo -e "Report bugs to Curt Rebelein, Junior <fw@rebby.com>."
      ;;
   *)
      cd `/bin/pwd`
      $0 -help
      exit 1
esac

exit 0

# we're done!!! :-)
