diff --git a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/ServiceController.php b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/ServiceController.php index a8fbebb7c2..bbf502baa9 100644 --- a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/ServiceController.php +++ b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/ServiceController.php @@ -32,6 +32,7 @@ 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 @@ public function hourlyAction() */ public function dailyAction() { - $backend = new Backend(); - $response = $backend->configdRun("vnstat daily"); - return array("response" => $response); + return $this->getStats('daily'); } /** @@ -73,9 +70,7 @@ public function dailyAction() */ public function monthlyAction() { - $backend = new Backend(); - $response = $backend->configdRun("vnstat monthly"); - return array("response" => $response); + return $this->getStats('monthly'); } /** @@ -84,9 +79,7 @@ public function monthlyAction() */ public function yearlyAction() { - $backend = new Backend(); - $response = $backend->configdRun("vnstat yearly"); - return array("response" => $response); + return $this->getStats('yearly'); } /** @@ -99,4 +92,31 @@ public function resetdbAction() $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()->toArray(); + $backend = new Backend(); + + if (isset($config['OPNsense']['vnstat']['general']['separate_stats']) && !$config['OPNsense']['vnstat']['general']['separate_stats']) { + // 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); + } } diff --git a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml index cd1f8ded82..76eaea5155 100644 --- a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml +++ b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml @@ -9,6 +9,12 @@ general.interface select_multiple - Set the interface to listen on. + Set the interface(s) to display stats for. + + + general.separate_stats + + checkbox + 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. diff --git a/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.xml b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.xml index fa94d7dc05..ed4ac8d518 100644 --- a/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.xml +++ b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.xml @@ -4,12 +4,16 @@ 0.0.1 - 0 + 0 Y N - Y + Y + + 1 + Y + diff --git a/net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf b/net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf index 4a4fcbd64e..89a149bf57 100644 --- a/net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf +++ b/net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf @@ -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 diff --git a/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf index 8745f976d3..abc9958d3f 100644 --- a/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf +++ b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf @@ -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"