package tracker;
require CGI;
require tracker;


# -------------- Configurable variables start --------------

$tracker_header = 
	CGI::header(-type=>'text/html', -expires=>'now') . 
	CGI::start_html(-title=> "Tracker",
	-style=>( {-src=>'/net-css/normal.css' } ));

$tracker_footer = CGI::end_html();

@domains = (
);

#comment out any definition of $nmap to disable the nmap functionality in
#host.pl
#The -O option to nmap requires root.  This one works if you have an suid
#copy of nmap in the tracker home direcrory
$nmap = $tracker::home_dir . "nmap -O";

#This should work if you have nmap in the path for the cgi script
#$nmap = "nmap"

# -------------- Configurable variables end ----------------
	
sub start_node_table
	{
	print "<center><h3><table border=1>\n";
	print CGI::Tr( CGI::td( [ "Name", "IP address", "MAC address", "attached to", 
		"Vlan", "updated" ] ) );
	print "</h3>\n"
	}
	
sub start_swport_table
	{
	print "<center><h3><table border=1>\n";
	print CGI::Tr ( CGI::td( [ "Port", "PhyType", "Status", 
		"Speed", "Duplex", "vlan", "Input counter", "Last input act." ] ) );
	print "</h3>\n"
	}


sub swport_row
	{
	my ($switch,$module,$port,$inoctets,$adminstatus,$adminspeed,$adminduplex,
		$vlan,$phytype,$last_input_act) = @_;
	my $speedM = $adminspeed / 1000000;

	print CGI::Tr(
		CGI::td("$switch $module/$port"),
		CGI::td($phy_types[$phytype]),
		CGI::td($admin_states[$adminstatus]),
		CGI::td($speedM . " Mbps"),
		CGI::td($admin_duplex[$adminduplex]),
		CGI::td($vlan),
		CGI::td($inoctets),
		CGI::td($last_input_act)
		)
	}
	

sub node_row
	{
	my ($hostname, $mac_addr, $switch, $vlan, $module, $port, 
		$ip_addr, $last_updated ) = @_;
	my $switch_port;
	if (length($switch)) { $switch_port = "$switch $module/$port" }
	print CGI::Tr (
		CGI::td(CGI::a( { -href=>"host.pl?node=$hostname" }, $hostname ) ),
		CGI::td(CGI::a( { -href=>"host.pl?node=$ip_addr" }, $ip_addr ) ),
		CGI::td( $mac_addr ),
		CGI::td(CGI::a( { -href=>"find_swport.pl?swport=$switch\%20$module/$port" }, $switch_port ) ),
		CGI::td( $vlan ),
		CGI::td( $last_updated ),
		)
	}	



sub end_table
	{ print "</table></center>\n" }

sub tracker_end
	{ print "</body></html>\n" }

sub http_error
{
	my ($error) = @_;
	print CGI::h3($error), CGI::end_html()
	}


#print a list of errors with red hr's above and below
sub html_errors
	{
	my $aref = shift;
	"\n<hr color='red'>\n". join(CGI::br(),@$aref). "\n<hr color='red'>\n"
	}

    
#Gracefully end the html document then die
sub cgi_die
	{
	my $error = shift;
	if ($error) { print html_errors([$error]) }
	print $tracker_footer, CGI::end_html();
	die $error
	}

1;
