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
1 change: 1 addition & 0 deletions net/vnstat/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PLUGIN_NAME= vnstat
PLUGIN_VERSION= 1.4
PLUGIN_REVISION= 1
PLUGIN_COMMENT= Network traffic monitor
PLUGIN_DEPENDS= vnstat
PLUGIN_MAINTAINER= m.muenz@gmail.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,23 @@ public function interfaceListAction()
$vnstatIfaces = array_values(array_filter(array_map('trim', explode("\n", $response))));

$opnsenseIfaces = [];
foreach (Config::getInstance()->object()->interfaces->children() as $node) {
$opnsenseIfaces[] = (string)$node->if;
foreach (Config::getInstance()->object()->interfaces->children() as $key => $node) {
$opnsenseIfaces[(string)$node->if] = [
'identifier' => (string)$key,
'description' => !empty((string)$node->descr) ? (string)$node->descr : strtoupper((string)$key),
];
}

$interfaces = array_values(array_intersect($vnstatIfaces, $opnsenseIfaces));
$interfaces = [];
foreach ($vnstatIfaces as $device) {
if (isset($opnsenseIfaces[$device])) {
$interfaces[] = [
'device' => $device,
'identifier' => $opnsenseIfaces[$device]['identifier'],
'description' => $opnsenseIfaces[$device]['description'],
];
}
}
return ["interfaces" => $interfaces];
}

Expand Down
27 changes: 16 additions & 11 deletions net/vnstat/src/opnsense/www/js/widgets/Vnstat.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export default class Vnstat extends BaseTableWidget {

async getWidgetOptions() {
const data = await this.ajaxCall(`/api/vnstat/service/${'interface_list'}`);
const ifaceOptions = (data?.interfaces ?? []).map(name => ({ value: name, label: name }));
const ifaceOptions = (data?.interfaces ?? []).map(iface => ({
value: iface.device,
label: iface.description
}));

return {
excluded_interfaces: {
Expand Down Expand Up @@ -154,22 +157,24 @@ export default class Vnstat extends BaseTableWidget {
const data = await this.ajaxCall(`/api/vnstat/service/${'interface_list'}`);
if (!data || !data.interfaces) return;

const names = data.interfaces.filter(name => !this.excludedInterfaces.includes(name));
const interfaces = data.interfaces.filter(iface => !this.excludedInterfaces.includes(iface.device));

const $select = $('#vnstat-interface-select');
$select.empty();
for (const name of names) {
$select.append($('<option></option>').val(name).text(name));
for (const iface of interfaces) {
$select.append($('<option></option>').val(iface.device).text(iface.description));
}

if (this.currentInterface && names.includes(this.currentInterface)) {
const devices = interfaces.map(iface => iface.device);
const wan = interfaces.find(iface => iface.identifier === 'wan');
if (this.currentInterface && devices.includes(this.currentInterface)) {
$select.val(this.currentInterface);
} else if (names.includes('WAN')) {
$select.val('WAN');
this.currentInterface = 'WAN';
} else if (names.length > 0) {
$select.val(names[0]);
this.currentInterface = names[0];
} else if (wan) {
$select.val(wan.device);
this.currentInterface = wan.device;
} else if (devices.length > 0) {
$select.val(devices[0]);
this.currentInterface = devices[0];
}
}

Expand Down