Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

watchcat: Add support for custom ping timeout #25174

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion utils/watchcat/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=watchcat
PKG_VERSION:=1
PKG_RELEASE:=17
PKG_RELEASE:=18

PKG_MAINTAINER:=Roger D <[email protected]>
PKG_LICENSE:=GPL-2.0
Expand Down
2 changes: 2 additions & 0 deletions utils/watchcat/files/migrate-watchcat
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ upgrade_watchcat() {
config_get mode "$cfg" mode
config_get pinghosts "$cfg" pinghosts
config_get forcedelay "$cfg" forcedelay
config_get pingtimeout "$cfg" pingtimeout

[ -f "/etc/config/watchcat" ] || touch /etc/config/watchcat
uci_add watchcat watchcat
uci_set watchcat @watchcat[-1] period "$period"
uci_set watchcat @watchcat[-1] mode "$mode"
uci_set watchcat @watchcat[-1] pinghosts "$pinghosts"
uci_set watchcat @watchcat[-1] forcedelay "$forcedelay"
uci_set watchcat @watchcat[-1] pingtimeout "$pingtimeout"

uci_remove system "$cfg"
}
Expand Down
1 change: 1 addition & 0 deletions utils/watchcat/files/watchcat.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ config watchcat
option mode 'ping_reboot'
option pinghosts '8.8.8.8'
option forcedelay '30'
option pingtimeout '10'
11 changes: 8 additions & 3 deletions utils/watchcat/files/watchcat.init
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ config_watchcat() {
config_get_bool unlockbands "$1" unlockbands "0"
config_get addressfamily "$1" addressfamily "any"
config_get script "$1" script
config_get pingtimeout "$1" pingtimeout "10"

# Fix potential typo in mode and provide backward compatibility.
[ "$mode" = "allways" ] && mode="periodic_reboot"
Expand All @@ -58,6 +59,10 @@ config_watchcat() {
[ "$period" -ge 1 ] ||
append_string "error" "period has invalid format. Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "

pingtimeout="$(time_to_seconds "$pingtimeout")"
[ "$pingtimeout" -ge 1 ] ||
append_string "error" "pingtimeout has invalid format. Use time value(ex: '1'; '10s')" "; "

# ping_reboot mode and restart_iface mode specific checks
if [ "$mode" = "ping_reboot" ] || [ "$mode" = "restart_iface" ] || [ "$mode" = "run_script" ]; then
if [ -z "$error" ]; then
Expand Down Expand Up @@ -104,19 +109,19 @@ config_watchcat() {
;;
ping_reboot)
procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" "$addressfamily"
procd_set_param command /usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$pinghosts" "$pingperiod" "$pingsize" "$addressfamily" "$pingtimeout"
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
;;
restart_iface)
procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily"
procd_set_param command /usr/bin/watchcat.sh "restart_iface" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$mmifacename" "$unlockbands" "$addressfamily" "$pingtimeout"
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
;;
run_script)
procd_open_instance "watchcat_${1}"
procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script"
procd_set_param command /usr/bin/watchcat.sh "run_script" "$period" "$pinghosts" "$pingperiod" "$pingsize" "$interface" "$addressfamily" "$script" "$pingtimeout"
procd_set_param respawn "${respawn_threshold:-3600}" "${respawn_timeout:-5}" "${respawn_retry:-5}"
procd_close_instance
;;
Expand Down
30 changes: 20 additions & 10 deletions utils/watchcat/files/watchcat.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ get_ping_family_flag() {
echo $family
}

get_ping_timeout() {
echo ${1:-10}
}

reboot_now() {
reboot &

Expand Down Expand Up @@ -107,6 +111,7 @@ watchcat_monitor_network() {
mm_iface_unlock_bands="$7"
address_family="$8"
script="$9"
timeout="${10}"

time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
Expand All @@ -122,6 +127,8 @@ watchcat_monitor_network() {

ping_family="$(get_ping_family_flag "$address_family")"

ping_timeout="$(get_ping_timeout "$timeout")"

while true; do
# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
time_now="$(cat /proc/uptime)"
Expand All @@ -137,12 +144,12 @@ watchcat_monitor_network() {
for host in $ping_hosts; do
if [ "$iface" != "" ]; then
ping_result="$(
ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
ping $ping_family ${iface:+-I $iface} ${ping_size:+-s $ping_size} -c 1 ${ping_timeout:+-W $ping_timeout} "$host" &> /dev/null
echo $?
)"
else
ping_result="$(
ping $ping_family -s "$ping_size" -c 1 "$host" &> /dev/null
ping $ping_family ${ping_size:+-s $ping_size} -c 1 ${ping_timeout:+-W $ping_timeout} "$host" &> /dev/null
echo $?
)"
fi
Expand Down Expand Up @@ -188,6 +195,7 @@ watchcat_ping() {
ping_frequency_interval="$4"
ping_size="$5"
address_family="$6"
timeout="$7"

time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}"
Expand All @@ -203,6 +211,8 @@ watchcat_ping() {

ping_family="$(get_ping_family_flag "$address_family")"

ping_timeout="$(get_ping_timeout "$timeout")"

while true; do
# account for the time ping took to return. With a ping time of 5s, ping might take more than that, so it is important to avoid even more delay.
time_now="$(cat /proc/uptime)"
Expand All @@ -218,12 +228,12 @@ watchcat_ping() {
for host in $ping_hosts; do
if [ "$iface" != "" ]; then
ping_result="$(
ping $ping_family -I "$iface" -s "$ping_size" -c 1 "$host" &> /dev/null
ping $ping_family ${iface:+-I $iface} ${ping_size:+-s $ping_size} -c 1 ${ping_timeout:+-W $ping_timeout} "$host" &> /dev/null
echo $?
)"
else
ping_result="$(
ping $ping_family -s "$ping_size" -c 1 "$host" &> /dev/null
ping $ping_family ${ping_size:+-s $ping_size} -c 1 ${ping_timeout:+-W $ping_timeout} "$host" &> /dev/null
echo $?
)"
fi
Expand Down Expand Up @@ -252,16 +262,16 @@ periodic_reboot)
watchcat_periodic "$2" "$3"
;;
ping_reboot)
# args from init script: period forcedelay pinghosts pingperiod pingsize addressfamily
watchcat_ping "$2" "$3" "$4" "$5" "$6" "$7"
# args from init script: period forcedelay pinghosts pingperiod pingsize addressfamily pingtimeout
watchcat_ping "$2" "$3" "$4" "$5" "$6" "$7" "$8"
;;
restart_iface)
# args from init script: period pinghosts pingperiod pingsize interface mmifacename unlockbands addressfamily
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" ""
# args from init script: period pinghosts pingperiod pingsize interface mmifacename unlockbands addressfamily pingtimeout
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "" "${10}"
;;
run_script)
# args from init script: period pinghosts pingperiod pingsize interface addressfamily script
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "" "" "$7" "$8"
# args from init script: period pinghosts pingperiod pingsize interface addressfamily script pingtimeout
watchcat_monitor_network "$2" "$3" "$4" "$5" "$6" "" "" "$7" "$8" "$9"
;;
*)
echo "Error: invalid mode selected: $mode"
Expand Down