AA: vnstat: merge r36716 (#13577)
[12.09/packages.git] / net / vnstat / files / vnstat.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2008-2011 OpenWrt.org
3
4 START=99
5
6 vnstat_option() {
7         sed -ne "s/^[[:space:]]*$1[[:space:]]*['\"]\([^'\"]*\)['\"].*/\1/p" \
8                 /etc/vnstat.conf
9 }
10
11 start() {
12         local lib="$(vnstat_option DatabaseDir)"
13         local pid="$(vnstat_option PidFile)"
14
15         [ -n "$lib" ] || {
16                 echo "Error: No DatabaseDir set in vnstat.conf" >&2
17                 exit 1
18         }
19
20         [ -n "$pid" ] || {
21                 echo "Error: No PidFile set in vnstat.conf" >&2
22                 exit 1
23         }
24
25         mkdir -p "$lib"
26
27         init_ifaces() {
28                 local cfg="$1"
29                 local url lnk
30
31                 init_iface() {
32                         local ifn="$1"
33
34                         [ -n "$url" ] && {
35                                 local try=0
36                                 local max=3
37                                 local hostname="$(cat /proc/sys/kernel/hostname)"
38
39                                 while [ $((++try)) -le $max ]; do
40                                         if wget -q -O "$lib/$ifn" "$url/${hostname}_$ifn" 2>/dev/null && [ -e "$lib/$ifn" ]; then
41                                                 logger -t "vnstat" "Downloaded backup for database $ifn"
42                                                 break
43                                         else
44                                                 logger -t "vnstat" "Download try $try/$max for database $ifn failed"
45                                                 sleep 30
46                                         fi
47                                 done
48                         }
49
50                         /usr/bin/vnstat -u -i "$ifn" >/dev/null
51
52                         [ -n "$lnk" ] && {
53                                 mkdir -p "$lnk"
54                                 [ -L "$lnk/$ifn" ] || ln -s "$lib/$ifn" "$lnk/$ifn"
55                         }
56                 }
57
58                 config_get url "$cfg" remote
59                 config_get lnk "$cfg" symlink
60                 config_list_foreach "$cfg" interface init_iface
61
62                 return 1
63         }
64
65         config_load vnstat
66         config_foreach init_ifaces vnstat
67
68         /usr/sbin/vnstatd -d
69 }
70
71 stop() {
72         local pid="$(vnstat_option PidFile)"
73         [ -n "$pid" ] && {
74                 service_kill vnstatd "$pid"
75                 rm -f "$pid"
76         }
77 }