#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/route-stability.pl Last modified on Thu DEC 5 2002, h.22:05:28

# Routine to create the route stability page for the known 6bone pTLAs
# and sTLAs

sub create_stability_page {
	my ($filename, $type) = @_;

	my ($ptlaprefix, $viewlink, $unstab, $unav, $key, $name, $title, $history_reldir);

	if ($type eq "ptla") {$title = "6bone pTLAs: BGP route stability"}
	elsif ($type eq "stla") {$title = "sTLAs: BGP route stability"}
	else {return(1)}

	$history_reldir = substr($HISTORYHTMLDIR, length($HTMLDIR_LOCAL) + 1, length($HISTORYHTMLDIR) - length($HTMLDIR_LOCAL));

	open(HTMLFILE,">".$filename);

	print HTMLFILE &header($title, 0);

	print HTMLFILE "<TABLE>\n<TR>";
	if ($SITEPICTURE) {
		print HTMLFILE "<TD VALIGN=top ROWSPAN=2><IMG SRC=\"$ICONS/$SITEPICTURE\" BORDER=0></TD>\n";
	}
	print HTMLFILE "<TD VALIGN=bottom ALIGN=center CLASS=a32b>$title</TD></TR>\n";
	print HTMLFILE "<TR><TD ALIGN=center VALIGN=top CLASS=n10b>Updated on $LONGDATE</TD></TR>\n";
	print HTMLFILE "</TABLE>\n";
	print HTMLFILE "<P><CENTER><TABLE CELLPADDING=5 BORDER=1>\n";

	if ($type eq "ptla") {
		print HTMLFILE "<TR BGCOLOR=\"$MAINCOLOR\" ALIGN=center><TD CLASS=w12b>Owner</TD>";
		print HTMLFILE "<TD CLASS=w12b>pTLA Prefix</TD>";
		print HTMLFILE "<TD CLASS=w12b>Instability</TD>";
		print HTMLFILE "<TD CLASS=w12b>Unavailability</TD>";
		print HTMLFILE "<TD CLASS=w12b>Details</TD></TR>\n";

		foreach $key (sort {$PTLASTAB{$b} <=> $PTLASTAB{$a}} (keys(%PTLASTAB))) {
			unless ($PTLAUNAV{$key} == 1) {
				$PTLAINFO{$key} =~ /(\S+)\s+(\S+)/;
				$ptlaprefix = $1;
				$viewlink = "./$history_reldir/".$2.".html";
				$unstab = &trunc_number($PTLASTAB{$key}*100, 1);
				$unav = &trunc_number($PTLAUNAV{$key}*100, 1);
				$name = &format_owner($key,0);
				print HTMLFILE "<TR><TD CLASS=n12>$name</TD>";
				print HTMLFILE "<TD CLASS=t12>$ptlaprefix</TT></TD>";
				print HTMLFILE "<TD ALIGN=right CLASS=t12>$unstab\%</TD><TD ALIGN=right CLASS=t12>$unav\%</TD>";
				print HTMLFILE "<TD ALIGN=center CLASS=n12><A HREF=\"$viewlink\">view</A></TD></TR>\n";
			}
		}
		foreach $key (sort {$PTLAUNAV{$a} <=> $PTLAUNAV{$b}} (keys(%PTLAUNAV))) {
			unless ($PTLAUNAV{$key} < 1) {
				$PTLAINFO{$key} =~ /(\S+)\s+(\S+)/;
				$ptlaprefix = $1;
				$viewlink = "./$history_reldir/".$2.".html";
				$unstab = &trunc_number($PTLASTAB{$key}*100, 1);
				$unav = &trunc_number($PTLAUNAV{$key}*100, 1);
				$name = &format_owner($key,0);
				print HTMLFILE "<TR><TD CLASS=n12>$name</TD>";
				print HTMLFILE "<TD CLASS=t12>$ptlaprefix</TD>";
				print HTMLFILE "<TD ALIGN=right CLASS=t12>$unstab\%</TD><TD ALIGN=right CLASS=t12>$unav\%</TD>";
				print HTMLFILE "<TD ALIGN=center CLASS=n12><A HREF=\"$viewlink\">view</A></TD></TR>\n";
			}
		}
		print HTMLFILE "
		</TABLE></CENTER>
		<P><HR>
                <SPAN CLASS=n10><A HREF=\"./$INDEXPAGE\">Back to routing information page</A>
		<HR>
		This page displays a summary of the IPv6 BGP routing stability in the 6bone backbone during the
		last 24 hours. For each assigned pTLA, figures representing instability and unavailability of
		the correspondent route entry in the IPv6 BGP routing table are provided. pTLAs are sorted
		starting from the most unstable route entry.<BR>
		These figures are calculated based on periodical snapshots of the IPv6 BGP routing table as follows:
		<UL><LI><SPAN CLASS=r10>Unstability</SPAN>: the ratio between the number of times there
		was a detection of AS Path changes for the specific route and the number of snapshots in the last 24 hours
		<LI><SPAN CLASS=r10>Unavailability</SPAN>: the ratio between the number of times the route
		was not present and the number of snapshots in the last 24 hours</UL>
		Clicking on \&quot;view\&quot; for each pTLA you can get more detailed information about routing stability
		in the last 24 hours.
		";
	}
	else {
		print HTMLFILE "<TR BGCOLOR=\"$MAINCOLOR\" ALIGN=center><TD CLASS=w12b>Owner</TD>";
		print HTMLFILE "<TD CLASS=w12b>Prefix</TD>";
		print HTMLFILE "<TD CLASS=w12b>Unstability</TD>";
		print HTMLFILE "<TD CLASS=w12b>Unavailability</TD>";
		print HTMLFILE "<TD CLASS=w12b>Details</TD></TR>\n";

		foreach $key (sort {$IANASTAB{$b} <=> $IANASTAB{$a}} (keys(%IANASTAB))) {
			unless ($IANAUNAV{$key} == 1) {
				$IANAINFO{$key} =~ /(\S+)\s+(\S+)/;
				$ptlaprefix = $1;
				$viewlink = "./$history_reldir/".$2.".html";
				$unstab = &trunc_number($IANASTAB{$key}*100, 1);
				$unav = &trunc_number($IANAUNAV{$key}*100, 1);
				$name = &format_owner($key,1);
				print HTMLFILE "<TR BGCOLOR=\"#FFFFFF\"><TD CLASS=n12>$name</TD>";
				print HTMLFILE "<TD CLASS=t12>$ptlaprefix</TD>";
				print HTMLFILE "<TD ALIGN=right CLASS=t12>$unstab\%</TD><TD ALIGN=right CLASS=t12>$unav\%</TD>";
				print HTMLFILE "<TD ALIGN=center CLASS=n12><A HREF=\"$viewlink\">view</A></TD></TR>\n";
			}
		}
		foreach $key (sort {$IANAUNAV{$a} <=> $IANAUNAV{$b}} (keys(%IANAUNAV))) {
			unless ($IANAUNAV{$key} < 1) {
				$IANAINFO{$key} =~ /(\S+)\s+(\S+)/;
				$ptlaprefix = $1;
				$viewlink = "./$history_reldir/".$2.".html";
				$unstab = &trunc_number($IANASTAB{$key}*100, 1);
				$unav = &trunc_number($IANAUNAV{$key}*100, 1);
				$name = &format_owner($key,1);
				print HTMLFILE "<TR BGCOLOR=\"#FFFFFF\"><TD CLASS=n12>$name</TD>";
				print HTMLFILE "<TD CLASS=t12>$ptlaprefix</TD>";
				print HTMLFILE "<TD ALIGN=right CLASS=t12>$unstab\%</TD><TD ALIGN=right CLASS=t12>$unav\%</TD>";
				print HTMLFILE "<TD ALIGN=center CLASS=n12><A HREF=\"$viewlink\">view</A></TD></TR>\n";
			}
		}
		print HTMLFILE "
		</TABLE></CENTER>\n
		<P><SPAN CLASS=n10><A HREF=\"./$INDEXPAGE\">Back to routing information page</A><P>\n
		<HR>\n
		This page displays all the locally known sTLAs that are advertised inside the 6Bone backbone.
	        For each IPv6 BGP route entry, figures representing instability and unavailability of are provided.
	        Ruote entries are sorted starting from the most unstable route entry.<BR>
	        These figures are calculated based on periodical snapshots of the IPv6 BGP routing table as follows:
	        <UL><LI><SPAN CLASS=r10>Unstability</SPAN>: the ratio between the number of times there
	        was a detection of AS Path changes for the specific route and the number of snapshots in the last 24 hours
	        <LI><SPAN CLASS=r10>Unavailability</SPAN>: the ratio between the number of times the route
	        was not present and the number of snapshots in the last 24 hours</UL>
		Clicking on \&quot;view\&quot; for each sTLA you can get more detailed information about routing stability
		in the last 24 hours.
	        ";
	}
	print HTMLFILE "
	<HR>\n".&footer."
	</SPAN>\n</BODY>\n</HTML>\n";
	close(HTMLFILE);
	return(0);
}

