Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,082 changes: 814 additions & 1,268 deletions includes/functions.php

Large diffs are not rendered by default.

114 changes: 114 additions & 0 deletions lib/PacketSizeTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

namespace Cacti\Flowview;

/**
* The HTML of the summary report: the share of the bytes seen in each
* packet size range, in two tables of fourteen and thirteen ranges.
*/
final class PacketSizeTable {
/**
* @param array<int, array{name: string, min: int, max: int, title: string}> $buckets
*/
public function __construct(
private readonly array $buckets,
) {
}

/**
* @param array<string, mixed>|null $row The bytes seen in each range, by its name.
*
* @return string
*/
public function render(?array $row): string {
$total = 0;

foreach ($this->buckets as $c) {
$total += $row[$c['name']];
}

$table = '<table class="cactiTable"><tbody>';
$table .= '<tr class="tableHeader right">';
$table .= $this->titles(0, 14);
$table .= '</tr>';
$table .= '<tr>';
$table .= $this->shares($row, $total, 0, 14);
$table .= '</tr></tbody></table>';

$table .= '<table class="cactiTable"><tbody>';
$table .= '</br>';
$table .= '<tr class="tableHeader right">';
$table .= $this->titles(14, 27);
$table .= '<th></th>';
$table .= '</tr>';

$table .= '<tr>';
$table .= $this->shares($row, $total, 14, 27);
$table .= '<td style="width:7.14%"></td>';
$table .= '</tr></tbody></table>';

return $table;
}

/**
* @param int $from
* @param int $to
*
* @return string
*/
private function titles(int $from, int $to): string {
$html = '';

for ($i = $from; $i < $to; $i++) {
$html .= '<th class="right">' . $this->buckets[$i]['title'] . '</th>';
}

return $html;
}

/**
* @param array<string, mixed>|null $row
* @param int|float $total
* @param int $from
* @param int $to
*
* @return string
*/
private function shares(?array $row, int|float $total, int $from, int $to): string {
$html = '';

for ($i = $from; $i < $to; $i++) {
$name = $this->buckets[$i]['name'];

if ($total > 0) {
$html .= '<td class="right" style="width:7.14%">' . number_format_i18n(($row[$name] / $total) * 100, 2) . ' %</td>';
} else {
$html .= '<td class="right" style="width:7.14%">' . __('N/A', 'flowview') . '</td>';
}
}

return $html;
}
}
235 changes: 235 additions & 0 deletions lib/ReportCatalog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/

namespace Cacti\Flowview;

