#!/usr/bin/perl

#
# Monitors RX bytes and TX bytes using iptables for $ARGV ip
#
# creates database for $ARGV ip if it does not exist
# or adds values for actual time
#

if ( $ARGV[0] eq "" || $ARGV[1] eq "") { 
    print "usage: rrstats_ip <ipaddr> <iface>\n";
    exit 1;
}

$ipa = $ARGV[0];					# which IP
$iface = $ARGV[1];					# which iface

require "./rrstats.conf";

$ipa2 = $ipa;
$ipa2 =~ s/\//_/;					# for subnets 
$rrdb = $rrdstore.$iface."/".$ipa2;			# rrd for this IP
$wwwdir = $wwwdir.$iface."/";


if ( !( -e "$rrdb.rrd") ) {
	$tm = time;
	# every 600s (10min), 1200s maximum interval between feeding data
	# 1. AVERAGE, every 1st value, store 144 (6*24) values (1day)
	# 2. AVERAGE, every 6th value (each hour), store 720 values (30days)
	# same for MAX values
	system ("$rrdtoolp create $rrdb.rrd --start $tm -s 600 \\
			    DS:input:ABSOLUTE:1200:U:U \\
			    DS:output:ABSOLUTE:1200:U:U \\
			    RRA:AVERAGE:0.5:1:144 \\
			    RRA:AVERAGE:0.5:6:720 \\
			    RRA:MAX:0.5:1:144 \\
			    RRA:MAX:0.5:6:720");
#	print "Database created.\n";
	exit 0;
}


# read iptables
# this counts with only one line for ip = not more ifaces
@iptin = `$iptables -t mangle -L RRD_IN -vxn`;
@iptout = `$iptables -t mangle -L RRD_OUT -vxn`;

@uiptin =  grep(/\s$ipa\s/,@iptin);
@uiptout = grep(/\s$ipa\s/,@iptout);
#print "@uiptin\n";

if ( $uiptin[0] >= 0 ) {
    ($not, $rxb, @dnn) = split(" ",$uiptin[0]); # get bytes counter
    ($not, $txb, @dnn) = split(" ",$uiptout[0]); # get bytes counter
} else { 
    $rxb = 0; $txp = 0; # make sure we allways feed some number to rrd
}
    
$tm = time;
open (OLOG, ">>$wwwdir"."iplog.log");
#	print OLOG "@uiptin\n@uiptout\n";
	print OLOG "$tm $ipa $rxb $txb\n";
#	print OLOG "-----\n";
close OLOG;

system("$rrdtoolp update $rrdb.rrd $tm:$rxb:$txb");

if ( $avgmax eq "MAX" ) { 
    $vlabel = "B/10min"; $MB = "1MB/10min" 
} else { 
    $vlabel = "B/s"; $MB = "1Mb/s" 
};

#86400 = 24h
$img = $wwwdir.$ipa2.".gif";
$size1 = `$rrdtoolp graph $img --start -86400 -X 3 -i -b 1024 \\
		    --vertical-label $vlabel \\
		    DEF:inoctets=$rrdb.rrd:input:$avgmax \\
		    DEF:outoctets=$rrdb.rrd:output:$avgmax \\
		    AREA:inoctets#00FF00:\"Recieved by AP\"  \\
		    LINE1:outoctets#FF0000:\"Transmited by AP\" \\
		    HRULE:100000#0000FF:$MB`;

#25932000 = 30*86400
$img = $wwwdir.$ipa2.".30d.gif";
$size2 = `$rrdtoolp graph $img --start -2592000 -X 3 -i -b 1024 \\
		    --vertical-label $vlabel \\
		    DEF:inoctets=$rrdb.rrd:input:$avgmax \\
		    DEF:outoctets=$rrdb.rrd:output:$avgmax \\
		    AREA:inoctets#00FF00:\"Recieved by AP\" \\
		    LINE1:outoctets#FF0000:\"Transmited by AP\" \\
		    HRULE:100000#0000FF:$MB`;


# NOT FINISHED DO NOT ENABLE!
# problem: when ip is active but it does not transmit any data (e.g. routers)
# then database will created, deleted, created, deleted ...etc.
# check if database is not empty, if yes, delete it
# iptables we will not remove - these are removed at reboot
# this XML dumping is bit ugly :(
#@rrdfetch = `$rrdtoolp dump $rrdb.rrd AVERAGE | grep row | cut -d\' ' -f9| grep -v NaN | grep -v 0000000000e | grep ^[0-9]`; # input - IP must transmit otherwise is dead
#$flen = @rrdfetch; # length of arrary
#if ( ($flen == 0) ) {
#	print "Deleting $rrdb.rrd and $wwwdir.$ipa2\*.gif\n";	
#	system("rm -f $rrdb.rrd; rm -f $wwwdir.$ipa2\*.gif");
#	exit;
#}