From d6c4c0ff951a6d8bf881177a4fa41245c6acc1dd Mon Sep 17 00:00:00 2001 From: jack Date: Sat, 25 Feb 2017 15:58:28 +0100 Subject: [PATCH] Store statistics into a sqlite3 database. This way, we can quickly filter links on the top page, deprecating the linkusage page Signed-off-by: jack --- bin/rrd-extractstats.pl | 51 ++++++++++--------------- www/config.inc | 1 - www/config_defaults.inc | 4 -- www/func.inc | 83 +++++++++++++++++++++++------------------ www/gengraph.php | 17 +++++++++ www/headermenu.inc | 8 ---- www/top.php | 29 +++++++++++--- 7 files changed, 108 insertions(+), 85 deletions(-) diff --git a/bin/rrd-extractstats.pl b/bin/rrd-extractstats.pl index 4cf278f..f134269 100755 --- a/bin/rrd-extractstats.pl +++ b/bin/rrd-extractstats.pl @@ -30,7 +30,7 @@ my @links = values %knownlinks; # walk through all RRD files in the given path and extract stats for all links -# from them; write the stats to a text file, sorted by total traffic +# from them; write the stats to an sqlite database my @rrdfiles = File::Find::Rule->maxdepth(2)->file->in($rrdpath); @@ -51,45 +51,34 @@ } print "\n"; -# now sort the keys in order of descending total traffic -my @asorder = sort { - my $total_a = 0; - - foreach my $t (values %{$astraffic->{$a}}) { - $total_a += $t; - } - my $total_b = 0; - foreach my $t (values %{$astraffic->{$b}}) { - $total_b += $t; - } - return $total_b <=> $total_a; -} keys %$astraffic; - -open(STATSFILE, ">$statsfile.tmp"); - -# print header line -print STATSFILE "as"; +my $query = 'create table stats(asn int'; foreach my $link (@links) { - print STATSFILE "\t${link}_in\t${link}_out\t${link}_v6_in\t${link}_v6_out"; + $query .= ", ${link}_in int, ${link}_out int, ${link}_v6_in int, ${link}_v6_out int"; } -print STATSFILE "\n"; +$query .= ');'; + +use DBI; +my $db = DBI->connect("dbi:SQLite:dbname=$statsfile.tmp", '', ''); +$db->do('PRAGMA synchronous = OFF'); +$db->do('drop table if exists stats'); +$db->do($query); # print data -foreach my $as (@asorder) { - print STATSFILE "$as"; +foreach my $as (keys %{ $astraffic }) { + + $query = "insert into stats values('$as'"; foreach my $link (@links) { - print STATSFILE "\t" . undefaszero($astraffic->{$as}->{"${link}_in"}); - print STATSFILE "\t" . undefaszero($astraffic->{$as}->{"${link}_out"}); - print STATSFILE "\t" . undefaszero($astraffic->{$as}->{"${link}_v6_in"}); - print STATSFILE "\t" . undefaszero($astraffic->{$as}->{"${link}_v6_out"}); + $query .= ", '" . undefaszero($astraffic->{$as}->{"${link}_in"}) . "'"; + $query .= ", '" . undefaszero($astraffic->{$as}->{"${link}_out"}) . "'"; + $query .= ", '" . undefaszero($astraffic->{$as}->{"${link}_v6_in"}) . "'"; + $query .= ", '" . undefaszero($astraffic->{$as}->{"${link}_v6_out"}) . "'"; } - - print STATSFILE "\n"; + $query .= ');'; + $db->do($query); } -close(STATSFILE); - +$db->disconnect(); rename("$statsfile.tmp", $statsfile); sub undefaszero { diff --git a/www/config.inc b/www/config.inc index 4889397..ecb423d 100644 --- a/www/config.inc +++ b/www/config.inc @@ -17,7 +17,6 @@ $show95th = true; $ntop = 20; $showv6 = true; $showtitledetail = true; -$hidelinkusagename = true; # $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 diff --git a/www/config_defaults.inc b/www/config_defaults.inc index 26c6919..835faa7 100644 --- a/www/config_defaults.inc +++ b/www/config_defaults.inc @@ -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; diff --git a/www/func.inc b/www/func.inc index b7890bf..4cbcd93 100644 --- a/www/func.inc +++ b/www/func.inc @@ -95,49 +95,52 @@ 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; } @@ -224,14 +227,14 @@ function clearCacheFileASSET($asset) { } # return the html used in top.php : -function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage){ - $img = getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, '', '', false); +function getHTMLUrl($as, $ipversion, $desc, $start, $end, $peerusage, $selected_links = array()){ + $img = getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, '', '', false, $selected_links); $result = "$img"; return($result); } # return the html used in history.php (for example) : -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; @@ -240,6 +243,14 @@ function getHTMLImg($as, $ipversion, $desc, $start, $end, $peerusage, $alt, $cla $result = "$alt $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) { diff --git a/www/headermenu.inc b/www/headermenu.inc index 0cebc10..58cd040 100644 --- a/www/headermenu.inc +++ b/www/headermenu.inc @@ -17,14 +17,6 @@ if($showpeeras == true){ endif; } -echo "Link usage: "; -foreach ($top_intervals as $interval) { - echo '' . $interval['label'] . ' | '; -} - if ($dpagename == "history"): ?>View an AS | @@ -109,11 +117,20 @@
+
+ + \n"; + echo "\n"; } ?>
"; + $tag = "link_${link['tag']}"; + + echo "
"; echo ""; if ($brighten_negative) { @@ -124,10 +141,12 @@ } echo "
"; - echo "
 " . $link['descr'] . "
+
+