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.

109 lines
3.1 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=97
  3. PIDFILE="/tmp/run/watchcat"
  4. append_string() {
  5. local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
  6. eval "actual=\$$varname"
  7. new="${actual:+$actual$separator}$add"
  8. eval "$varname=\$new"
  9. }
  10. timetoseconds() {
  11. local time=$1
  12. unset seconds
  13. { [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } || \
  14. { [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } || \
  15. { [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m}*60)); } || \
  16. { [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h}*3600)); } || \
  17. { [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d}*86400)); }
  18. }
  19. load_watchcat() {
  20. config_get period $1 period
  21. config_get mode $1 mode "always"
  22. config_get pinghosts $1 pinghosts "8.8.8.8"
  23. config_get pingperiod $1 pingperiod
  24. config_get forcedelay $1 forcedelay "0"
  25. # Fix potential typo in mode (backward compatibility).
  26. [ "$mode" = "allways" ] && mode="always"
  27. error=""
  28. timetoseconds "$period"
  29. period="$seconds"
  30. [ "$period" -ge 1 ] \
  31. || append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
  32. [ "$mode" = "always" -o "$mode" = "ping" ] \
  33. || append_string "error" "mode must be 'always' or 'ping'" "; "
  34. [ -n "$pinghosts" -o "$mode" = "always" ] \
  35. || append_string "error" "pinghosts must be set when in 'ping' mode" "; "
  36. [ "$mode" = "ping" ] && {
  37. if [ -n "$pingperiod" ]
  38. then
  39. timetoseconds "$pingperiod"
  40. pingperiod="$seconds"
  41. if [ "$pingperiod" -ge 0 ]
  42. then
  43. [ "$pingperiod" -lt "$period" ] \
  44. || append_string "error" "pingperiod must be less than period" "; "
  45. else
  46. append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
  47. fi
  48. else
  49. pingperiod="$((period/20))"
  50. fi
  51. }
  52. [ "$forcedelay" -ge 0 ] \
  53. || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
  54. [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
  55. if [ "$mode" = "always" ]
  56. then
  57. /usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
  58. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
  59. else
  60. /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
  61. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
  62. fi
  63. echo $! >> "${PIDFILE}.pids"
  64. }
  65. stop() {
  66. if [ -f "${PIDFILE}.pids" ]
  67. then
  68. logger -p user.info -t "watchcat" "stopping all tasks"
  69. while read pid
  70. do
  71. kill -KILL "$pid"
  72. done < "${PIDFILE}.pids"
  73. rm "${PIDFILE}.pids"
  74. logger -p user.info -t "watchcat" "all tasks stopped"
  75. else
  76. logger -p user.info -t "watchcat" "no tasks running"
  77. fi
  78. }
  79. start() {
  80. [ -f "${PIDFILE}.pids" ] && stop
  81. config_load system
  82. if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
  83. then
  84. logger -p user.info -t "watchcat" "starting all tasks"
  85. config_foreach load_watchcat watchcat
  86. logger -p user.info -t "watchcat" "all tasks started"
  87. else
  88. logger -p user.info -t "watchcat" "no tasks defined"
  89. fi
  90. }