5632c6a777a5f8f6acbeb463bd193e0f06aa28be
[openwrt/svn-archive/archive.git] / net / vnstat / files / vnstat.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008 OpenWrt.org
3
4 START=99
5 LIB_D=/var/lib/vnstat
6 WWW_D=/www/vnstat
7 RUN_D=/var/run
8 PID_F=$RUN_D/vnstat.pid
9 VNSTATD_BIN=/usr/sbin/vnstatd
10
11 system_config() {
12 local cfg="$1"
13
14 config_get hostname "$cfg" hostname
15 hostname="${hostname:-OpenWrt}"
16 }
17
18 do_download() {
19 include /lib/network
20 scan_interfaces
21
22 local cfg="$1"
23
24 config_get ifname "$cfg" ifname
25 [ -n "$ifname" ] || return 0
26 [ -e $LIB_D/$ifname ] && return 0
27
28 config_get_bool enabled "$cfg" enabled '1'
29 [ "$enabled" -gt 0 ] && {
30 config_get remote_host "$cfg" remote_host
31 [ -n "$remote_host" ] || return 0
32 config_get remote_path "$cfg" remote_path
33 [ -n "$remote_path" ] || return 0
34
35 while [ ! -e $LIB_D/$ifname ]; do
36 wget http://$remote_host/$remote_path/${hostname}_$ifname -O $LIB_D/$ifname 2>/dev/null
37 [ -e $LIB_D/$ifname ] && {
38 logger -t "vnstat" "Successfully downloaded ${ifname}'s vnStat database backup"
39 [ -L $WWW_D/$ifname ] || ln -s $LIB_D/$ifname $WWW_D/$ifname
40 return 0
41 }
42 sleep 30
43 done
44 }
45 }
46
47 start() {
48 [ -d $LIB_D ] || mkdir -p $LIB_D
49 [ -d $WWW_D ] || mkdir -p $WWW_D
50
51 config_load system
52 config_foreach system_config system
53
54 config_load vnstat
55 config_foreach do_download interface
56
57 [ ! -f $PID_F ] && $VNSTATD_BIN -d
58 }
59
60 stop() {
61 [ -f $PID_F ] && kill $(cat $PID_F)
62 }
63
64 reload() {
65 [ -f $PID_F ] && kill -HUP $(cat $PID_F)
66 }
67