Skip to content

Commit

Permalink
Wireguard application graph cleanup and new wireguard interface/globa…
Browse files Browse the repository at this point in the history
…l metrics. (librenms#15847)
  • Loading branch information
bnerickson authored Mar 7, 2024
1 parent c855d6c commit d7c31e0
Show file tree
Hide file tree
Showing 7 changed files with 354 additions and 121 deletions.
6 changes: 6 additions & 0 deletions includes/html/graphs/application/wireguard-common.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$name = 'wireguard';
$polling_type = 'app';
$bigdescrlen = 100;
$smalldescrlen = 100;
77 changes: 48 additions & 29 deletions includes/html/graphs/application/wireguard_time.inc.php
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
<?php

$name = 'wireguard';
$polling_type = 'app';

if (isset($vars['interface']) && isset($vars['client'])) {
$interface = $vars['interface'];
$client = $vars['client'];
$interface_client = $vars['interface'] . '-' . $vars['client'];
} else {
$interface_client_list = Rrd::getRrdApplicationArrays($device, $app->app_id, $name);
$interface_client = $interface_client_list[0] ?? '';
}
require 'wireguard-common.inc.php';

$unit_text = 'Minutes';
$interface_client_map = $app->data['mappings'] ?? [];
$colours = 'psychedelic';
$metric_desc = 'Last Handshake';
$metric_name = 'minutes_since_last_handshake';
$rrd_list = [];
$rrdArray = [];
$scale_min = 0;
$unitlen = 7;

if (isset($vars['interface']) && isset($vars['client'])) {
// This section draws the individual graphs in the device application page
// displaying the SPECIFIED wireguard interface and client metric.
$wg_intf_client = $vars['interface'] . '-' . $vars['client'];
$rrdArray[$wg_intf_client] = [
$metric_name => ['descr' => $metric_desc],
];
} elseif (! isset($vars['interface']) && ! isset($vars['client'])) {
// This section draws the graph for the application-specific pages
// displaying ALL wireguard interfaces' clients' metrics.
foreach ($interface_client_map as $wg_intf => $wg_client_list) {
foreach ($wg_client_list as $wg_client) {
$wg_intf_client = $wg_intf . '-' . $wg_client;
$rrdArray[$wg_intf_client] = [
$metric_name => [
'descr' => $wg_intf_client . ' ' . $metric_desc,
],
];
}
}
}

$rrdArray = [
'minutes_since_last_handshake' => ['descr' => 'Last Handshake'],
];
if (! $rrdArray) {
graph_error('No Data to Display', 'No Data');
}

$rrd_filename = Rrd::name($device['hostname'], [
$polling_type,
$name,
$app->app_id,
$interface_client,
]);
$i = 0;
foreach ($rrdArray as $wg_intf_client => $wg_metric) {
$rrd_filename = Rrd::name($device['hostname'], [
$polling_type,
$name,
$app->app_id,
$wg_intf_client,
]);

if (Rrd::checkRrdExists($rrd_filename)) {
foreach ($rrdArray as $rrdVar => $rrdValues) {
$rrd_list[] = [
'filename' => $rrd_filename,
'descr' => $rrdValues['descr'],
'ds' => $rrdVar,
];
if (Rrd::checkRrdExists($rrd_filename)) {
$rrd_list[$i]['filename'] = $rrd_filename;
$rrd_list[$i]['descr'] = $wg_metric[$metric_name]['descr'];
$rrd_list[$i]['ds'] = $metric_name;
$i++;
} else {
graph_error('No Data file ' . basename($rrd_filename), 'No Data');
}
} else {
d_echo('RRD ' . $rrd_filename . ' not found');
}

require 'includes/html/graphs/generic_multi_line_exact_numbers.inc.php';
69 changes: 42 additions & 27 deletions includes/html/graphs/application/wireguard_traffic.inc.php
Original file line number Diff line number Diff line change
@@ -1,44 +1,59 @@
<?php

$name = 'wireguard';
$polling_type = 'app';

if (isset($vars['interface']) && isset($vars['client'])) {
$interface = $vars['interface'];
$client = $vars['client'];
$interface_client = $vars['interface'] . '-' . $vars['client'];
} else {
$interface_client_list = Rrd::getRrdApplicationArrays($device, $app->app_id, $name);
$interface_client = $interface_client_list[0] ?? '';
}

$unit_text = 'Bytes';

$ds_in = 'bytes_rcvd';
$in_text = 'Rcvd';
$ds_out = 'bytes_sent';
$out_text = 'Sent';
require 'wireguard-common.inc.php';

$unit_text = 'Bytes/s';
$format = 'bytes';
$print_total = true;

$in_text = 'In';
$out_text = 'Out';
$colour_area_in = 'FF3300';
$colour_line_in = 'FF0000';
$colour_area_out = 'FF6633';
$colour_line_out = 'CC3300';

$colour_area_in_max = 'FF6633';
$colour_area_out_max = 'FF9966';

$rrd_filename = Rrd::name($device['hostname'], [
$polling_type,
$name,
$app->app_id,
$interface_client,
]);
if (! isset($vars['interface']) && ! isset($vars['client'])) {
// This section is called if we're being asked to graph
// the host's wireguard global metrics.
$ds_in = 'bytes_rcvd_total';
$ds_out = 'bytes_sent_total';
} elseif (isset($vars['interface']) && isset($vars['client'])) {
// This section is called if we're being asked to graph
// a wireguard interface's client metrics.
$flattened_name = $vars['interface'] . '-' . $vars['client'];
$ds_in = 'bytes_rcvd';
$ds_out = 'bytes_sent';
} elseif (isset($vars['interface'])) {
// This section is called if we're being asked to graph
// a wireguard interface's metrics.
$flattened_name = $vars['interface'];
$ds_in = 'bytes_rcvd_total_intf';
$ds_out = 'bytes_sent_total_intf';
}

if (! isset($vars['interface']) && ! isset($vars['client'])) {
$rrd_filename = Rrd::name($device['hostname'], [
$polling_type,
$name,
$app->app_id,
]);
} elseif (isset($vars['interface'])) {
$rrd_filename = Rrd::name($device['hostname'], [
$polling_type,
$name,
$app->app_id,
$flattened_name,
]);
}

if (! isset($rrd_filename)) {
graph_error('No Data to Display', 'No Data');
}

if (! Rrd::checkRrdExists($rrd_filename)) {
d_echo('RRD ' . $rrd_filename . ' not found');
graph_error('No Data file ' . basename($rrd_filename), 'No Data');
}

require 'includes/html/graphs/generic_duplex.inc.php';
126 changes: 98 additions & 28 deletions includes/html/pages/device/apps/wireguard.inc.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
<?php

/**
* Builds a graph array and outputs the graph.
*
* @param string $gtype
* @param string $app_id
* @param null|string $interface
* @param null|string $client
* @param string $gtext
*/
function wireguard_graph_printer($gtype, $app_id, $interface, $client, $gtext)
{
$graph_type = $gtype;
$graph_array['height'] = '100';
$graph_array['width'] = '215';
$graph_array['to'] = time();
$graph_array['id'] = $app_id;
$graph_array['type'] = 'application_' . $gtype;
if (! is_null($interface)) {
$graph_array['interface'] = $interface;
}
if (! is_null($client)) {
$graph_array['client'] = $client;
}
echo '<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">' .
$gtext .
'</h3>
</div>
<div class="panel-body">
<div class="row">';
include 'includes/html/print-graphrow.inc.php';
echo '</div>';
echo '</div>';
echo '</div>';
}

$link_array = [
'page' => 'device',
'device' => $device['device_id'],
'tab' => 'apps',
'app' => 'wireguard',
];

$interface_client_map = $app->data['mappings'] ?? [];
$graph_map = [
'interface' => [
'clients' => [],
'total' => [],
],
'total' => [],
];

print_optionbar_start();

echo generate_link('All Interfaces', $link_array);
echo ' | Interfaces: ';

$interface_client_map = $app->data['mappings'] ?? [];

// generate interface links
// generate interface links on the host application page
$i = 0;
foreach ($interface_client_map as $interface => $client_list) {
$label =
Expand All @@ -32,14 +76,25 @@

print_optionbar_end();

// build the interface/client -> graph map
// generates the global wireguard graph mapping
if (! isset($vars['interface'])) {
$graph_map['total'] = [
'wireguard_traffic' => 'Wireguard Total Traffic',
];
}

foreach ($interface_client_map as $interface => $client_list) {
if (
! isset($vars['interface']) ||
(isset($vars['interface']) && $interface == $vars['interface'])
) {
// generates the interface graph mapping
$graph_map['interface']['total'][$interface] = [
'wireguard_traffic' => $interface . ' ' . 'Total Traffic',
];
foreach ($client_list as $client) {
$interface_client_map[$interface][$client] = [
// generates the interface+client graph mapping
$graph_map['interface']['clients'][$interface][$client] = [
'wireguard_traffic' => $interface . ' ' . $client . ' Traffic',
'wireguard_time' => $interface .
' ' .
Expand All @@ -50,29 +105,44 @@
}
}

// generate graphs on a per-interface, per-client basis
foreach ($interface_client_map as $interface => $client_list) {
foreach ($client_list as $client => $graphs) {
foreach ($graphs as $gtype => $gtext) {
$graph_type = $gtype;
$graph_array['height'] = '100';
$graph_array['width'] = '215';
$graph_array['to'] = time();
$graph_array['id'] = $app['app_id'];
$graph_array['type'] = 'application_' . $gtype;
$graph_array['interface'] = $interface;
$graph_array['client'] = $client;

echo '<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">' . $gtext . '</h3>
</div>
<div class="panel-body">
<div class="row">';
include 'includes/html/print-graphrow.inc.php';
echo '</div>';
echo '</div>';
echo '</div>';
// print graphs
foreach ($graph_map as $category => $category_map) {
foreach ($category_map as $subcategory => $subcategory_map) {
if ($category === 'total') {
// print graphs for global wireguard metrics
wireguard_graph_printer(
$subcategory,
$app['app_id'],
null,
null,
$subcategory_map
);
} elseif ($category === 'interface') {
foreach ($subcategory_map as $interface => $interface_map) {
foreach ($interface_map as $client => $client_map) {
if ($subcategory === 'total') {
// print graphs for wireguard interface metrics
wireguard_graph_printer(
$client,
$app['app_id'],
$interface,
null,
$client_map
);
} elseif ($subcategory === 'clients') {
foreach ($client_map as $gtype => $gtext) {
// print graphs for wireguard interface+client metrics
wireguard_graph_printer(
$gtype,
$app['app_id'],
$interface,
$client,
$gtext
);
}
}
}
}
}
}
}
Loading

0 comments on commit d7c31e0

Please sign in to comment.