/**
* The SQL of each statistical and printed report, by its number.
*/
final class ReportCatalog {
/**
* The statistical report $statistics, or null for one that is not known.
*
* The number is matched with ==, as the switch this replaced did, so a
* number given as a string, as the database and the request give it,
* finds its report.
*
* @param mixed $statistics
*
* @return ReportSql|null
*/
public static function statistical(mixed $statistics): ?ReportSql {
if ($statistics == 99) {
return self::packetSizes();
}

return self::find(self::statisticalReports(), $statistics);
}

/**
* The printed report $printed, or null for one that is not known.
*
* @param mixed $printed
*
* @return ReportSql|null
*/
public static function printed(mixed $printed): ?ReportSql {
return self::find(self::printedReports(), $printed);
}

/**
* The packet size ranges of the summary report, statistical report 99.
*
* @return array<int, array{name: string, min: int, max: int, title: string}>
*/
public static function packetSizeBuckets(): array {
return [
['name' => 'p0To32', 'min' => 0, 'max' => 32, 'title' => __('%d - %d Bytes', 0, 32, 'flowview')],
['name' => 'p32To64', 'min' => 32, 'max' => 64, 'title' => __('%d - %d Bytes', 32, 64, 'flowview')],
['name' => 'p64To96', 'min' => 64, 'max' => 96, 'title' => __('%d - %d Bytes', 64, 96, 'flowview')],
['name' => 'p96To128', 'min' => 96, 'max' => 128, 'title' => __('%d - %d Bytes', 96, 128, 'flowview')],
['name' => 'p128To160', 'min' => 128, 'max' => 160, 'title' => __('%d - %d Bytes', 128, 160, 'flowview')],
['name' => 'p160To192', 'min' => 160, 'max' => 192, 'title' => __('%d - %d Bytes', 160, 192, 'flowview')],
['name' => 'p192To224', 'min' => 192, 'max' => 224, 'title' => __('%d - %d Bytes', 192, 224, 'flowview')],
['name' => 'p224To256', 'min' => 224, 'max' => 256, 'title' => __('%d - %d Bytes', 224, 256, 'flowview')],
['name' => 'p256To288', 'min' => 256, 'max' => 288, 'title' => __('%d - %d Bytes', 256, 288, 'flowview')],
['name' => 'p288To320', 'min' => 288, 'max' => 320, 'title' => __('%d - %d Bytes', 288, 320, 'flowview')],
['name' => 'p320To352', 'min' => 320, 'max' => 352, 'title' => __('%d - %d Bytes', 320, 352, 'flowview')],
['name' => 'p352To384', 'min' => 352, 'max' => 384, 'title' => __('%d - %d Bytes', 352, 384, 'flowview')],
['name' => 'p384To416', 'min' => 384, 'max' => 416, 'title' => __('%d - %d Bytes', 384, 416, 'flowview')],
['name' => 'p416To448', 'min' => 416, 'max' => 448, 'title' => __('%d - %d Bytes', 416, 448, 'flowview')],
['name' => 'p448To480', 'min' => 448, 'max' => 480, 'title' => __('%d - %d Bytes', 448, 480, 'flowview')],
['name' => 'p480To512', 'min' => 480, 'max' => 512, 'title' => __('%d - %d Bytes', 480, 512, 'flowview')],
['name' => 'p512To544', 'min' => 512, 'max' => 544, 'title' => __('%d - %d Bytes', 512, 544, 'flowview')],
['name' => 'p544To576', 'min' => 544, 'max' => 576, 'title' => __('%d - %d Bytes', 544, 576, 'flowview')],
['name' => 'p576To1024', 'min' => 576, 'max' => 1024, 'title' => __('%d - %d Bytes', 576, 1024, 'flowview')],
['name' => 'p1024To1536', 'min' => 1024, 'max' => 1536, 'title' => __('%d - %d Bytes', 1024, 1536, 'flowview')],
['name' => 'p1536To2048', 'min' => 1536, 'max' => 2048, 'title' => __('%d - %d Bytes', 1536, 2048, 'flowview')],
['name' => 'p2048To2560', 'min' => 2048, 'max' => 2560, 'title' => __('%d - %d Bytes', 2048, 2560, 'flowview')],
['name' => 'p2560To3072', 'min' => 2560, 'max' => 3072, 'title' => __('%d - %d Bytes', 2560, 3072, 'flowview')],
['name' => 'p3072To3568', 'min' => 3072, 'max' => 3568, 'title' => __('%d - %d Bytes', 3072, 3568, 'flowview')],
['name' => 'p3568To4096', 'min' => 3568, 'max' => 4096, 'title' => __('%d - %d Bytes', 3568, 4096, 'flowview')],
['name' => 'p4096To4608', 'min' => 4096, 'max' => 4608, 'title' => __('%d - %d Bytes', 4096, 4680, 'flowview')],
['name' => 'p4608ToInfin', 'min' => 4680, 'max' => -1, 'title' => __esc('> %d Bytes', 4680, 'flowview')],
];
}

/**
* The summary report sums the bytes of each packet size range, and has
* no GROUP BY.
*
* @return ReportSql
*/
private static function packetSizes(): ReportSql {
$outer = '';
$inner = '';

foreach (self::packetSizeBuckets() as $el) {
$outer .= ($outer != '' ? ', ' : 'SELECT ') . 'SUM(' . $el['name'] . ') AS ' . $el['name'];
$inner .= ($inner != '' ? ', ' : 'SELECT ') .
'SUM(CASE WHEN bytes_ppacket BETWEEN ' .
$el['min'] . ' AND ' . $el['max'] . ' THEN bytes_ppacket ELSE 0 END) AS ' . $el['name'];
}

return new ReportSql(outer: $outer, inner: $inner);
}

/**
* @param array<int, array{0: int|string, 1: ReportSql}> $reports
* @param mixed $number
*
* @return ReportSql|null
*/
private static function find(array $reports, mixed $number): ?ReportSql {
foreach ($reports as [$key, $report]) {
if ($number == $key) {
return $report;
}
}

return null;
}

private static function totalsBy(string $columns): ReportSql {
$select = "SELECT $columns, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets";
$group = "GROUP BY $columns";

return new ReportSql(outer: $select, inner: $select, groupby: $group, innerGroupby: $group);
}

/**
* Each statistical report other than the summary, by number, in the
* order the switch this replaced tested them.
*
* @return array<int, array{0: int, 1: ReportSql}>
*/
private static function statisticalReports(): array {
return [
[2, self::totalsBy('src_rdomain')],
[3, self::totalsBy('dst_rdomain')],
[4, self::totalsBy('src_rdomain, dst_rdomain')],
[5, self::totalsBy('dst_port, protocol')],
[6, self::totalsBy('src_port, protocol')],
[7, self::totalsBy('src_port, dst_port, protocol')],
[8, new ReportSql(
outer: 'SELECT INET6_NTOA(dst_addr) AS dst_addr, dst_domain, dst_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
inner: 'SELECT dst_addr, dst_domain, dst_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
groupby: 'GROUP BY INET6_NTOA(dst_addr)',
innerGroupby: 'GROUP BY dst_addr',
)],
[9, new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, src_domain, src_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
inner: 'SELECT src_addr, src_domain, src_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
groupby: 'GROUP BY INET6_NTOA(src_addr)',
innerGroupby: 'GROUP BY src_addr',
)],
[10, new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, INET6_NTOA(dst_addr) AS dst_addr, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT src_addr, dst_addr, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY INET6_NTOA(src_addr), INET6_NTOA(dst_addr)',
innerGroupby: 'GROUP BY src_addr, dst_addr',
)],
[11, new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, src_domain, src_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
groupby: 'GROUP BY INET6_NTOA(src_addr)',
inner1: 'SELECT src_addr, src_domain AS src_domain, src_rdomain AS src_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
inner2: 'SELECT dst_addr AS src_addr, dst_domain AS src_domain, dst_rdomain AS src_rdomain, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets',
innerGroupby1: 'GROUP BY src_addr',
innerGroupby2: 'GROUP BY dst_addr',
)],
[12, self::totalsBy('protocol')],
[17, self::totalsBy('src_if')],
[18, self::totalsBy('dst_if')],
[23, self::totalsBy('src_if, dst_if')],
[19, self::totalsBy('src_as')],
[20, self::totalsBy('dst_as')],
[21, self::totalsBy('src_as, dst_as')],
[22, self::totalsBy('tos')],
[24, self::totalsBy('src_prefix')],
[25, self::totalsBy('dst_prefix')],
[26, self::totalsBy('src_prefix, dst_prefix')],
];
}

