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

View File

@ -10,10 +10,6 @@ $default_graph_height = 360;
$top_graph_width = 600;
$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 */
$asset_graph_width = 600;
$asset_graph_height = 200;

141
func.inc
View File

@ -78,49 +78,48 @@ function getknownlinks() {
return $knownlinks;
}
function getasstats_top($ntop, $statfile) {
/* first step: walk the data for all ASes to determine the top 5 for the given link */
$fd = fopen($statfile, "r");
if (!$fd)
function getasstats_top($ntop, $statfile, $selected_links) {
try{
$db = new SQLite3($statfile);
}catch(Exception $e){
return array();
$cols = explode("\t", trim(fgets($fd)));
/* read in up to $ntop AS stats, sum up columns */
while (!feof($fd)) {
$line = trim(fgets($fd));
if (!$line)
continue;
$els = explode("\t", $line);
/* first element is the AS */
$as = $els[0];
}
if(sizeof($selected_links) == 0){
$selected_links = array();
foreach(getknownlinks() as $link)
$selected_links[] = $link['tag'];
}
$nlinks = 0;
$query_total = '0';
$query_links = '';
foreach($selected_links as $tag){
$query_links .= "${tag}_in, ${tag}_out, ${tag}_v6_in, ${tag}_v6_out, ";
$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_out = 0;
$tot_v6_in = 0;
$tot_v6_out = 0;
for ($i = 1; $i < count($els); $i++) {
if (strpos($cols[$i], "_in") !== false) {
if (strpos($cols[$i], "_v6_") !== false)
$tot_v6_in += $els[$i];
foreach($row as $key => $value){
if (strpos($key, '_in') !== false) {
if (strpos($key, '_v6_') !== false)
$tot_v6_in += $value;
else
$tot_in += $els[$i];
} else {
if (strpos($cols[$i], "_v6_") !== false)
$tot_v6_out += $els[$i];
$tot_in += $value;
} else if (strpos($key, '_out') !== false) {
if (strpos($key, '_v6_') !== false)
$tot_v6_out += $value;
else
$tot_out += $els[$i];
$tot_out += $value;
}
}
$asstats[$as] = array($tot_in, $tot_out, $tot_v6_in, $tot_v6_out);
if (count($asstats) >= $ntop)
break;
$asstats[$row['asn']] = array($tot_in, $tot_out, $tot_v6_in, $tot_v6_out);
}
fclose($fd);
return $asstats;
}
@ -201,35 +200,30 @@ function clearCacheFileASSET($asset) {
}
# return the html used in top.php : <a href=blabla><img src=blabla/></url>
function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage){
$result = "";
$img = getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, '', '', false);
if ( $img ) {
$result = "<a href='history.php?as=$as&peerusage=$peerusage&v=$ipversion'>$img</a>";
}
function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage, $selected_links = array()){
$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>";
return($result);
}
# 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_height;
$result = "";
$rrdfile = getRRDFileForAS($as);
if (file_exists($rrdfile)) {
$dname = rawurlencode("AS$as - $desc - IPV$ipversion");
$result = "<img alt='$alt' class='$class' src='gengraph.php?v=$ipversion&as=$as&peerusage=$peerusage&dname=$dname&start=$start&end=$end";
if(!$history)
$result .= "&width=$top_graph_width&height=$top_graph_height&nolegend=1";
$result .= "'";
if(!$history)
$result .= " width='$top_graph_width' height='$top_graph_height' border='0'";
$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";
if(!$history)
$result .= "&width=$top_graph_width&height=$top_graph_height&nolegend=1";
if(sizeof($selected_links) != 0){
$result .= "&selected_links=";
foreach($selected_links as $link)
$result .= "$link,";
$result = rtrim($result, ',');
}
$result .= "'";
if(!$history)
$result .= " width='$top_graph_width' height='$top_graph_height' border='0'";
$result .= "/>";
return($result);
}
@ -259,16 +253,15 @@ function debug($txt) {
print "</pre>";
}
function menu() {
function menu($selected_links) {
global $top_intervals;
//debug($selected_links);
//echo count($top_intervals);
$dpagename = basename($_SERVER['PHP_SELF'], ".php");
$active_top = $dpagename == "index" ? "active": "";
$active_searchas = $dpagename == "history" ? 'class="active"' : "";
$active_searchasset = $dpagename == "asset" ? 'class="active"' : "";
$active_linkusage = $dpagename == "linkusage" ? "active" : "";
$return = '<header class="main-header">';
$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_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>';
$val_ntop = isset($_GET['n']) ? $_GET['n'] : "";
if ($dpagename == "index") {
$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 .='<input type="text" class="form-control menu-input" name="n" placeholder="Number of AS" value="'.$val_ntop.'">';
$return .='<span class="input-group-btn">';
@ -373,18 +358,4 @@ function content_header($titre, $small) {
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;
$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);
if ($compat_rrdtool12) {

View File

@ -1,7 +1,6 @@
<?php include("func.inc"); ?>
<?php
if(!isset($peerusage)) $peerusage = 0;
if (isset($_GET['n'])) $ntop = (int)$_GET['n'];
if ($ntop > 200) $ntop = 200;
@ -15,7 +14,14 @@ if ($peerusage) {
}
$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;
$end = time();
@ -69,14 +75,14 @@ foreach ($topas as $as => $nbytes) {
if ($showv6) { $col = "5"; } else { $col="10"; }
$aff_astable .= '<div class="col-lg-'.$col.'">';
$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 .= '</div>';
if ($showv6) {
$aff_astable .= '<div class="col-lg-5">';
$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 .= '</div>';
}
@ -91,12 +97,19 @@ foreach ($topas as $as => $nbytes) {
$aff_astable .= '</ul>';
// LEGEND
$knownlinks = getknownlinks();
//$knownlinks = getknownlinks();
if ( !$detect->isMobile() && !$detect->isTablet() ) {
$aff_legend = "<table class='small'>";
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 .= "<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 .= "</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>";
@ -119,6 +134,13 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
$aff_legend .= "<table style=\"border-collapse: collapse; margin: 0; padding: 0\"><tr>";
foreach ($knownlinks as $link) {
$tag = "link_${link['tag']}";
$checked = '';
if(isset($_GET[$tag]) && $_GET[$tag] == 'on') {
$checked = 'checked';
}
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=\"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>&nbsp;" . $link['descr'] . "&nbsp;</td>\n";
$aff_legend .= "<td>&nbsp;<input type='checkbox' name='".$tag."' id ='".$tag."' ".$checked.">&nbsp;</td>";
}
$aff_legend .= "</tr></table>";
@ -158,7 +182,7 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
<div class="wrapper">
<!-- =============================================== -->
<?php echo menu(); ?>
<?php echo menu($selected_links); ?>
<!-- =============================================== -->
<div class="content-wrapper">
@ -176,6 +200,9 @@ 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-header with-border">
<h3 class="box-title">Legend</h3>
@ -183,21 +210,34 @@ if ( !$detect->isMobile() && !$detect->isTablet() ) {
<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>
</form>
<?php
} else {
?>
<div class="row affix col-md-12 col-lg-<?php echo $first_col; ?>">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">Legend</h3>
<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-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 class="box-body">
<?php echo $aff_legend; ?>
</div>
</div>
</form>
</div>
<?php } ?>