From 3d11b3d40522b182f12bedf474c335bca3c48501 Mon Sep 17 00:00:00 2001 From: Michael Date: Fri, 14 Sep 2018 15:32:37 +0200 Subject: [PATCH] net/vnstat: new plugin (#833) (cherry picked from commit 7f5e8230035eec420ea07f817a83b7b7666fd6a0) (cherry picked from commit 0ce92100d994d100ee835ac923eea672bb8b8d8f) (cherry picked from commit ba17f126557ee5b66ab30af8d15f40ce3eb4e497) --- README.md | 1 + net/vnstat/Makefile | 8 + net/vnstat/pkg-descr | 8 + .../src/etc/inc/plugins.inc.d/vnstat.inc | 55 +++++++ .../OPNsense/Vnstat/Api/GeneralController.php | 39 +++++ .../OPNsense/Vnstat/Api/ServiceController.php | 91 +++++++++++ .../OPNsense/Vnstat/GeneralController.php | 38 +++++ .../OPNsense/Vnstat/forms/general.xml | 14 ++ .../app/models/OPNsense/Vnstat/ACL/ACL.xml | 9 ++ .../app/models/OPNsense/Vnstat/General.php | 35 ++++ .../app/models/OPNsense/Vnstat/General.xml | 14 ++ .../app/models/OPNsense/Vnstat/Menu/Menu.xml | 5 + .../app/views/OPNsense/Vnstat/general.volt | 113 +++++++++++++ .../opnsense/scripts/OPNsense/Vnstat/setup.sh | 9 ++ .../conf/actions.d/actions_vnstat.conf | 47 ++++++ .../templates/OPNsense/Vnstat/+TARGETS | 2 + .../service/templates/OPNsense/Vnstat/vnstat | 6 + .../templates/OPNsense/Vnstat/vnstat.conf | 153 ++++++++++++++++++ 18 files changed, 647 insertions(+) create mode 100644 net/vnstat/Makefile create mode 100644 net/vnstat/pkg-descr create mode 100644 net/vnstat/src/etc/inc/plugins.inc.d/vnstat.inc create mode 100644 net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/GeneralController.php create mode 100644 net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/ServiceController.php create mode 100644 net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/GeneralController.php create mode 100644 net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml create mode 100644 net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/ACL/ACL.xml create mode 100644 net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.php create mode 100644 net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.xml create mode 100644 net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/Menu/Menu.xml create mode 100644 net/vnstat/src/opnsense/mvc/app/views/OPNsense/Vnstat/general.volt create mode 100755 net/vnstat/src/opnsense/scripts/OPNsense/Vnstat/setup.sh create mode 100644 net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf create mode 100644 net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/+TARGETS create mode 100644 net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat create mode 100644 net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf diff --git a/README.md b/README.md index e8d349d06..4f588d96e 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,7 @@ net/relayd -- Relayd Load Balancer net/shadowsocks -- Secure socks5 proxy net/siproxd -- Siproxd is a proxy daemon for the SIP protocol net/upnp -- Universal Plug and Play Service +net/vnstat -- vnStat is a console-based network traffic monitor net/wireguard -- WireGuard VPN service net/wol -- Wake on LAN Service net/zerotier -- Virtual Networks That Just Work diff --git a/net/vnstat/Makefile b/net/vnstat/Makefile new file mode 100644 index 000000000..0993d08e6 --- /dev/null +++ b/net/vnstat/Makefile @@ -0,0 +1,8 @@ +PLUGIN_NAME= vnstat +PLUGIN_VERSION= 0.1 +PLUGIN_COMMENT= vnStat is a console-based network traffic monitor +PLUGIN_DEPENDS= vnstat +PLUGIN_MAINTAINER= m.muenz@gmail.com +PLUGIN_DEVEL= yes + +.include "../../Mk/plugins.mk" diff --git a/net/vnstat/pkg-descr b/net/vnstat/pkg-descr new file mode 100644 index 000000000..7e2b955c5 --- /dev/null +++ b/net/vnstat/pkg-descr @@ -0,0 +1,8 @@ +vnStat is a console-based network traffic monitor for Linux +and BSD that keeps a log of network traffic for the selected +interface(s). It uses the network interface statistics +provided by the kernel as information source. This means +that vnStat won't actually be sniffing any traffic and also +ensures light use of system resources. + +WWW: https://humdi.net/vnstat/ diff --git a/net/vnstat/src/etc/inc/plugins.inc.d/vnstat.inc b/net/vnstat/src/etc/inc/plugins.inc.d/vnstat.inc new file mode 100644 index 000000000..9c0521acc --- /dev/null +++ b/net/vnstat/src/etc/inc/plugins.inc.d/vnstat.inc @@ -0,0 +1,55 @@ +enabled == '1'; +} + +function vnstat_services() +{ + $services = array(); + + if (!vnstat_enabled()) { + return $services; + } + + $services[] = array( + 'description' => gettext('vnStat Daemon'), + 'configd' => array( + 'restart' => array('vnstat restart'), + 'start' => array('vnstat start'), + 'stop' => array('vnstat stop'), + ), + 'name' => 'vnstatd', + 'pidfile' => '/var/run/vnstat/vnstat.pid' + ); + + return $services; +} diff --git a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/GeneralController.php b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/GeneralController.php new file mode 100644 index 000000000..d2858311e --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/Api/GeneralController.php @@ -0,0 +1,39 @@ +configdRun("vnstat hourly"); + return array("response" => $response); + } + + /** + * list daily statistics + * @return array + */ + public function dailyAction() + { + $backend = new Backend(); + $response = $backend->configdRun("vnstat daily"); + return array("response" => $response); + } + + /** + * list weekly statistics + * @return array + */ + public function weeklyAction() + { + $backend = new Backend(); + $response = $backend->configdRun("vnstat weekly"); + return array("response" => $response); + } + + /** + * list monthly statistics + * @return array + */ + public function monthlyAction() + { + $backend = new Backend(); + $response = $backend->configdRun("vnstat monthly"); + return array("response" => $response); + } +} diff --git a/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/GeneralController.php b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/GeneralController.php new file mode 100644 index 000000000..1e71fd2a5 --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/GeneralController.php @@ -0,0 +1,38 @@ +view->generalForm = $this->getForm("general"); + $this->view->pick('OPNsense/Vnstat/general'); + } +} 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 new file mode 100644 index 000000000..dd8a6c910 --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/controllers/OPNsense/Vnstat/forms/general.xml @@ -0,0 +1,14 @@ +
+ + general.enabled + + checkbox + This will activate the vnstat daemon. + + + general.interface + + dropdown + Set the interface to listen on. + +
diff --git a/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/ACL/ACL.xml b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/ACL/ACL.xml new file mode 100644 index 000000000..ee1163bf1 --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/ACL/ACL.xml @@ -0,0 +1,9 @@ + + + Services: Vnstat + + ui/vnstat/* + api/vnstat/* + + + diff --git a/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.php b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.php new file mode 100644 index 000000000..87d39e142 --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/General.php @@ -0,0 +1,35 @@ + + //OPNsense/vnstat/general + Vnstat configuration + 0.0.1 + + + 0 + Y + + + N + + + diff --git a/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/Menu/Menu.xml b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/Menu/Menu.xml new file mode 100644 index 000000000..1dcdcdf69 --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/models/OPNsense/Vnstat/Menu/Menu.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/net/vnstat/src/opnsense/mvc/app/views/OPNsense/Vnstat/general.volt b/net/vnstat/src/opnsense/mvc/app/views/OPNsense/Vnstat/general.volt new file mode 100644 index 000000000..36414773e --- /dev/null +++ b/net/vnstat/src/opnsense/mvc/app/views/OPNsense/Vnstat/general.volt @@ -0,0 +1,113 @@ +{# + +OPNsense® is Copyright © 2014 – 2018 by Deciso B.V. +This file is Copyright © 2018 by Michael Muenz +All rights reserved. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, +INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, +OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +#} + + + + +
+
+
+ {{ partial("layout_partials/base_form",['fields':generalForm,'id':'frm_general_settings'])}} +
+
+ +
+
+
+
+

+    
+
+

+    
+
+

+    
+
+

+    
+
+ + diff --git a/net/vnstat/src/opnsense/scripts/OPNsense/Vnstat/setup.sh b/net/vnstat/src/opnsense/scripts/OPNsense/Vnstat/setup.sh new file mode 100755 index 000000000..02d682ad7 --- /dev/null +++ b/net/vnstat/src/opnsense/scripts/OPNsense/Vnstat/setup.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +mkdir -p /var/run/vnstat +chown -R vnstat:vnstat /var/run/vnstat +chmod 755 /var/run/vnstat + +mkdir -p /var/lib/vnstat +chown -R vnstat:vnstat /var/lib/vnstat +chmod 755 /var/lib/vnstat 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 new file mode 100644 index 000000000..60d04b96b --- /dev/null +++ b/net/vnstat/src/opnsense/service/conf/actions.d/actions_vnstat.conf @@ -0,0 +1,47 @@ +[start] +command:/usr/local/opnsense/scripts/OPNsense/Vnstat/setup.sh;/usr/local/etc/rc.d/vnstat start +parameters: +type:script +message:starting Vnstat + +[stop] +command:/usr/local/etc/rc.d/vnstat stop; exit 0 +parameters: +type:script +message:stopping Vnstat + +[restart] +command:/usr/local/opnsense/scripts/OPNsense/Vnstat/setup.sh;/usr/local/etc/rc.d/vnstat restart +parameters: +type:script +message:restarting Vnstat + +[status] +command:/usr/local/etc/rc.d/vnstat status;exit 0 +parameters: +type:script_output +message:request Vnstat status + +[hourly] +command:/usr/local/bin/vnstat -h +parameters: +type:script_output +message:request Vnstat hourly status + +[daily] +command:/usr/local/bin/vnstat -d +parameters: +type:script_output +message:request Vnstat daily status + +[weekly] +command:/usr/local/bin/vnstat -w +parameters: +type:script_output +message:request Vnstat weekly status + +[monthly] +command:/usr/local/bin/vnstat -m +parameters: +type:script_output +message:request Vnstat weekly status diff --git a/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/+TARGETS b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/+TARGETS new file mode 100644 index 000000000..7dd8d361a --- /dev/null +++ b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/+TARGETS @@ -0,0 +1,2 @@ +vnstat:/etc/rc.conf.d/vnstat +vnstat.conf:/usr/local/etc/vnstat.conf diff --git a/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat new file mode 100644 index 000000000..d81a43261 --- /dev/null +++ b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat @@ -0,0 +1,6 @@ +{% if helpers.exists('OPNsense.vnstat.general.enabled') and OPNsense.vnstat.general.enabled == '1' %} +vnstat_var_script="/usr/local/opnsense/scripts/OPNsense/Vnstat/setup.sh" +vnstat_enable="YES" +{% else %} +vnstat_enable="NO" +{% endif %} diff --git a/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf new file mode 100644 index 000000000..1e7d33467 --- /dev/null +++ b/net/vnstat/src/opnsense/service/templates/OPNsense/Vnstat/vnstat.conf @@ -0,0 +1,153 @@ +{% if helpers.exists('OPNsense.vnstat.general.enabled') and OPNsense.vnstat.general.enabled == '1' %} +{% from 'OPNsense/Macros/interface.macro' import physical_interface %} + +{% if helpers.exists('OPNsense.vnstat.general.interface') and OPNsense.vnstat.general.interface != '' %} +# default interface +Interface "{{ physical_interface(OPNsense.vnstat.general.interface) }}" +{% endif %} + +# location of the database directory +DatabaseDir "/var/lib/vnstat" + +# locale (LC_ALL) ("-" = use system locale) +Locale "-" + +# on which day should months change +MonthRotate 1 + +# date output formats for -d, -m, -t and -w +# see 'man date' for control codes +DayFormat "%x" +MonthFormat "%b '%y" +TopFormat "%x" + +# characters used for visuals +RXCharacter "%" +TXCharacter ":" +RXHourCharacter "r" +TXHourCharacter "t" + +# how units are prefixed when traffic is shown +# 0 = IEC standard prefixes (KiB/MiB/GiB/TiB) +# 1 = old style binary prefixes (KB/MB/GB/TB) +UnitMode 0 + +# output style +# 0 = minimal & narrow, 1 = bar column visible +# 2 = same as 1 except rate in summary and weekly +# 3 = rate column visible +OutputStyle 3 + +# used rate unit (0 = bytes, 1 = bits) +RateUnit 1 + +# try to detect interface maximum bandwidth, 0 = disable feature +# MaxBandwidth will be used as fallback value when enabled +BandwidthDetection 1 + +# maximum bandwidth (Mbit) for all interfaces, 0 = disable feature +# (unless interface specific limit is given) +MaxBandwidth 1000 + +# interface specific limits +# example 8Mbit limit for 'ethnone': +MaxBWethnone 8 + +# how many seconds should sampling for -tr take by default +Sampletime 5 + +# default query mode +# 0 = normal, 1 = days, 2 = months, 3 = top10 +# 4 = exportdb, 5 = short, 6 = weeks, 7 = hours +QueryMode 0 + +# filesystem disk space check (1 = enabled, 0 = disabled) +CheckDiskSpace 1 + +# database file locking (1 = enabled, 0 = disabled) +UseFileLocking 1 + +# how much the boot time can variate between updates (seconds) +BootVariation 15 + +# log days without traffic to daily list (1 = enabled, 0 = disabled) +TrafficlessDays 1 + + +# vnstatd +## + +# switch to given user when started as root (leave empty to disable) +DaemonUser "" + +# switch to given user when started as root (leave empty to disable) +DaemonGroup "" + +# how often (in seconds) interface data is updated +UpdateInterval 30 + +# how often (in seconds) interface status changes are checked +PollInterval 5 + +# how often (in minutes) data is saved to file +SaveInterval 5 + +# how often (in minutes) data is saved when all interface are offline +OfflineSaveInterval 30 + +# how often (in minutes) bandwidth detection is redone when +# BandwidthDetection is enabled (0 = disabled) +BandwidthDetectionInterval 5 + +# force data save when interface status changes (1 = enabled, 0 = disabled) +SaveOnStatusChange 1 + +# enable / disable logging (0 = disabled, 1 = logfile, 2 = syslog) +UseLogging 2 + +# create dirs if needed (1 = enabled, 0 = disabled) +CreateDirs 1 + +# update ownership of files if needed (1 = enabled, 0 = disabled) +UpdateFileOwner 1 + +# file used for logging if UseLogging is set to 1 +LogFile "/var/log/vnstat/vnstat.log" + +# file used as daemon pid / lock file +PidFile "/var/run/vnstat/vnstat.pid" + + +# vnstati +## + +# title timestamp format +HeaderFormat "%x %H:%M" + +# show hours with rate (1 = enabled, 0 = disabled) +HourlyRate 1 + +# show rate in summary (1 = enabled, 0 = disabled) +SummaryRate 1 + +# layout of summary (1 = with monthly, 0 = without monthly) +SummaryLayout 1 + +# transparent background (1 = enabled, 0 = disabled) +TransparentBg 0 + +# image colors +CBackground "FFFFFF" +CEdge "AEAEAE" +CHeader "606060" +CHeaderTitle "FFFFFF" +CHeaderDate "FFFFFF" +CText "000000" +CLine "B0B0B0" +CLineL "-" +CRx "92CF00" +CTx "606060" +CRxD "-" +CTxD "-" + +{% endif %}