Update code to include this [d6c4c0ff951a6d8bf881177a4fa41245c6acc1dd](d6c4c0ff95 (diff-4eae35f1b35977a00ebd8086c259d4c9)) update from AS-Stats.

This commit is contained in:
Nicolas Debrigode 2017-04-03 09:04:11 +02:00
parent d2cd398eaf
commit 11af3523fc
5 changed files with 133 additions and 112 deletions

View File

@ -3,7 +3,7 @@
/* changes these values to suit your local configuration */ /* changes these values to suit your local configuration */
$rrdpath = "/data/asstats/rrd"; $rrdpath = "/data/asstats/rrd";
$daystatsfile = "/data/asstats/asstats_day.txt"; $daystatsfile = "/data/asstats/asstats/asstats_day.txt";
$rrdtool = "/usr/bin/rrdtool"; $rrdtool = "/usr/bin/rrdtool";
$daypeerstatsfile = "/data/asstats/peerasstats_day.txt"; $daypeerstatsfile = "/data/asstats/peerasstats_day.txt";
@ -11,13 +11,12 @@ $showpeeras = false;
$asinfofile = "asinfo.txt"; $asinfofile = "asinfo.txt";
$knownlinksfile = "/data/asstats/conf/knownlinks"; $knownlinksfile = "/data/asstats/conf/knownlinks";
$outispositive = false; $outispositive = false;
$show95th = true; $show95th = true;
$ntop = 10; $ntop = 20;
$showv6 = true; $showv6 = true;
$showtitledetail = true; $showtitledetail = true;
$hidelinkusagename = false; # $showtitledetail will need to be true to allow this
$vertical_label = true; # vertical IN/OUT label in graph $vertical_label = true; # vertical IN/OUT label in graph
$brighten_negative = true; # brighten the "negative" part of graphs $brighten_negative = true; # brighten the "negative" part of graphs
@ -40,13 +39,13 @@ $customlinks = array(
); );
/* Custom time intervals for top N AS */ /* Custom time intervals for top N AS */
/*
/*$top_intervals[] = array( $top_intervals[] = array(
'hours' => 7*24, 'hours' => 7*24,
'statsfile' => '/data/asstats/asstats_day.txt', 'statsfile' => '/data/as-stats/asstats_week.txt',
'label' => '1 week' 'label' => '1 week'
);*/ );
/*$top_intervals[] = array( $top_intervals[] = array(
'hours' => 30*24, 'hours' => 30*24,
'statsfile' => '/data/as-stats/asstats_month.txt', 'statsfile' => '/data/as-stats/asstats_month.txt',
'label' => '30 days' 'label' => '30 days'

View File

@ -10,10 +10,6 @@ $default_graph_height = 360;
$top_graph_width = 600; $top_graph_width = 600;
$top_graph_height = 220; $top_graph_height = 220;
/* Size of graphs on link usage page */
$linkusage_graph_width = 600;
$linkusage_graph_height = 480;
/* Size of graphs on AS-Set page */ /* Size of graphs on AS-Set page */
$asset_graph_width = 600; $asset_graph_width = 600;
$asset_graph_height = 200; $asset_graph_height = 200;

141
func.inc
View File

