Skip to content

Commit

Permalink
Merge pull request #475 from shonjir/rrd
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgarga committed Jan 17, 2018
2 parents 17a7aeb + 20db740 commit 26761fa
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 48 deletions.
3 changes: 1 addition & 2 deletions sysutils/pfSense-pkg-RRD_Summary/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# $FreeBSD$

PORTNAME= pfSense-pkg-RRD_Summary
PORTVERSION= 1.3.2
PORTREVISION= 2
PORTVERSION= 2.0
CATEGORIES= sysutils
MASTER_SITES= # empty
DISTFILES= # empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<title>Status: RRD Summary</title>
<menu>
<name>RRD Summary</name>
<tooltiptext>Display total amount of traffic passed In/Out during this and the previous month.</tooltiptext>
<tooltiptext>Estimated month-over-month traffic passed In/Out during the specified period.</tooltiptext>
<section>Status</section>
<url>/status_rrd_summary.php</url>
</menu>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package>
<name>RRD Summary</name>
<internal_name>RRD_Summary</internal_name>
<descr><![CDATA[RRD Summary Page, which will give a total amount of traffic passed In/Out during this and the previous month.]]></descr>
<descr><![CDATA[RRD Summary Page, which will give estimated month-over-month traffic passed In/Out during the specified period.]]></descr>
<version>%%PKGVERSION%%</version>
<configurationfile>rrd-summary.xml</configurationfile>
</package>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,87 @@
*/
require_once("guiconfig.inc");

$rrds = glob("/var/db/rrd/*-traffic.rrd");
$startday = isset($_POST['startday']) ? $_POST['startday'] : "01";
$rrd = isset($_POST['rrd']) ? $_POST['rrd'] : "wan-traffic.rrd";

$start = "00 " . date("m/{$startday}/Y");
$lastmonthstart = "00 " . date("m/d/Y", strtotime("-1 month", strtotime(date("m/{$startday}/Y"))));
$lastmonthend = "00 " . date("m/d/Y", strtotime("-1 second", strtotime(date("m/{$startday}/Y"))));

$thismonth = fetch_rrd_summary($rrd, $start, "now");
$lastmonth = fetch_rrd_summary($rrd, $lastmonthstart, $lastmonthend, 24*60*60);

function fetch_rrd_summary($rrd, $start, $end, $resolution=60*60) {
// Using too small a divisor for traffic sums will likely exceed PHP_MAX_INT
$unitlist = array("MiB" => 2, "GiB" => 3, "TiB" => 4, "PiB" => 5);

// validate interface
$iflist = get_configured_interface_list();
$interface = filter_input(INPUT_POST, "interface", FILTER_SANITIZE_STRING);
$interface = in_array($interface, $iflist) ? $interface : "wan";

// get rrd bounds for interface
$rrd = "{$interface}-traffic.rrd";
$data = fetch_rrd_summary($rrd, "-10y", "now");
$firstyear = date("Y", ($data["first"] > 0) ? $data["first"] : time() );
$lastyear = date("Y", ($data["last"] > 0) ? $data["last"] : time() );

// validate year
$startyear = filter_input(INPUT_POST, "startyear", FILTER_VALIDATE_INT,
array("options" => array("min_range" => 0, "max_range" => $lastyear)) );
$startyear = ($startyear >= $firstyear || $startyear == "0") ? $startyear : $lastyear;
// validate startday
$startday = filter_input(INPUT_POST, "startday", FILTER_VALIDATE_INT,
array("options" => array("min_range" => 1, "max_range" => 28)) );
$startday = ($startday > 0) ? $startday : 1;
// validate units
$units = filter_input(INPUT_POST, "units", FILTER_VALIDATE_INT,
array("options" => array("min_range" => min(array_values($unitlist)), "max_range" => max(array_values($unitlist)) )) );
$units = in_array($units, $unitlist) ? $units : $unitlist["GiB"];

// 1:timestamp 2:inpass 3:outpass 4:inblock 5:outblock 6:inpass6 7:outpass6 8:inblock6 9:outblock6
// total_in = $2 + $4 + $6 + $8; total_out = $3 + $7 (blocked outbound traffic is excluded)
function fetch_rrd_summary($rrd, $start, $end, $units=2, $resolution=24*60*60) {
$traffic = array();
$rrd = escapeshellarg("/var/db/rrd/{$rrd}");
$start = escapeshellarg($start);
$end = escapeshellarg($end);
exec("/usr/local/bin/rrdtool fetch {$rrd} AVERAGE -r {$resolution} -s {$start} -e {$end} | grep -v nan | awk '{ sum1 += $2/(1024*1024) + $4/(1024*1024) + $6/(1024*1024) + $8/(1024*1024); sum2 += $3/(1024*1024) + $5/(1024*1024) + $7/(1024*1024) + $9/(1024*1024); } END { printf \"%u|%u\", sum1*{$resolution}, sum2*{$resolution}; }'", $traffic);
return explode('|', trim($traffic[0]));
exec("/usr/local/bin/rrdtool fetch {$rrd} AVERAGE -r {$resolution} -s {$start} -e {$end}", $traffic);
$divisor = 1024 ** $units;
$t_keys = preg_split("/\s+/", $traffic[0]); // grab keys
//print "time=$t_first st=$start end=$end div=$divisor res=$resolution";
$traffic = preg_grep("/^[0-9]+:/", $traffic); // select data rows
$traffic = preg_grep("/nan/", $traffic, PREG_GREP_INVERT); // filter nan rows
$data = array( "first" => time() );
foreach ( $traffic as $t ) {
$t = preg_split("/[\s:]+/", $t);
if (count($t) != count($t_keys)) continue; // error: field mismatch
$data["first"] = min($t[0], $data["first"]);
$data["last"] = max($t[0], $data["last"]);
foreach ($t_keys as $i => $k) { $data[$k] += ($t[$i] / $divisor) * $resolution; }
}
// Adjust for resolution
foreach ($t_keys as $k) {
if (preg_match("/^outblock/", $k)) continue;
$data["total_in"] += preg_match("/^in/", $k) ? $data[$k] : 0;
$data["total_out"] += preg_match("/^out/", $k) ? $data[$k] : 0;
}
return $data;
}

function print_rrd_summary_table($data) { ?>
<div class="table-responsive">
<table class="table table-striped table-hover table-condensed" id="rrdsummary">
<thead>
<tr><th>Direction</th><th>Bandwidth</th></tr>
</thead>
<tbody>
<tr><td>In</td><td align="right"><?=$data[0]; ?> MBytes</td></tr>
<tr><td>Out</td><td align="right"><?=$data[1]; ?> MBytes</td></tr>
<tr><td>Total</td><td align="right"><?=$data[0] + $data[1]; ?> MBytes</td></tr>
</tbody>
</table>
</div>
<?php
function print_rrd_summary($rrd, $units, $startyear, $startday) {
$data = fetch_rrd_summary($rrd, "-10y", "now");
$first = $data["first"];
$last = $data["last"];
global $unitlist; $u = array_flip($unitlist);
foreach (range(date("Y", $last), date("Y", $first), 1) as $year) {
if ($startyear > 0 && $startyear != $year) continue;
foreach (range(12, 1, -1) as $month) {
$start = strtotime(date("{$month}/{$startday}/{$year}"));
$end = strtotime("-1 second", strtotime("+1 month", $start));
if ($start > $last || $end < $first) continue;
if ($start < $first) $start = $first;
if ($end > $last) $end = $last;
$data = fetch_rrd_summary($rrd, "epoch+{$start}s", "epoch+{$end}s", $units, 24*60*60);
?>
<tr>
<td><?=date("Y-m-d", $start); ?> to <?=date("m-d-Y", $end); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_in"], $u[$units]); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_out"], $u[$units]); ?></td>
<td><?=sprintf("%0.2f %s", $data["total_in"] + $data["total_out"], $u[$units]); ?></td>
</tr>
<?php
}
}
}

