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.

142 lines
4.5 KiB

  1. #!/bin/sh /etc/rc.common
  2. START=97
  3. PIDFILE="/tmp/run/watchcat"
  4. append_string() {
  5. varname="$1"
  6. add="$2"
  7. separator="${3:- }"
  8. actual
  9. eval "actual=\$$varname"
  10. new="${actual:+$actual$separator}$add"
  11. eval "$varname=\$new"
  12. }
  13. time_to_seconds() {
  14. time=$1
  15. { [ "$time" -ge 1 ] 2>/dev/null && seconds="$time"; } ||
  16. { [ "${time%s}" -ge 1 ] 2>/dev/null && seconds="${time%s}"; } ||
  17. { [ "${time%m}" -ge 1 ] 2>/dev/null && seconds=$((${time%m} * 60)); } ||
  18. { [ "${time%h}" -ge 1 ] 2>/dev/null && seconds=$((${time%h} * 3600)); } ||
  19. { [ "${time%d}" -ge 1 ] 2>/dev/null && seconds=$((${time%d} * 86400)); }
  20. echo $seconds
  21. unset seconds
  22. unset time
  23. }
  24. load_watchcat() {
  25. config_get period "$1" period "120"
  26. config_get mode "$1" mode "restart_iface"
  27. config_get pinghosts "$1" pinghosts "8.8.8.8"
  28. config_get pingperiod "$1" pingperiod "60"
  29. config_get forcedelay "$1" forcedelay "60"
  30. config_get pingsize "$1" pingsize "standard"
  31. config_get interface "$1" interface
  32. config_get mmifacename "$1" mmifacename
  33. config_get unlockbands "$1" unlockbands "0"
  34. # Fix potential typo in mode and provide backward compatibility.
  35. [ "$mode" = "allways" ] && mode="periodic_reboot"
  36. [ "$mode" = "always" ] && mode="periodic_reboot"
  37. [ "$mode" = "ping" ] && mode="ping_reboot"
  38. error=""
  39. warn=""
  40. # Checks for settings common to all operation modes
  41. if [ "$mode" != "periodic_reboot" ] && [ "$mode" != "ping_reboot" ] && [ "$mode" != "restart_iface" ]; then
  42. append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot' or 'restart_iface'" "; "
  43. fi
  44. period="$(time_to_seconds "$period")"
  45. [ "$period" -ge 1 ] ||
  46. append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
  47. # ping_reboot mode and restart_iface mode specific checks
  48. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ]; then
  49. if [ -z "$error" ]; then
  50. pingperiod_default="$((period / 5))"
  51. pingperiod="$(time_to_seconds "$pingperiod")"
  52. if [ "$pingperiod" -ge 0 ] && [ "$pingperiod" -ge "$period" ]; then
  53. pingperiod="$(time_to_seconds "$pingperiod_default")"
  54. append_string "warn" "pingperiod cannot be greater than $period. Defaulted to $pingperiod_default seconds (1/5 of period)" "; "
  55. fi
  56. if [ "$pingperiod" -lt 0 ]; then
  57. append_string "warn" "pingperiod cannot be a negative value." "; "
  58. fi
  59. if [ "$mmifacename" != "" ] && [ "$period" -lt 30 ]; then
  60. append_string "error" "Check interval is less than 30s. For robust operation with ModemManager modem interfaces it is recommended to set the period to at least 30s."
  61. fi
  62. fi
  63. fi
  64. # ping_reboot mode and periodic_reboot mode specific checks
  65. if [ "$mode" = "ping_reboot" ] || [ "$mode" = "periodic_reboot" ]; then
  66. forcedelay="$(time_to_seconds "$forcedelay")"
  67. fi
  68. [ -n "$warn" ] && logger -p user.warn -t "watchcat" "$1: $warn"
  69. [ -n "$error" ] && {
  70. logger -p user.err -t "watchcat" "reboot program $1 not started - $error"
  71. return
  72. }
  73. case "$mode" in
  74. periodic_reboot)
  75. /usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay" &
  76. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
  77. ;;
  78. ping_reboot)
  79. /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" &
  80. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay;pingsize=$pingsize)"
  81. ;;
  82. restart_iface)
  83. /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" &
  84. logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;pingsize=$pingsize;interface=$interface;mmifacename=$mmifacename;unlockbands=$unlockbands)"
  85. ;;
  86. *)
  87. echo "Error starting Watchcat service. Invalid mode selection: $mode"
  88. ;;
  89. esac
  90. echo $! >>"${PIDFILE}.pids"
  91. }
  92. stop() {
  93. if [ -f "${PIDFILE}.pids" ]; then
  94. logger -p user.info -t "watchcat" "stopping all tasks"
  95. while read pid; do
  96. kill -KILL "$pid"
  97. done <"${PIDFILE}.pids"
  98. rm "${PIDFILE}.pids"
  99. logger -p user.info -t "watchcat" "all tasks stopped"
  100. else
  101. logger -p user.info -t "watchcat" "no tasks running"
  102. fi
  103. }
  104. start() {
  105. [ -f "${PIDFILE}.pids" ] && stop
  106. config_load system
  107. if [ -n "$(uci show system.@watchcat[0])" ]; then # at least one watchcat section exists
  108. logger -p user.info -t "watchcat" "starting all tasks"
  109. config_foreach load_watchcat watchcat
  110. logger -p user.info -t "watchcat" "all tasks started"
  111. else
  112. logger -p user.info -t "watchcat" "no tasks defined"
  113. fi
  114. }