#!/usr/bin/perl

# Title: iptables-accounting.pl 
# Theory:  All packets from uplink (eth0 in my case) pass through FORW-IN
#	   Everything leaving goes through FORW-OUT
#	   Kernel uses them to keep statistics later retrieved by MRTG
#
# Prerequesit: 
#	Add this to the iptables scritp
#	  $iptables -N FORW-IN
#	  $iptables -N FORW-OUT
#	  $iptables -A FORWARD -i eth0 -j FORW-IN
#	  $iptables -A FORWARD -o eth0 -j FORW-OUT
#
#	Created a iptables-accounting.mrtg.conf
#         Options[_]: bits
#         WorkDir: /usr/local/apache/mrtg.solidusdesign.com/htdocs       
#         Target[iptables]: `/usr/local/mrtg-2/iptables-accounting`
#         MaxBytes[iptables]: 1250000
#         Title[iptables]: Interface Forwarding Stats
#         PageTop[iptables]: <h1>Interface Forwarding Stats</h1>
#
#	run mrtg
#	mrtg iptables-accounting.mrtg.cfg --logging logs/mrtg.log
  


$iptables = "/usr/sbin/iptables";   # Adjust these to fit your site
$uptime = "/usr/bin/uptime";
$host = "sherman.solidusdesign.net";

@chain=("FORW-IN", "FORW-OUT");

foreach (@chain) {
  $_=`$iptables -nvxL | grep $_ | grep -v Chain`;
  s/^\s+\d+\s+(\d+).*$/$1\n/s;
  print;
  } 
($uptime = `$uptime`) =~ s/^.*up (.*,[\d: ]+),.*$/$1/s;
print "$uptime\n$host\n";
   
