#!/usr/bin/perl
#
# Run this periodicaly by cron as root
#

#
# RRSTATS
#
# author:	Adam Pribyl, covex@ahoj.fsik.cvut.cz 
# url: 		http://bbs.cvut.cz/~covex/czfree/index.html#rrd [CZ]
# desc: 	perl scripts for monitoring trafic for each IP on given 
#		interface
# deps:		perl, iptables >= 1.2.7a, kernel >= 2.4.20
# lic:		GNU GPL (whole package)
#
# see rrstats.conf for some configuration details

# you can replace this with
# $iface = $ARGV[0];		# then call rrstats with_iface_as_parametr
$iface = "wlan1";		# for which interface statistic should be generated

require "./rrstats.conf";

# check wherether we have dirs for iface
if ( !( -e $wwwdir.$iface."/")) { mkdir ( $wwwdir.$iface ) || die "$wwwdir";};
if ( !(-e $rrdstore.$iface."/")) { mkdir ( $rrdstore.$iface ) || die "$rrdstore" };

$rrdstore = $rrdstore.$iface."/";
$wwwdir = $wwwdir.$iface."/";


# generate iface statistic (using ifconfig)
system("./rrstats_iface $iface");
system("./rrstats_iface wlan0"); #hack to display wlan0 stats

# generate ip statistic (using iptables)
### do not put any non .rrd files in $rrdstore!
@sbd = `ls -1 $rrdstore | grep -v base | grep rrd | cut -d\. -f1-4`;
for (@sbd) {s/_/\//g}; # replace _ with / - for filenames it must be _
if ($dis_arp == 0) {
    @arps = `$arpp | grep $iface | grep -v incomplete | cut -d\' ' -f1`;
} else {
#    print "Arp detection disabled.\n";
    @arps = ();
}

# merge arraries
@cip = sort (@sbd, @arps, @exip);
for (@cip) { chomp($_) };
# remove duplicate items
$prev = "not equal to $cip[0]";
@out = grep($_ ne $prev && ($prev = $_, 1), @cip);

#print @out;

# we want dynamicaly created html page for IP while they are changing
#print "$wwwdir/ipstats\.html\n";
open( IPSTAT, ">$wwwdir"."ipstats\.html") || die "ipstats.html";
print IPSTAT "<html>\n<head><title>$title</title></head>\n<body bgcolor=\"\#ffffff\">\n<center>\n<table width=700><tr><td>\n<img src=\"$gifimg\">\n</td></tr></table>\n<table width=700 cellpadding=5><tr><td valign=top>\n";

foreach $uipa ( @out ) {
#	chomp $uipa;
	# check wherether we have already RRD_IN and RRD_OUT in mangle
	@tmp = `$iptables -t mangle -L`;
	if ( grep(/RRD_IN/, @tmp) == 0) {
	    system("$iptables -t mangle -N RRD_IN 2> /dev/null");
	    system("$iptables -t mangle -A RRD_IN -j RETURN");
	    system("$iptables -t mangle -A PREROUTING -j RRD_IN 2>/dev/null");
	}
	if ( grep(/RRD_OUT/, @tmp) == 0) {    
	    system("$iptables -t mangle -N RRD_OUT 2> /dev/null");
	    system("$iptables -t mangle -A RRD_OUT -j RETURN");
	    system("$iptables -t mangle -A POSTROUTING -j RRD_OUT 2>/dev/null");
	}
	# check if RRDs are in chain
	if ( grep(/RRD_IN/, @tmp) == 1) {
	    system("$iptables -t mangle -A PREROUTING -j RRD_IN 2>/dev/null");
	}
	if ( grep(/RRD_OUT/, @tmp) == 1) {    
	    system("$iptables -t mangle -A POSTROUTING -j RRD_OUT 2>/dev/null");
	}
	

	# check if in iptables is filter for IP address we are processing
	@tmp = `$iptables -t mangle -L RRD_IN -vn`;
	if ( (grep(/^$uipa$/i, @sbd) == 0) || (grep(/\s$uipa\s/i, @tmp) == 0) ) {
	    # we need to add iptable filter - need recheck
	    system("$iptables -t mangle -I RRD_IN -i $iface -s $uipa -j $WHERE_IN");
	    system("$iptables -t mangle -I RRD_OUT -o $iface -d $uipa -j $WHERE_OUT");
	}	    
#	print "$uipa\n";
    # generate IP statistic
    system ("./rrstats_ip $uipa $iface");
    # add IP to html page
    if ( grep(/^$uipa$/i, @arps) == 1 ) { $status = "ONLINE" } elsif ( $dis_arp == 0) { $status = "OFFLINE" } else { $status = "NOTDET" };
    if ( $uipa =~ /\// ) { $status = "SUBNET"; $uipa2 = $uipa; $uipa2 =~ s/\//_/g; }
    else {$uipa2 = $uipa }
    if ( -e $dhcpconf ) { 
	open( DHCPC, $dhcpconf);
	while ($radek = <DHCPC>) { 
	    if ($radek =~ /$uipa\;/ && $radek !~ /^\#/) { 
		@rad = split(' ',$radek);
		$uipa = "$rad[1] $uipa";
		break;
	    }
	};
	close DHCPC;
    }
    print IPSTAT "Prenos ip <b>$uipa : $status</b> na <i>$iface</i> za poslednich 24hodin<br>\n<img src=\"$uipa2\.gif\">\n<p>Prenos ip <b>$uipa</b> za poslednich 30dni<br>\n<img src=\"$uipa2\.30d\.gif\">\n<p>\n<hr width=60%>\n"; 
}

# finish html page
print IPSTAT "</td></tr><tr><td><font size=\"-1\">generated by - :<A HREF=\"http://bbs.cvut.cz/~covex/czfree/#rrd\">rrstats</a> : -</font></td></tr></table>\n</center>\n</body></html>";
close IPSTAT;

# zero all counter in RRD_IN and RRD_OUT
system("$iptables -t mangle -L RRD_IN -Z > /dev/null; $iptables -t mangle -L RRD_OUT -Z > /dev/null");

# monitor load
system("./rrstats_load");