# Routine to create HTML route stability files
sub create_html_stability_detailfile {
	my ($filename, $prefix, $aspath, $sitename, $nunavail, $nchanges, $nsamples, @aspaths) = @_;

	my ($unav, $unstab);
	my ($currentaspath, $currentn, $perc, $asn, $separator, $truncname);
	my ($maxlen, $ncolumns, $ntcolumns, $key);
	my @asns = ();
	my %hints = ();

	open(HIFILE,">".$filename);

	print HIFILE &header("Last 24h $sitename history: $LONGDATE", 0);

	if ($sitename =~ /^[\-\?]/) {$sitename = ''}
	else {$sitename = "($sitename)"}
	print HIFILE "<SPAN CLASS=a16b>$prefix $sitename</SPAN>\n<P>\n";
	print HIFILE "<P><TABLE BORDER=0>\n";
	print HIFILE "<TR><TD CLASS=l12b>Current status:&nbsp;</TD>\n";
	print HIFILE "<TD CLASS=n12>";
	if ($aspath eq "0") {print HIFILE "<FONT COLOR=\"FF0000\">not present</FONT>"}
	else {print HIFILE "route present"}
	print HIFILE "</TD></TR>\n";
	print HIFILE "<TR><TD VALIGN=top CLASS=l12b>Current AS Path:&nbsp;</TD>\n";
	print HIFILE "<TD CLASS=n12>";
	if ($aspath eq "0") {print HIFILE "<FONT COLOR=\"FF0000\">none</FONT>"}
	else {print HIFILE "$aspath"}
	print HIFILE "</TD></TR>\n</TABLE>\n<P>\n";

	print HIFILE "<SPAN CLASS=a16b>Last 24 hour report<BR>\n";

	$unav = &trunc_number(($nunavail/$nsamples)*100, 1)."\%";
	$unstab = &trunc_number(($nchanges/$nsamples)*100, 1)."\%";

	print HIFILE "<P>\n<TABLE BORDER=0>\n";
	print HIFILE "<TR><TD CLASS=l12b>Unavailability <SPAN CLASS=t12>(n_path_unavailable_detections/n_samples)</SPAN>:&nbsp;</TD>\n";
	print HIFILE "<TD ALIGN=right CLASS=l12b>$unav</TD>\n";
	print HIFILE "<TD ALIGN=right CLASS=n12>($nunavail\/$nsamples)</TD></TR>\n";
	print HIFILE "<TR><TD CLASS=l12b>Unstability <SPAN CLASS=t12>(n_path_changes/n_samples)</SPAN>:&nbsp;</TD>\n";
	print HIFILE "<TD ALIGN=right CLASS=l12b>$unstab</TD>\n";
	print HIFILE "<TD ALIGN=right CLASS=n12>($nchanges\/$nsamples)</TD></TR>\n";
	print HIFILE "</TABLE>\n<P>\n";

	print HIFILE "<P><SPAN CLASS=l12b>Route distribution:</SPAN>\n";
	print HIFILE "<P><TABLE BORDER=0 CELLPADDING=1 CELLSPACING=0>\n";

	$maxlen = 0;
	for (@aspaths) {
		/^\s*\S+\s+(\S+)/;
		$currentaspath = $1;
		@asns = split(/\//,$currentaspath);
		if (@asns > $maxlen) {$maxlen = @asns}
	}
	$ncolumns = 2*$maxlen;
	$ntcolumns = $ncolumns + 7;

	print HIFILE "<TR>\n";
	print HIFILE "<TD COLSPAN=6 CLASS=n10b>Frequency&nbsp;</TD>\n";
	print HIFILE "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../$ICONS/p.gif\"></TD>\n";
	print HIFILE "<TD COLSPAN=$ncolumns CLASS=n10b>&nbsp;AS path&nbsp;</TD>\n";
	print HIFILE "</TR>\n";
	print HIFILE "<TR>\n";
	print HIFILE "<TD COLSPAN=$ntcolumns BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../$ICONS/p.gif\"></TD>\n";
	print HIFILE "</TR>\n";

	undef %hints;
	for (@aspaths) {
		/^\s*(\S+)\s+(\S+)/;
		$currentn = $1;
		$currentaspath = $2;
		if ($currentn == 0) {next}
		$perc = &trunc_number(($currentn/$nsamples)*100, 1)."\%";
		@asns = split(/\//,$currentaspath);

		print HIFILE "<TR><TD ALIGN=right CLASS=t12>$perc&nbsp;</TD>";
		print HIFILE "<TD CLASS=t12>(</TD>";
		print HIFILE "<TD ALIGN=right CLASS=t12>$currentn</TD>";
		print HIFILE "<TD CLASS=t12>/</TD>";
		print HIFILE "<TD ALIGN=left CLASS=t12>$nsamples</TD>";
		print HIFILE "<TD CLASS=t12>)&nbsp;</TD>\n";
		print HIFILE "<TD BGCOLOR=\"$LINESDETTABLECOLOR\"><IMG SRC=\"../$ICONS/p.gif\"></TD>\n";

		if ($currentaspath eq "0") {
			print HIFILE "<TD COLSPAN=$ncolumns CLASS=t12><FONT COLOR=\"FF0000\">&nbsp;Unavailable</FONT></TD></TR>\n";
		}
		else {
			$separator = "&nbsp;";
			foreach $asn (@asns) {
				$truncname = &format_asname($asn);
				if ($ASNAME{$asn} =~ /^AS\d+$/ && $HINTS{$asn}) {$hints{$asn} = $HINTS{$asn}};
				print HIFILE "<TD CLASS=t12>$separator</TD>\n";
				print HIFILE "<TD CLASS=t12>$truncname</TD>\n";
				$separator = "-";
			}
			print HIFILE "</TR>\n";
		}
	}

	print HIFILE "</TABLE>\n<P>\n";

	if (%hints) {
		print HIFILE "<SPAN CLASS=a16b>Hints:</SPAN><BR>\n";
		foreach $key (keys(%hints)) {
			print HIFILE "<SPAN CLASS=n12><B>$key</B>: $hints{$key}<BR>\n";
		}
		print HIFILE "</SPAN>\n";
	}
	print HIFILE "
	<P><HR>\n
	<SPAN CLASS=n10>".&footer."
	</SPAN>\n</BODY>\n</HTML>\n
	";

	close(HIFILE);
	return(1);
}

(1);
#### ASpath-tree v.4.2 - Released on Thu APR 17 2003, h.16:58:12
#### File: lib/route-stability.pl Last modified on Thu DEC 5 2002, h.22:05:28
