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.

146 lines
4.5 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
  22. config_get pinghosts $1 pinghosts
  23. config_get pingperiod $1 pingperiod
  24. config_get nopingtime $1 nopingtime
  25. config_get forcedelay $1 forcedelay
  26. local nopingtime_dflt="900"
  27. local forcedelay_dflt="60"
  28. # Fix potential typo in mode (backward compatibility).
  29. [ "$mode" = "allways" ] && mode="always"
  30. error=""
  31. warn=""
  32. if [ -z "$period" ]
  33. then
  34. append_string "error" "period is not set! Use time value(ex: '30'; '4m'; '6h'; '2d')." "; "
  35. else
  36. timetoseconds "$period";period="$seconds"
  37. [ "$period" -ge 1 ] \
  38. || append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
  39. fi
  40. [ "$mode" = "always" -o "$mode" = "ping" ] \
  41. || append_string "error" "mode must be 'always' or 'ping'" "; "
  42. if [ -z "$forcedelay" ]
  43. then
  44. forcedelay="$forcedelay_dflt"
  45. append_string "warn" "forcedelay is not configured! Defaulted to $forcedelay seconds" "; "
  46. else
  47. [ "$forcedelay" -ge 0 ] || {
  48. forcedelay="$forcedelay_dflt"
  49. append_string "warn" "forcedelay is invalid! Defaulted to $forcedelay seconds" "; "
  50. }
  51. fi
  52. [ -z "$error" -a "$mode" = "ping" ] && {
  53. [ -z "$pinghosts" ] \
  54. && append_string "error" "pinghosts must be set in 'ping' mode! Use space separated address list (ex: '8.8.8.8 9.9.9.9')" "; "
  55. if [ -z "$nopingtime" ]
  56. then
  57. nopingtime="$nopingtime_dflt"
  58. append_string "warn" "nopingtime is not configured! Defaulted to $nopingtime seconds" "; "
  59. else
  60. timetoseconds "$nopingtime";nopingtime="$seconds"
  61. [ "$nopingtime" -ge 0 ] || {
  62. nopingtime="$nopingtime_dflt"
  63. append_string "warn" "nopingtime invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $nopingtime seconds" "; "
  64. }
  65. fi
  66. local pingperiod_dflt="$((period/5))"
  67. if [ -z "$pingperiod" ]
  68. then
  69. pingperiod="$pingperiod_dflt"
  70. append_string "warn" "pingperiod is not configured! Defaulted to $pingperiod seconds(1/5 of period)" "; "
  71. else
  72. timetoseconds "$pingperiod";pingperiod="$seconds"
  73. [ "$pingperiod" -ge 0 -a "$pingperiod" -ge "$period" ] && {
  74. pingperiod="$pingperiod_dflt"
  75. append_string "warn" "pingperiod is invalid value(greater than period)! Defaulted to $pingperiod seconds(1/5 of period)" "; "
  76. }
  77. [ "$pingperiod" -ge 0 ] || {
  78. pingperiod="$pingperiod_dflt"
  79. append_string "warn" "pingperiod has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $pingperiod seconds(1/5 of period)" "; "
  80. }
  81. fi
  82. }
  83. [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
  84. [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
  85. if [ "$mode" = "always" ]
  86. then
  87. /usr/bin/watchcat.sh "always" "$period" "$forcedelay" &
  88. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
  89. else
  90. /usr/bin/watchcat.sh "ping" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$nopingtime" &
  91. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay;nopingtime=$nopingtime)"
  92. fi
  93. echo $! >> "${PIDFILE}.pids"
  94. }
  95. stop() {
  96. if [ -f "${PIDFILE}.pids" ]
  97. then
  98. logger -p user.info -t "watchcat" "stopping all tasks"
  99. while read pid
  100. do
  101. kill -KILL "$pid"
  102. done < "${PIDFILE}.pids"
  103. rm "${PIDFILE}.pids"
  104. logger -p user.info -t "watchcat" "all tasks stopped"
  105. else
  106. logger -p user.info -t "watchcat" "no tasks running"
  107. fi
  108. }
  109. start() {
  110. [ -f "${PIDFILE}.pids" ] && stop
  111. config_load system
  112. if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
  113. then
  114. logger -p user.info -t "watchcat" "starting all tasks"
  115. config_foreach load_watchcat watchcat
  116. logger -p user.info -t "watchcat" "all tasks started"
  117. else
  118. logger -p user.info -t "watchcat" "no tasks defined"
  119. fi
  120. }