You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
1.5 KiB

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