[packages] vnstat: readd download and symlink functionality
[openwrt/svn-archive/archive.git] / net / vnstat / files / vnstat.init
index 5632c6a777a5f8f6acbeb463bd193e0f06aa28be..fab1f341abe5dabb759de7516fc27cb1971fe6ba 100644 (file)
@@ -1,67 +1,77 @@
 #!/bin/sh /etc/rc.common
-# Copyright (C) 2008 OpenWrt.org
+# Copyright (C) 2008-2011 OpenWrt.org
 
 START=99
-LIB_D=/var/lib/vnstat
-WWW_D=/www/vnstat
-RUN_D=/var/run
-PID_F=$RUN_D/vnstat.pid
-VNSTATD_BIN=/usr/sbin/vnstatd
 
-system_config() {
-       local cfg="$1"
-
-       config_get hostname "$cfg" hostname
-       hostname="${hostname:-OpenWrt}"
+vnstat_option() {
+       sed -ne "s/^[[:space:]]*$1[[:space:]]*['\"]\([^'\"]*\)['\"].*/\1/p" \
+               /etc/vnstat.conf
 }
 
-do_download() {
-       include /lib/network
-       scan_interfaces
-
-       local cfg="$1"
-
-       config_get ifname "$cfg" ifname
-       [ -n "$ifname" ] || return 0
-       [ -e $LIB_D/$ifname ] && return 0
-
-       config_get_bool enabled "$cfg" enabled '1'
-       [ "$enabled" -gt 0 ] && {
-               config_get remote_host "$cfg" remote_host
-               [ -n "$remote_host" ] || return 0
-               config_get remote_path "$cfg" remote_path
-               [ -n "$remote_path" ] || return 0
-
-               while [ ! -e $LIB_D/$ifname ]; do
-                       wget http://$remote_host/$remote_path/${hostname}_$ifname -O $LIB_D/$ifname 2>/dev/null
-                       [ -e $LIB_D/$ifname ] && {
-                               logger -t "vnstat" "Successfully downloaded ${ifname}'s vnStat database backup"
-                               [ -L $WWW_D/$ifname ] || ln -s $LIB_D/$ifname $WWW_D/$ifname
-                               return 0
-                       }
-                       sleep 30
-               done
+start() {
+       local lib="$(vnstat_option DatabaseDir)"
+       local pid="$(vnstat_option PidFile)"
+
+       [ -n "$lib" ] || {
+               echo "Error: No DatabaseDir set in vnstat.conf" >&2
+               exit 1
        }
-}
 
-start() {
-       [ -d $LIB_D ] || mkdir -p $LIB_D
-       [ -d $WWW_D ] || mkdir -p $WWW_D
+       [ -n "$pid" ] || {
+               echo "Error: No PidFile set in vnstat.conf" >&2
+               exit 1
+       }
+
+       mkdir -p "$lib"
+
+       init_ifaces() {
+               local cfg="$1"
+               local url lnk
+
+               init_iface() {
+                       local ifn="$1"
+
+                       [ -n "$url" ] && {
+                               local try=0
+                               local max=3
+                               local hostname="$(cat /proc/sys/kernel/hostname)"
 
-       config_load system
-       config_foreach system_config system
+                               while [ $((++try)) -le $max ]; do
+                                       if wget -q -O "$lib/$ifn" "$url/${hostname}_$ifn" 2>/dev/null && [ -e "$lib/$ifn" ]; then
+                                               logger -t "vnstat" "Downloaded backup for database $ifn"
+                                               break
+                                       else
+                                               logger -t "vnstat" "Download try $try/$max for database $ifn failed"
+                                               sleep 30
+                                       fi
+                               done
+                       }
+
+                       /usr/bin/vnstat -u -i "$ifn" >/dev/null
+
+                       [ -n "$lnk" ] && {
+                               mkdir -p "$lnk"
+                               [ -L "$lnk/$ifn" ] || ln -s "$lib/$ifn" "$lnk/$ifn"
+                       }
+               }
+
+               config_get url "$cfg" remote
+               config_get lnk "$cfg" symlink
+               config_list_foreach "$cfg" interface init_iface
+
+               return 1
+       }
 
        config_load vnstat
-       config_foreach do_download interface
+       config_foreach init_ifaces vnstat
 
-       [ ! -f $PID_F ] && $VNSTATD_BIN -d
+       /usr/sbin/vnstatd -d
 }
 
 stop() {
-       [ -f $PID_F ] && kill $(cat $PID_F)
-}
-
-reload() {
-       [ -f $PID_F ] && kill -HUP $(cat $PID_F)
+       local pid="$(vnstat_option PidFile)"
+       [ -n "$pid" ] && {
+               service_kill vnstatd "$pid"
+               rm -f "$pid"
+       }
 }
-