/**
* Each printed report, by number. The numbers are strings, as they were
* in the switch this replaced.
*
* @return array<int, array{0: string, 1: ReportSql}>
*/
private static function printedReports(): array {
return [
['1', new ReportSql(
outer: 'SELECT src_if, INET6_NTOA(src_addr) AS src_addr, dst_if, INET6_NTOA(dst_addr) AS dst_addr, protocol, src_port, dst_port, tos, flags, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT src_if, src_addr, dst_if, dst_addr, protocol, src_port, dst_port, tos, flags, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY src_if, INET6_NTOA(src_addr), dst_if, INET6_NTOA(dst_addr), protocol, src_port, dst_port, tos, flags',
innerGroupby: 'GROUP BY src_if, src_addr, dst_if, dst_addr, protocol, src_port, dst_port, tos, flags',
)],
['4', new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, INET6_NTOA(dst_addr) AS dst_addr, protocol, src_as, dst_as, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT src_addr, dst_addr, protocol, src_as, dst_as, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY INET6_NTOA(src_addr), INET6_NTOA(dst_addr), protocol, src_as, dst_as',
innerGroupby: 'GROUP BY src_addr, dst_addr, protocol, src_as, dst_as',
)],
['5', new ReportSql(
outer: 'SELECT start_time, end_time, src_if, INET6_NTOA(src_addr) AS src_addr, src_port, dst_if, INET6_NTOA(dst_addr) AS dst_addr, dst_port, protocol, flags, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT start_time, end_time, src_if, src_addr, src_port, dst_if, dst_addr, dst_port, protocol, flags, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY start_time, end_time, src_if, INET6_NTOA(src_addr), src_port, dst_if, INET6_NTOA(dst_addr), dst_port, protocol, flags',
innerGroupby: 'GROUP BY start_time, end_time, src_if, src_addr, src_port, dst_if, dst_addr, dst_port, protocol, flags',
)],
['6', new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, INET6_NTOA(dst_addr) AS dst_addr, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT src_addr, dst_addr, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY INET6_NTOA(src_addr), INET6_NTOA(dst_addr)',
innerGroupby: 'GROUP BY src_addr, dst_addr',
)],
['7', new ReportSql(
outer: 'SELECT INET6_NTOA(src_addr) AS src_addr, src_port, protocol, INET6_NTOA(dst_addr) AS dst_addr, dst_port, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
inner: 'SELECT src_addr, src_port, protocol, dst_addr, dst_port, SUM(flows) AS flows, SUM(bytes) AS bytes, SUM(packets) AS packets, src_domain, dst_domain',
groupby: 'GROUP BY INET6_NTOA(src_addr), src_port, protocol, INET6_NTOA(dst_addr), dst_port',
innerGroupby: 'GROUP BY src_addr, src_port, protocol, dst_addr, dst_port',
)],
];
}
}
Loading