$pgtitle = array("Status", "RRD Summary");
Expand All @@ -64,17 +111,30 @@ function print_rrd_summary_table($data) { ?>

$section = new Form_Section('Select RRD Parameters');

$rrd_options = array();
foreach ($rrds as $r) {
$r = basename($r);
$rrd_options[$r] = $r;
$if_options = array();
foreach ($iflist as $i) {
$if_options[$i] = $i;
}

$section->addInput(new Form_Select(
'rrd',
'RRD File',
$rrd,
$rrd_options
'interface',
'Interface',
$interface,
$if_options
));

$section->addInput(new Form_Select(
'units',
'Units',
$units,
array_flip($unitlist)
));

$section->addInput(new Form_Select(
'startyear',
'Year',
$startyear,
array("0" => "All") + array_combine(range($lastyear, $firstyear, -1), range($lastyear, $firstyear, -1))
));

$section->addInput(new Form_Select(
Expand All @@ -93,18 +153,29 @@ function print_rrd_summary_table($data) { ?>
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title"><?=gettext("RRD Summary")?></h2></div>
<div class="panel-body">
<strong>This Month (to date, does not include this hour, starting at day <?=$startday; ?>):</strong>
<?php print_rrd_summary_table($thismonth); ?>
<br/>
<strong>Last Month:</strong>
<?php print_rrd_summary_table($lastmonth); ?>
<div class="table-responsive">
<strong>Estimated monthly traffic for given time period beginning on day <?=$startday; ?> of each month</strong>
<table class="table table-striped table-hover table-condensed sortable-theme-bootstrap" data-sortable id="rrdsummary">
<thead>
<tr>
<th data-sortable-type="alpha"><?=gettext("Period")?></th>
<th data-sortable-type="numeric"><?=gettext("Avg Traffic In")?></th>
<th data-sortable-type="numeric"><?=gettext("Avg Traffic Out")?></th>
<th data-sortable-type="numeric"><?=gettext("Est Total Traffic")?></th>
</tr>
</thead>
<tbody>
<?php print_rrd_summary($rrd, $units, $startyear, $startday); ?>
</tbody>
</table>
</div>
</div>
</div>

<script type="text/javascript">
//<![CDATA[
events.push(function(){
$('#rrd, #startday').on('change', function(){
$('#interface, #units, #startyear, #startday').on('change', function(){
$(this).parents('form').submit();
});
});
Expand Down
4 changes: 2 additions & 2 deletions sysutils/pfSense-pkg-RRD_Summary/pkg-descr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
RRD Summary Page, which will give a total amount of traffic passed In/Out during
this and the previous month.
RRD Summary Page, which will give estimated month-over-month traffic passed
In/Out during the specified period.

0 comments on commit 26761fa

Please sign in to comment.