#!/usr/bin/perl

$IP_DIR="/home/ipaudit";
# List of allowed files and full path
%IsAllowed = (
"traffic",  "$IP_DIR/reports/30min/graphic/ReportTraffic.log",
"localhost",     "$IP_DIR/reports/30min/graphic/ReportLocalHost.log",
"remotehost",   "$IP_DIR/reports/30min/graphic/ReportRemoteHost.log",
"external", "$IP_DIR/reports/30min/graphic/ReportExternal.log",
);

%Title= (
'traffic',  'Internet Traffic',
'host',     'Possible Incoming Scans',
'remotehost',     'Possible Outgoing Scans',
'external', 'Backbone Internal/External Traffic'
);


&Usage unless @ARGV;

sub Usage {
print<<EOM;
Content-type: text/html

<html><head><title>No File Specified</title></head>
<body bgcolor=white>
<tt>No File Specified</tt>
</body></html>
EOM
exit;
}

&Usage unless $IsAllowed{$ARGV[0]};

print<<"EOM";
Content-type: text/html

<html><head><title>$Title{$ARGV[0]} - Raw Data ($ARGV[1])</title></head>
<body bgcolor=white>
<tt><font size+2>$Title{$ARGV[0]} - Raw Data ($ARGV[1])</font></tt>
<hr nospace>
<p>
<pre>
EOM


#  Determine if reading entire file or just beginning or end
if ($ARGV[1]>0) {
	#  Open 'head' output to get beginning lines
	open (FILE, "head -$ARGV[1] $IsAllowed{$ARGV[0]} |")
		or  die "ERROR:  Cannot open file<br></body></html>\n";
} elsif ($ARGV[1]<0) {
	#  Open 'tail' output to get last lines
	open (FILE, "tail $ARGV[1] $IsAllowed{$ARGV[0]} |")
		or  die "ERROR:  Cannot open file<br></body></html>\n";
} else {
	#  Open entire file
	open (FILE, $IsAllowed{$ARGV[0]})
		or  die "ERROR:  Cannot open file<br></body></html>\n";
}


while (<FILE>) {
	@F = split;
	print "<a href=", &getURL($F[0]), ">$F[0]</a>";
	for ($i=1;$i<@F;$i++) {
		#  Use commas if field is a number
		if ($F[$i]=~/^\d+$/) {
			printf " %15s", &ic($F[$i]);
		} else {
			printf " %15s", $F[$i];
		}
	}
	print "\n";
}
close FILE;

print "</pre></body></html>\n";

exit;


sub getURL {
	my ($date) = @_;
	my ($URL0) = 
		"/cgi-bin/ipaudit/ipahttp?30min/0traffic+";
	my ($URL1) = ".html";
	return $URL0 . $date . $URL1;
}

#
#  Convert positive whole number from nnnnnn to n,nnn,nnn etc.
#
sub ic {
	my ($string) = @_;
	1 while $string=~s/(\d)(\d\d\d)(?!\d)/$1,$2/g;
	return $string;
}
