#!/usr/bin/perl
#
#  Search ipaudit LocalHost daily files for total traffic 
#  for particular ip and date
#

$DAILY_LIMIT=500_000_000;
$WEEKLY_LIMIT=1_500_000_000;
$DAILY_DIR="/home/ipaudit/data/daily/localhost";
$WEEKLY_DIR="/home/ipaudit/data/weekly/localhost";
$ZGREP="/usr/bin/zgrep";
$TITLE="IPAUDIT - Automatic IP Lookup";
$DAILY_COLOR ="#ddeedd";
$WEEKLY_COLOR="#ddddee";
$ERROR_COLOR="#eecccc";

#  Get ip address from environment
$ipf  = sprintf "%03d.%03d.%03d.%03d", split(/\./, $ENV{REMOTE_ADDR});


#  Get date from stdin
for (split/&/, $ENV{QUERY_STRING}) {
	s/\+/ /g;
	@code = /%([0-9a-fA-F]{2})/g;
	for $c (@code) {
		$v = chr(eval "0x$c");
		s/%$c/$v/g;
	}
	($key,$val) = split (/=/, $_);
	$val =~ s/\+/ /g;
	$input{$key} = $val;
}


#  If no date entered then use yesterday
if (defined($input{date}) && $input{date} ne "") {
	$date = `date -d "$input{date}" +"%Y-%m-%d"`;
} else {
	$date = `date -d "1 day ago" +"%Y-%m-%d"`;
}

chomp $date;
$display_date = `date -d $date +"%a %b %e, %Y"`;
chomp $display_date;
$field_date = `date -d $date +"%b %e, %Y"`;
chomp $field_date;


#  Get data for data and 6 previous days
$weekcum = 0;
$atSunday=0;
for ($day=0;$day<7;$day++) {
	$cur = `date -d "$date $day day ago" +"%Y-%m-%d"`;
	chomp $cur;
	$res = `$ZGREP $ipf $DAILY_DIR/$cur*`;
	chomp $res;
	(undef, $in, $out) = split (/\s+/, $res);
	if (defined($in) && defined($out)) {
		$tot = $in+$out;
		$inf  = &ic($in);
		$outf = &ic($out);
		$totf = &ic($tot);
	} else {
		$inf  = "(missing data)";
		$outf = "(missing data)";
		$totf = "(missing data)";
	}
	@{$daily{$cur}} = ($inf, $outf, $totf, $tot);

	# Calculate traffic so far for this calendar week
	next if $atSunday;
	$weekcum += $tot;
	$dow = `date -d "$date $day day ago" +"%w"`;
	$atSunday = ($dow==0);
}

#  Get data for past 4 weeks
$sunday = &get_sunday($date);

for ($day=0;$day<=21;$day+=7) {
	$cur = `date -d "$sunday $day day ago" +"%Y-%m-%d"`;
	chomp $cur;
	$res = `$ZGREP $ipf $WEEKLY_DIR/$cur*`;
	chomp $res;
	(undef, $in, $out) = split (/\s+/, $res);
	if (defined($in) && defined($out)) {
		$tot = $in+$out;
		$inf  = &ic($in);
		$outf = &ic($out);
		$totf = &ic($tot);
	} else {
		$inf = "(missing data)";
		$outf = "(missing data)";
		$totf = "(missing data)";
	}
	@{$weekly{$cur}} = ($inf, $outf, $totf, $tot);
}

#
#  Web page output starts here
#

open HTML, "> -";

#  Print page heading
print HTML <<"EOM";
Content-type: text/html

<html><head><title>$TITLE</title></head>
<body bgcolor=white>
<h2 align=center>$TITLE</h2>
<hr noshade>
<table bgcolor=#dddddd cellpadding=5 cellspacing=5 border=0>
<tr><th align=left>Your IP Address</th><td align=right><tt>$ipf</tt></td></tr>
EOM


