Skip to content

Commit

Permalink
Use PHP RRD function for summary. Fixes #12338
Browse files Browse the repository at this point in the history
Use PECL rrd_fetch() rather than making a shell exec for rrdtool. The
rrdtool binary outputs data improperly on ARMv7.
  • Loading branch information
jim-p committed May 26, 2023
1 parent 0049587 commit 961bbfe
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sysutils/pfSense-pkg-RRD_Summary/Makefile
@@ -1,7 +1,7 @@
# $FreeBSD$

PORTNAME= pfSense-pkg-RRD_Summary
PORTVERSION= 2.1
PORTVERSION= 2.2
CATEGORIES= sysutils
MASTER_SITES= # empty
DISTFILES= # empty
Expand Down
Expand Up @@ -47,32 +47,49 @@
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)
/* rrd_fetch() outputs a multi-level array first by DS name, then timestamp */
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}", $traffic);
$rrd = "/var/db/rrd/{$rrd}";
$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; }

$rrd_options = array( 'AVERAGE', '-r', $resolution, '-s', $start, '-e', $end );
$traffic = rrd_fetch($rrd, $rrd_options);

if (!is_array($traffic) ||
empty($traffic) ||
!is_array($traffic["data"])) {
return [];
}
// 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;

$data = [
"first" => time(),
"last" => 0,
"total_in" => 0,
"total_out" => 0
];

foreach ($traffic["data"] as $dsname => $dsdata) {
$dstotal = 0;
foreach ($dsdata as $timestamp => $d) {
if (is_nan($d)) {
continue;
}
$data["first"] = min((int) $timestamp, (int) $data["first"]);
$data["last"] = max((int) $timestamp, (int) $data["last"]);
$dstotal += ($d / $divisor) * $resolution;
}
$data[$dsname] = $dstotal;
}

foreach (array_keys($traffic["data"]) as $ds) {
if ($ds != 'outblock' ||
$ds != 'outblock6') {
if (substr($ds, 0, 2) == 'in') {
$data["total_in"] += $data[$ds];
} elseif (substr($ds, 0, 3) == 'out') {
$data["total_out"] += $data[$ds];
}
}
}
return $data;
}
Expand Down Expand Up @@ -103,7 +120,7 @@ function print_rrd_summary($rrd, $units, $startyear, $startday) {
<td><?=sprintf("%0.2f %s", $data["total_in"] + $data["total_out"], $u[$units]); ?></td>
</tr>
<?php
}
}
}
}

Expand Down

0 comments on commit 961bbfe

Please sign in to comment.