#!/bin/sh DAEMON=vnstat DAEMON_BIN=vnstatd . /etc/init.d/modlibrc HTTPD_PID_VNS=/var/run/httpd_vns.pid config() { local SUBDAEMON=vns modlib_config httpd-$SUBDAEMON /mod/etc/httpd-${SUBDAEMON}.conf mkdir $VNSTAT_DBDIR 2>/dev/null mkdir -p /var/lib rm -rf /var/lib/vnstat 2>/dev/null ln -s $VNSTAT_DBDIR /var/lib/vnstat if [ -z "$VNSTAT_INTERFACES" ]; then VNSTAT_INTERFACES=$(ifconfig | grep "^\w" | sed 's/ .*//g') else #delete unused databases for dbfile in $(ls -A /var/lib/vnstat/ 2>/dev/null); do useif=no for ifname in $VNSTAT_INTERFACES; do [ "$ifname" = "$dbfile" -o ".$ifname" = "$dbfile" ] && useif=yes done if [ "$useif" = no ]; then rm -rf /var/lib/vnstat/$dbfile rm -rf /var/lib/vnstat/.$dbfile fi done fi #create new databeses for ifname in $VNSTAT_INTERFACES; do vnstat -u -i $ifname --config /tmp/flash/vnstat/vnstat.conf >/dev/null if [ $? -ne 0 ]; then echo 'failed.' vnstat -u -i $ifname --config /tmp/flash/vnstat/vnstat.conf echo 'Is your USB-device yet available?' exit $? fi done } stop_status() { kill -9 "$(cat $HTTPD_PID_VNS 2>/dev/null)" 2>/dev/null [ -x /etc/init.d/rc.inetd ] && /etc/init.d/rc.inetd config vnstat >/dev/null modunreg status vnstat stats } start() { stop_status if [ "$VNSTAT_WEBENABLED" = "yes" -a "$VNSTAT_WEB_INETD" != "yes" ]; then if [ "$VNSTAT_WEB_AUTH" = yes ]; then echo "/:$VNSTAT_WEB_USER:$(httpd -m $VNSTAT_WEB_PASS)" >/mod/etc/httpd-vns.conf VNS_CONF="-c /mod/etc/httpd-vns.conf" fi webcfg-vns -P $HTTPD_PID_VNS fi modlib_startdaemon $DAEMON_BIN -s -d --config /tmp/flash/vnstat/vnstat.conf [ -x /etc/init.d/rc.inetd ] && //etc/init.d/rc.inetd config vnstat modreg status vnstat vnstat stats } stop() { stop_status killall $DAEMON_BIN 2>/dev/null } case $1 in config) config ;; ""|load) modlib_add_httpd_symlink vns [ ! -d "/tmp/flash/vnstat" ] && mkdir -p /tmp/flash/vnstat [ ! -e "/tmp/flash/vnstat/vnstat.conf" ] && /etc/default.vnstat/vnstat_conf > /tmp/flash/vnstat/vnstat.conf modreg file vnstat conf 'vnstat.conf' 1 "vnstat_conf" modreg cgi vnstat vnstat modreg daemon $DAEMON modlib_start $VNSTAT_ENABLED ;; unload) modunreg daemon $DAEMON modunreg cgi vnstat modunreg file vnstat modlib_stop ;; start) modlib_start ;; stop) modlib_stop ;; restart) modlib_restart ;; status) modlib_status ;; status_webcfg-vns) [ "$VNSTAT_WEBENABLED" = "yes" -a "$VNSTAT_WEB_INETD" = "yes" -a "$(modlib_status)" == "running" ] && echo 'inetd' || modlib_status ;; *) echo "Usage: $0 [load|unload|start|stop|restart|status|config]" 1>&2 exit 1 ;; esac exit 0