vnstat2: fix all interfaces being monitored when none are configured
authorJan Hoffmann <jan@3e8.eu>
Thu, 21 Oct 2021 21:38:58 +0000 (23:38 +0200)
committerRosen Penev <rosenp@gmail.com>
Thu, 28 Oct 2021 08:31:18 +0000 (01:31 -0700)
By default, vnstatd adds all available interfaces on startup when its
database is empty. The --noadd option prevents this, but it breaks
import of legacy databases, and causes vnstatd to exit immediately
after startup, which breaks reloading.

This changes the init script to add the --noadd option when no legacy
databases need to be imported, and patches vnstatd to keep running
even when no interfaces are configured.

Signed-off-by: Jan Hoffmann <jan@3e8.eu>
net/vnstat2/Makefile
net/vnstat2/files/vnstat.init
net/vnstat2/patches/100-noexit.patch [new file with mode: 0644]

index e31e9fa3622ef347813ef95b5611746c135d816c..eadd0c67159e233364701c6efce6407801890cb7 100644 (file)
@@ -7,7 +7,7 @@ include $(TOPDIR)/rules.mk
 
 PKG_NAME:=vnstat2
 PKG_VERSION:=2.8
-PKG_RELEASE:=1
+PKG_RELEASE:=$(AUTORELEASE)
 
 PKG_SOURCE:=vnstat-$(PKG_VERSION).tar.gz
 PKG_SOURCE_URL:=https://humdi.net/vnstat
index 9754e1507eb43e1eebf3c7e2fb2c32218ac787f4..d69d7385f5e78f1db63752e323c734d9db5ee501 100644 (file)
@@ -11,13 +11,19 @@ vnstat_option() {
 }
 
 init_database() {
-       local lib="$(vnstat_option DatabaseDir)"
+       local lib database_count
 
-       local database_count="$(ls "$lib" 2>/dev/null | wc -l)"
+       lib="$(vnstat_option DatabaseDir)"
+       [ -n "$lib" ] || lib="/var/lib/vnstat"
+
+       database_count="$(ls "$lib" 2>/dev/null | wc -l)"
 
        # only init database when folder is empty, as it would prevent import of legacy databases
        if [ "$database_count" -eq "0" ]; then
                /usr/sbin/vnstatd --initdb >/dev/null
+       else
+               # if vnstat.db doesn't exist, there are legacy databases to import
+               [ ! -f "$lib/vnstat.db" ] && echo -n "1"
        fi
 }
 
@@ -31,12 +37,15 @@ init_ifaces() {
        }
 
        config_list_foreach "$cfg" interface init_iface
-
-       return 1
 }
 
 start_service() {
-       init_database
+       local options needs_import
+
+       needs_import="$(init_database)"
+
+       # --noadd would prevent import of legacy databases
+       [ -z "$needs_import" ] && options="--noadd --noexit"
 
        config_load vnstat
        config_foreach init_ifaces vnstat
@@ -44,7 +53,7 @@ start_service() {
        procd_open_instance
        procd_set_param stdout 1
        procd_set_param stderr 1
-       procd_set_param command /usr/sbin/vnstatd --nodaemon
+       procd_set_param command /usr/sbin/vnstatd --nodaemon $options
        procd_set_param file /etc/vnstat.conf
        procd_set_param respawn
        procd_close_instance
diff --git a/net/vnstat2/patches/100-noexit.patch b/net/vnstat2/patches/100-noexit.patch
new file mode 100644 (file)
index 0000000..8ef9ac2
--- /dev/null
@@ -0,0 +1,60 @@
+--- a/src/daemon.c
++++ b/src/daemon.c
+@@ -252,6 +252,7 @@ void initdstate(DSTATE *s)
+       s->sync = 0;
+       s->forcesave = 0;
+       s->noadd = 0;
++      s->noexit = 0;
+       s->initdb = 0;
+       s->iflisthash = 0;
+       s->cfgfile[0] = '\0';
+@@ -282,6 +283,9 @@ void preparedatabase(DSTATE *s)
+       }
+       if (s->noadd) {
++              if (s->noexit) {
++                      return;
++              }
+               printf("No interfaces found in database, exiting.\n");
+               exit(EXIT_FAILURE);
+       }
+@@ -300,6 +304,9 @@ void preparedatabase(DSTATE *s)
+       }
+       if (!addinterfaces(s) && s->dbifcount == 0) {
++              if (s->noexit) {
++                      return;
++              }
+               printf("Nothing to do, exiting.\n");
+               exit(EXIT_FAILURE);
+       }
+--- a/src/daemon.h
++++ b/src/daemon.h
+@@ -4,7 +4,7 @@
+ typedef struct {
+       int updateinterval, saveinterval;
+       short running, dodbsave, rundaemon;
+-      short dbsaved, showhelp, sync, forcesave, noadd, initdb;
++      short dbsaved, showhelp, sync, forcesave, noadd, noexit, initdb;
+       short bootdetected, cleanuphour, dbretrycount;
+       uint32_t iflisthash;
+       uint64_t dbifcount;
+--- a/src/vnstatd.c
++++ b/src/vnstatd.c
+@@ -248,6 +248,7 @@ void showhelp(void)
+       printf("      --config <config file>   select used config file\n");
+       printf("      --noadd                  prevent startup if database has no interfaces\n");
+       printf("      --alwaysadd [mode]       automatically start monitoring all new interfaces\n");
++      printf("      --noexit                 keep running even when database has no interfaces\n");
+       printf("      --initdb                 create empty database and exit\n\n");
+       printf("See also \"man vnstatd\".\n");
+@@ -319,6 +320,8 @@ void parseargs(DSTATE *s, int argc, char
+                       } else {
+                               cfg.alwaysadd = 1;
+                       }
++              } else if (strcmp(argv[currentarg], "--noexit") == 0) {
++                      s->noexit = 1;
+               } else if (strcmp(argv[currentarg], "--initdb") == 0) {
+                       s->initdb = 1;
+                       s->showhelp = 0;