if ($weekcum>$WEEKLY_LIMIT) {
$weekcum = &ic($weekcum);
print HTML <<"EOM";
<tr><th align=left>Weekly Traffic So Far</th><td bgcolor=$ERROR_COLOR align=right><tt>$weekcum</tt></td></tr>
<tr><th align=left>Remaining Allocation</th><td bgcolor=$ERROR_COLOR align=right><tt>0</tt></td></tr>
</table>
EOM
} else {
$weekrem = $WEEKLY_LIMIT-$weekcum;
$weekcum = &ic($weekcum);
$weekrem = &ic($weekrem);
print HTML <<"EOM";
<tr><td align=left><b>Weekly Traffic So Far</b><br>(Excluding today)</td><td align=right><tt>$weekcum</tt></td></tr>
<tr><th align=left>Remaining Allocation</th> <td align=right><tt>$weekrem</tt></td></tr>
</table>
EOM
}

# Print daily table
$dlf = &ic($DAILY_LIMIT);
print HTML <<"EOM";
<hr noshade>
<table bgcolor=$DAILY_COLOR cellpadding=5 cellspacing=5 border=0>
<tr><td colspan=4 align=center><b>DAILY TRAFFIC (bytes)</b><br>(Daily Limit = $dlf)</td></tr>
<tr>
<th align=left>Date</th>
<th align=left>Incoming</th>
<th align=left>Outgoing</th>
<th align=left>Total</th>
</tr>
EOM

for (sort keys %daily) {
	($inf, $outf, $totf, $tot) = @{$daily{$_}};
	$format_date = `date -d $_ +"%a, %b %e, %Y"`;
	chomp $format_date;
	print HTML "<tr><td><tt>&nbsp;$format_date&nbsp;</tt></td><td align=right><tt>$inf</tt></td><td align=right><tt>$outf</tt></td>\n";
	if ($tot>$DAILY_LIMIT) {
		print HTML "<td align=right bgcolor=$ERROR_COLOR><tt><b>$totf</b></tt></td></tr>\n";
	} else {
		print HTML "<td align=right><tt>$totf</tt></td></tr>\n";
	}
}

print HTML <<"EOM";
</table>
</body></html>
EOM



# Print weekly table
$wlf = &ic($WEEKLY_LIMIT);
print HTML <<"EOM";
<hr noshade>
<table bgcolor=$WEEKLY_COLOR cellpadding=5 cellspacing=5 border=0>
<tr>
<td colspan=4 align=center>
<b>WEEKLY TRAFFIC (bytes)</b> <br>
(Weekly Limit = $wlf)<br>
(Sunday through Saturday)
</td></tr>
<tr>
<th align=left>Date</th>
<th align=left>Incoming</th>
<th align=left>Outgoing</th>
<th align=left>Total</th>
</tr>
EOM

for (sort keys %weekly) {
	($inf, $outf, $totf, $tot) = @{$weekly{$_}};
	$format_date = `date -d $_ +"%a, %b %e, %Y"`;
	chomp $format_date;
	print HTML "<tr><td><tt>&nbsp;$format_date&nbsp;</tt></td><td align=right><tt>$inf</tt></td><td align=right><tt>$outf</tt></td>\n";
	if ($tot>$WEEKLY_LIMIT) {
		print HTML "<td align=right bgcolor=$ERROR_COLOR><tt><b>$totf</b></tt></td></tr>\n";
	} else {
		print HTML "<td align=right><tt>$totf</tt></td></tr>\n";
	}
}

print HTML <<"EOM";
</table>
<hr noshade>
</body></html>
EOM


close HTML;


exit;

#-----------------------------------------------------------------------
#  Subroutines
#-----------------------------------------------------------------------

#  Add commas to number
sub ic {
	my ($x) = @_;
	1 while ($x=~s/(\d)(\d\d\d)(?!\d)/$1,$2/g);
	return $x;
}


#  Get date of TWO sundays ago
sub get_sunday {
	my ($cur) = @_;
	my ($day);
	$day = `date -d $cur +"%w"`;
	chomp $day;
	$day += 7;
	$date = `date -d "$cur $day day ago" +"%Y-%m-%d"`;
	chomp $date;
	return $date;
}
