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.

108 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 "allways"
  22. config_get pinghosts $1 pinghosts "8.8.8.8"
  23. config_get pingperiod $1 pingperiod
  24. config_get forcedelay $1 forcedelay "0"
  25. error=""
  26. timetoseconds "$period"
  27. period="$seconds"
  28. [ "$period" -ge 1 ] \
  29. || append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
  30. [ "$mode" = "allways" -o "$mode" = "ping" ] \
  31. || append_string "error" "mode must be 'allways' or 'ping'" "; "
  32. [ -n "$pinghosts" -o "$mode" = "allways" ] \
  33. || append_string "error" "pinghosts must be set when in 'ping' mode" "; "
  34. [ "$mode" = "ping" ] && {
  35. if [ -n "$pingperiod" ]
  36. then
  37. timetoseconds "$pingperiod"
  38. pingperiod="$seconds"
  39. if [ "$pingperiod" -ge 0 ]
  40. then
  41. [ "$pingperiod" -le "$period" ] \
  42. || append_string "error" "pingperiod must be less than period" "; "
  43. else
  44. append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
  45. fi
  46. else
  47. pingperiod="$((period/20))"
  48. fi
  49. }
  50. [ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \
  51. || append_string "error" "pingperiod is not recognized" "; "
  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" = "allways" ]
  56. then
  57. /usr/bin/watchcat.sh "allways" "$period" "$forcedelay" &
  58. logger -p user.info -t "wathchat" "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 "wathchat" "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 "$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. }