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.

111 lines
3.2 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" -le "$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. [ "$pingperiod" -lt "$period" -o "$mode" = "always" ] \
  53. || append_string "error" "pingperiod is not recognized" "; "
  54. [ "$forcedelay" -ge 0 ] \
  55. || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
  56. [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
  57. if [ "$mode" = "always" ]
  58. then
  59. /usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
  60. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
  61. else
  62. /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
  63. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
  64. fi
  65. echo $! >> "${PIDFILE}.pids"
  66. }
  67. stop() {
  68. if [ -f "${PIDFILE}.pids" ]
  69. then
  70. logger -p user.info -t "watchcat" "stopping all tasks"
  71. while read pid
  72. do
  73. kill "$pid"
  74. done < "${PIDFILE}.pids"
  75. rm "${PIDFILE}.pids"
  76. logger -p user.info -t "watchcat" "all tasks stopped"
  77. else
  78. logger -p user.info -t "watchcat" "no tasks running"
  79. fi
  80. }
  81. start() {
  82. [ -f "${PIDFILE}.pids" ] && stop
  83. config_load system
  84. if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
  85. then
  86. logger -p user.info -t "watchcat" "starting all tasks"
  87. config_foreach load_watchcat watchcat
  88. logger -p user.info -t "watchcat" "all tasks started"
  89. else
  90. logger -p user.info -t "watchcat" "no tasks defined"
  91. fi
  92. }