packages/net/vnstat2/files/vnstat.init
Jan Hoffmann f8820d2aeb
vnstat2: fix all interfaces being monitored when none are configured
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>
(cherry picked from commit ecae7dedde)
2022-02-18 23:25:49 +01:00

71 lines
1.4 KiB
Bash

#!/bin/sh /etc/rc.common
START=60
STOP=50
USE_PROCD=1
vnstat_option() {
sed -ne "s/^[[:space:]]*$1[[:space:]]*['\"]\([^'\"]*\)['\"].*/\1/p" \
/etc/vnstat.conf
}
init_database() {
local lib database_count
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
}
init_ifaces() {
local cfg="$1"
init_iface() {
local ifn="$1"
/usr/bin/vnstat --add -i "$ifn" >/dev/null
}
config_list_foreach "$cfg" interface init_iface
}
start_service() {
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
procd_open_instance
procd_set_param stdout 1
procd_set_param stderr 1
procd_set_param command /usr/sbin/vnstatd --nodaemon $options
procd_set_param file /etc/vnstat.conf
procd_set_param respawn
procd_close_instance
}
reload_service() {
config_load vnstat
config_foreach init_ifaces vnstat
procd_send_signal vnstat
}
service_triggers() {
procd_add_reload_trigger vnstat
}