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.

86 lines
1.8 KiB

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