@ -78,49 +78,48 @@ function getknownlinks() {
return $knownlinks; return $knownlinks;
} }
function getasstats_top($ntop, $statfile) { function getasstats_top($ntop, $statfile, $selected_links) {
/* first step: walk the data for all ASes to determine the top 5 for the given link */ try{
$fd = fopen($statfile, "r"); $db = new SQLite3($statfile);
if (!$fd) }catch(Exception $e){
return array(); return array();
$cols = explode("\t", trim(fgets($fd))); }
if(sizeof($selected_links) == 0){
/* read in up to $ntop AS stats, sum up columns */ $selected_links = array();
while (!feof($fd)) { foreach(getknownlinks() as $link)
$line = trim(fgets($fd)); $selected_links[] = $link['tag'];
if (!$line) }
continue; $nlinks = 0;
$query_total = '0';
$els = explode("\t", $line); $query_links = '';
foreach($selected_links as $tag){
/* first element is the AS */ $query_links .= "${tag}_in, ${tag}_out, ${tag}_v6_in, ${tag}_v6_out, ";
$as = $els[0]; $nlinks += 4;
$query_total .= " + ${tag}_in + ${tag}_out + ${tag}_v6_in + ${tag}_v6_out";
}
$query = "select asn, $query_links $query_total as total from stats order by total desc limit $ntop";
$asn = $db->query($query);
$asstats = array();
while($row = $asn->fetchArray()){
$tot_in = 0; $tot_in = 0;
$tot_out = 0; $tot_out = 0;
$tot_v6_in = 0; $tot_v6_in = 0;
$tot_v6_out = 0; $tot_v6_out = 0;
foreach($row as $key => $value){
for ($i = 1; $i < count($els); $i++) { if (strpos($key, '_in') !== false) {
if (strpos($cols[$i], "_in") !== false) { if (strpos($key, '_v6_') !== false)
if (strpos($cols[$i], "_v6_") !== false) $tot_v6_in += $value;
$tot_v6_in += $els[$i];
else else
$tot_in += $els[$i]; $tot_in += $value;
} else { } else if (strpos($key, '_out') !== false) {
if (strpos($cols[$i], "_v6_") !== false) if (strpos($key, '_v6_') !== false)
$tot_v6_out += $els[$i]; $tot_v6_out += $value;
else else
$tot_out += $els[$i]; $tot_out += $value;
} }
} }
$asstats[$row['asn']] = array($tot_in, $tot_out, $tot_v6_in, $tot_v6_out);
$asstats[$as] = array($tot_in, $tot_out, $tot_v6_in, $tot_v6_out);
if (count($asstats) >= $ntop)
break;
} }
fclose($fd);
return $asstats; return $asstats;
} }
@ -201,35 +200,30 @@ function clearCacheFileASSET($asset) {
} }
# return the html used in top.php : <a href=blabla><img src=blabla/></url> # return the html used in top.php : <a href=blabla><img src=blabla/></url>
function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage){ function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage, $selected_links = array()){
$result = ""; $img = getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, '', '', false, $selected_links);
$result = "<a href='history.php?as=$as&peerusage=$peerusage&v=$ipversion' target='_blank'>$img</a>";
$img = getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, '', '', false);
if ( $img ) {
$result = "<a href='history.php?as=$as&peerusage=$peerusage&v=$ipversion'>$img</a>";
}
return($result); return($result);
} }
# return the html used in history.php (for example) : <img src=blabla/> # return the html used in history.php (for example) : <img src=blabla/>
function getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, $alt, $class, $history = false){ function getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, $alt, $class, $history = false, $selected_links=array()){
global $top_graph_width; global $top_graph_width;
global $top_graph_height; global $top_graph_height;
$result = ""; $dname = rawurlencode("AS$as - $desc - IPV$ipversion");
$result = "<img class='img-responsive' alt='$alt' class='$class' src='gengraph.php?v=$ipversion&as=$as&peerusage=$peerusage&dname=$dname&start=$start&end=$end";
$rrdfile = getRRDFileForAS($as); if(!$history)
if (file_exists($rrdfile)) { $result .= "&width=$top_graph_width&height=$top_graph_height&nolegend=1";
$dname = rawurlencode("AS$as - $desc - IPV$ipversion"); if(sizeof($selected_links) != 0){
$result = "<img alt='$alt' class='$class' src='gengraph.php?v=$ipversion&as=$as&peerusage=$peerusage&dname=$dname&start=$start&end=$end"; $result .= "&selected_links=";
if(!$history) foreach($selected_links as $link)
$result .= "&width=$top_graph_width&height=$top_graph_height&nolegend=1"; $result .= "$link,";
$result .= "'"; $result = rtrim($result, ',');
if(!$history)
$result .= " width='$top_graph_width' height='$top_graph_height' border='0'";
$result .= "/>";
} }
$result .= "'";
if(!$history)
$result .= " width='$top_graph_width' height='$top_graph_height' border='0'";
$result .= "/>";
return($result); return($result);
} }
@ -259,16 +253,15 @@ function debug($txt) {
print "</pre>"; print "</pre>";
} }
function menu() { function menu($selected_links) {
global $top_intervals; global $top_intervals;
//debug($selected_links);
//echo count($top_intervals); //echo count($top_intervals);
$dpagename = basename($_SERVER['PHP_SELF'], ".php"); $dpagename = basename($_SERVER['PHP_SELF'], ".php");
$active_top = $dpagename == "index" ? "active": ""; $active_top = $dpagename == "index" ? "active": "";
$active_searchas = $dpagename == "history" ? 'class="active"' : ""; $active_searchas = $dpagename == "history" ? 'class="active"' : "";
$active_searchasset = $dpagename == "asset" ? 'class="active"' : ""; $active_searchasset = $dpagename == "asset" ? 'class="active"' : "";
$active_linkusage = $dpagename == "linkusage" ? "active" : "";
$return = '<header class="main-header">'; $return = '<header class="main-header">';
$return .= '<nav class="navbar navbar-static-top">'; $return .= '<nav class="navbar navbar-static-top">';
@ -300,25 +293,17 @@ function menu() {
$return .='<li '.$active_searchas.'><a href="history.php">View AS</a></li>'; $return .='<li '.$active_searchas.'><a href="history.php">View AS</a></li>';
$return .='<li '.$active_searchasset.'><a href="asset.php">View AS-SET</a></li>'; $return .='<li '.$active_searchasset.'><a href="asset.php">View AS-SET</a></li>';
if ( count($top_intervals) < 2 ) {
$return .='<li class="'.$active_linkusage.'"><a href="linkusage.php">Link Usage</a></li>';
} else {
$return .= '<li class="dropdown '.$active_linkusage.'">';
$return .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false">Link Usage <span class="caret"></span></a>';
$return .= '<ul class="dropdown-menu" role="menu">';
foreach ($top_intervals as $interval) {
$return .= '<li><a href="linkusage.php?numhours=' . $interval['hours'] . '">Link Usage - ' . $interval['label'] . '</a></li>';
}
$return .= '</ul>';
$return .= '</li>';
}
$return .='</ul>'; $return .='</ul>';
$val_ntop = isset($_GET['n']) ? $_GET['n'] : ""; $val_ntop = isset($_GET['n']) ? $_GET['n'] : "";
if ($dpagename == "index") { if ($dpagename == "index") {
$return .='<form class="navbar-form navbar-left" role="search">'; $return .='<form class="navbar-form navbar-left" role="search">';
foreach($selected_links as $tag){
$tag = "link_".$tag;
$return .='<input type="hidden" name="'.$tag.'" value="on">';
}
$return .='<div class="input-group">'; $return .='<div class="input-group">';
$return .='<input type="text" class="form-control menu-input" name="n" placeholder="Number of AS" value="'.$val_ntop.'">'; $return .='<input type="text" class="form-control menu-input" name="n" placeholder="Number of AS" value="'.$val_ntop.'">';
$return .='<span class="input-group-btn">'; $return .='<span class="input-group-btn">';
@ -373,18 +358,4 @@ function content_header($titre, $small) {
return $header; return $header;
} }
function box_linkusage ($title, $img) {
$return = '<div class="box box-primary">';
$return .= '<div class="box-header with-border">';
$return .= '<i class="fa fa-bar-chart-o"></i>';
$return .= '<h3 class="box-title">'.$title.'</h3>';
$return .= '</div>';
$return .= '<div class="box-body">';
$return .= '<center>'.$img.'</center>';
$return .= '</div>';
$return .= '</div>';
return $return;
}
?> ?>

View File

@ -29,6 +29,21 @@ else
$peerusage = 0; $peerusage = 0;
$knownlinks = getknownlinks(); $knownlinks = getknownlinks();
if(isset($_GET['selected_links'])){
$reverse = array();
foreach($knownlinks as $link)
$reverse[$link['tag']] = array('color' => $link['color'], 'descr' => $link['descr']);
$links = array();
foreach(explode(',', $_GET['selected_links']) as $tag){
$link = array('tag' => $tag,
'color' => $reverse[$tag]['color'],
'descr' => $reverse[$tag]['descr']);
$links[] = $link;
}
$knownlinks = $links;
}
$rrdfile = getRRDFileForAS($as, $peerusage); $rrdfile = getRRDFileForAS($as, $peerusage);
if ($compat_rrdtool12) { if ($compat_rrdtool12) {

View File

@ -1,7 +1,6 @@
<?php include("func.inc"); ?> <?php include("func.inc"); ?>
<?php <?php
if(!isset($peerusage)) $peerusage = 0; if(!isset($peerusage)) $peerusage = 0;
if (isset($_GET['n'])) $ntop = (int)$_GET['n']; if (isset($_GET['n'])) $ntop = (int)$_GET['n'];
if ($ntop > 200) $ntop = 200; if ($ntop > 200) $ntop = 200;
@ -15,7 +14,14 @@ if ($peerusage) {
} }
$label = statsLabelForHours($hours); $label = statsLabelForHours($hours);
$topas = getasstats_top($ntop, $statsfile); $knownlinks = getknownlinks();
$selected_links = array();
foreach($knownlinks as $link){
if(isset($_GET["link_${link['tag']}"]))
$selected_links[] = $link['tag'];
}
$topas = getasstats_top($ntop, $statsfile, $selected_links);
$start = time() - $hours*3600; $start = time() - $hours*3600;
$end = time(); $end = time();
@ -69,14 +75,14 @@ foreach ($topas as $as => $nbytes) {
if ($showv6) { $col = "5"; } else { $col="10"; } if ($showv6) { $col = "5"; } else { $col="10"; }
$aff_astable .= '<div class="col-lg-'.$col.'">'; $aff_astable .= '<div class="col-lg-'.$col.'">';
$aff_astable .= '<span class="pull-right">'; $aff_astable .= '<span class="pull-right">';
$aff_astable .= getHTMLUrl($as, 4, $asinfo['descr'], $start, $end, $peerusage); $aff_astable .= getHTMLUrl($as, 4, $asinfo['descr'], $start, $end, $peerusage, $selected_links);
$aff_astable .= '</span>'; $aff_astable .= '</span>';
$aff_astable .= '</div>'; $aff_astable .= '</div>';
if ($showv6) { if ($showv6) {
$aff_astable .= '<div class="col-lg-5">'; $aff_astable .= '<div class="col-lg-5">';
$aff_astable .= '<span class="pull-right">'; $aff_astable .= '<span class="pull-right">';
$aff_astable .= getHTMLUrl($as, 6, $asinfo['descr'], $start, $end, $peerusage); $aff_astable .= getHTMLUrl($as, 6, $asinfo['descr'], $start, $end, $peerusage, $selected_links);
$aff_astable .= '</span>'; $aff_astable .= '</span>';
$aff_astable .= '</div>'; $aff_astable .= '</div>';
} }
@ -91,12 +97,19 @@ foreach ($topas as $as => $nbytes) {
$aff_astable .= '</ul>'; $aff_astable .= '</ul>';
// LEGEND // LEGEND
$knownlinks = getknownlinks(); //$knownlinks = getknownlinks();
if ( !$detect->isMobile() && !$detect->isTablet() ) { if ( !$detect->isMobile() && !$detect->isTablet() ) {
$aff_legend = "<table class='small'>"; $aff_legend = "<table class='small'>";
foreach ($knownlinks as $link) { foreach ($knownlinks as $link) {
$tag = "link_${link['tag']}";
$checked = '';
if(isset($_GET[$tag]) && $_GET[$tag] == 'on') {
$checked = 'checked';
}
$aff_legend .= "<tr><td style=\"border: 4px solid #fff;\">"; $aff_legend .= "<tr><td style=\"border: 4px solid #fff;\">";
$aff_legend .= "<table style=\"border-collapse: collapse; margin: 0; padding: 0\"><tr>"; $aff_legend .= "<table style=\"border-collapse: collapse; margin: 0; padding: 0\"><tr>";
@ -108,7 +121,9 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
} }
$aff_legend .= "</tr></table>"; $aff_legend .= "</tr></table>";
$aff_legend .= "</td><td>&nbsp;" . $link['descr'] . "</td></tr>\n"; $aff_legend .= "</td><td>&nbsp;" . $link['descr'] . "</td>";
$aff_legend .= "<td>&nbsp;<input type='checkbox' name='".$tag."' id ='".$tag."' ".$checked."></td>";
$aff_legend .= "</tr>\n";
} }
$aff_legend .= "</table>"; $aff_legend .= "</table>";
@ -119,6 +134,13 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
$aff_legend .= "<table style=\"border-collapse: collapse; margin: 0; padding: 0\"><tr>"; $aff_legend .= "<table style=\"border-collapse: collapse; margin: 0; padding: 0\"><tr>";
foreach ($knownlinks as $link) { foreach ($knownlinks as $link) {
$tag = "link_${link['tag']}";
$checked = '';
if(isset($_GET[$tag]) && $_GET[$tag] == 'on') {
$checked = 'checked';
}
if ($brighten_negative) { if ($brighten_negative) {
$aff_legend .= "<td width=\"9\" height=\"18\" style=\"background-color: #{$link['color']}\">&nbsp;</td>"; $aff_legend .= "<td width=\"9\" height=\"18\" style=\"background-color: #{$link['color']}\">&nbsp;</td>";
$aff_legend .= "<td width=\"9\" height=\"18\" style=\"opacity: 0.73; background-color: #{$link['color']}\">&nbsp;</td>"; $aff_legend .= "<td width=\"9\" height=\"18\" style=\"opacity: 0.73; background-color: #{$link['color']}\">&nbsp;</td>";
@ -126,6 +148,8 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
$aff_legend .= "<td width=\"18\" height=\"18\" style=\"background-color: #{$link['color']}\">&nbsp;</td>"; $aff_legend .= "<td width=\"18\" height=\"18\" style=\"background-color: #{$link['color']}\">&nbsp;</td>";
} }
$aff_legend .= "<td>&nbsp;" . $link['descr'] . "&nbsp;</td>\n"; $aff_legend .= "<td>&nbsp;" . $link['descr'] . "&nbsp;</td>\n";
$aff_legend .= "<td>&nbsp;<input type='checkbox' name='".$tag."' id ='".$tag."' ".$checked.">&nbsp;</td>";
} }
$aff_legend .= "</tr></table>"; $aff_legend .= "</tr></table>";
@ -158,7 +182,7 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
<div class="wrapper"> <div class="wrapper">
<!-- =============================================== --> <!-- =============================================== -->
<?php echo menu(); ?> <?php echo menu($selected_links); ?>
<!-- =============================================== --> <!-- =============================================== -->
<div class="content-wrapper"> <div class="content-wrapper">
@ -176,6 +200,9 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
if ( $detect->isMobile() || $detect->isTablet() ) { if ( $detect->isMobile() || $detect->isTablet() ) {
?> ?>
<form method='get'>
<input type='hidden' name='numhours' value='<?php echo $hours; ?>'/>
<input type='hidden' name='n' value='<?php echo $ntop; ?>'/>
<div class="box box-primary"> <div class="box box-primary">
<div class="box-header with-border"> <div class="box-header with-border">
<h3 class="box-title">Legend</h3> <h3 class="box-title">Legend</h3>
@ -183,21 +210,34 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
<div class="box-body"> <div class="box-body">
<?php echo $aff_legend; ?> <?php echo $aff_legend; ?>
</div> </div>
<div class="box-footer">
<button type="submit" class="btn pull-right"><i class="fa fa-search"></i></button>
</div>
</div> </div>
</form>
<?php <?php
} else { } else {
?> ?>
<div class="row affix col-md-12 col-lg-<?php echo $first_col; ?>"> <div class="row affix col-md-12 col-lg-<?php echo $first_col; ?>">
<div class="box box-primary">
<div class="box-header with-border"> <form method='get'>
<h3 class="box-title">Legend</h3> <input type='hidden' name='numhours' value='<?php echo $hours; ?>'/>
<input type='hidden' name='n' value='<?php echo $ntop; ?>'/>
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Legend</h3>
</div>
<div class="box-body">
<?php echo $aff_legend; ?>
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right"><i class="fa fa-search"></i></button>
</div>
</div> </div>
<div class="box-body"> </form>
<?php echo $aff_legend; ?>
</div>
</div>
</div> </div>
<?php } ?> <?php } ?>