This commit is contained in:
Ben Smithurst 2026-05-25 09:20:04 +08:00 committed by GitHub
commit 6f472be63a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 49 additions and 17 deletions

View file

@ -32,6 +32,7 @@ namespace OPNsense\Vnstat\Api;
use OPNsense\Base\ApiMutableServiceControllerBase;
use OPNsense\Core\Backend;
use OPNsense\Core\Config;
use OPNsense\Vnstat\General;
/**
@ -51,9 +52,7 @@ class ServiceController extends ApiMutableServiceControllerBase
*/
public function hourlyAction()
{
$backend = new Backend();
$response = $backend->configdRun("vnstat hourly");
return array("response" => $response);
return $this->getStats('hourly');
}
/**
@ -62,9 +61,7 @@ class ServiceController extends ApiMutableServiceControllerBase
*/
public function dailyAction()
{
$backend = new Backend();
$response = $backend->configdRun("vnstat daily");
return array("response" => $response);
return $this->getStats('daily');
}
/**
@ -73,9 +70,7 @@ class ServiceController extends ApiMutableServiceControllerBase
*/
public function monthlyAction()
{
$backend = new Backend();
$response = $backend->configdRun("vnstat monthly");
return array("response" => $response);
return $this->getStats('monthly');
}
/**
@ -84,9 +79,7 @@ class ServiceController extends ApiMutableServiceControllerBase
*/
public function yearlyAction()
{
$backend = new Backend();
$response = $backend->configdRun("vnstat yearly");
return array("response" => $response);
return $this->getStats('yearly');
}
/**
@ -99,4 +92,31 @@ class ServiceController extends ApiMutableServiceControllerBase
$response = $backend->configdRun("vnstat resetdb");
return array("response" => $response);
}
/**
* Get stats of the given type
* @param string $type Which type of statistics to return (hourly, daily, weekly, or monthly)
* @return array
*/
private function getStats(string $type) {
$config = Config::getInstance()->object();
$backend = new Backend();
if ($config->OPNsense->vnstat->general->separate_stats == 0) {
// Separate stats are not wanted, just get totals - that's the default in vnstat itself,
// no need to specify anything here.
$response = $backend->configdpRun("vnstat", [ $type ]);
return array("response" => $response);
}
// Loop over configured interfaces, concatenating the output of each.
$result = '';
foreach (explode(',', $config->OPNsense->vnstat->general->interface) as $interface) {
// Map the OPNsense interface name to the kernel interface name that vnstat uses.
if (isset($config->interfaces->{$interface}->if)) {
$result .= $backend->configdpRun("vnstat", [ $type, $config->interfaces->{$interface}->if ]);
}
}
return array("response" => $result);
}
}

View file

@ -9,6 +9,12 @@
<id>general.interface</id>
<label>Interface</label>
<type>select_multiple</type>
<help>Set the interface to listen on.</help>
<help>Set the interface(s) to display stats for.</help>
</field>
<field>
<id>general.separate_stats</id>
<label>Separate interface stats</label>
<type>checkbox</type>
<help>If checked, separate stats are shown for each configured interface. If unchecked, a single set of stats is shown containing the sum of traffic on all configured interfaces.</help>
</field>
</form>

View file

@ -11,5 +11,9 @@
<Required>N</Required>
<Multiple>Y</Multiple>
</interface>
<separate_stats type="BooleanField">
<Default>1</Default>
<Required>Y</Required>
</separate_stats>
</items>
</model>

View file

@ -24,25 +24,25 @@ message:request Vnstat status
[hourly]
command:/usr/local/bin/vnstat -h
parameters:
parameters:%s
type:script_output
message:request Vnstat hourly status
[daily]
command:/usr/local/bin/vnstat -d
parameters:
parameters:%s
type:script_output
message:request Vnstat daily status
[monthly]
command:/usr/local/bin/vnstat -m
parameters:
parameters:%s
type:script_output
message:request Vnstat monthly status
[yearly]
command:/usr/local/bin/vnstat -y
parameters:
parameters:%s
type:script_output
message:request Vnstat yearly status

View file

@ -9,6 +9,8 @@
Interface {{ interfaces|join('+') }}
{% endif %}
# scan for new interfaces and add to DB
AlwaysAddNewInterfaces 1
# location of the database directory
DatabaseDir "/var/lib/vnstat"