watchcat: add support for specifying ping packet size
Signed-off-by: Nicholas Smith <nicholas@nbembedded.com>
This commit is contained in:
parent
ddd2729313
commit
a4ce06de1c
3 changed files with 81 additions and 49 deletions
utils/watchcat
|
@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
|
||||||
|
|
||||||
PKG_NAME:=watchcat
|
PKG_NAME:=watchcat
|
||||||
PKG_VERSION:=1
|
PKG_VERSION:=1
|
||||||
PKG_RELEASE:=9
|
PKG_RELEASE:=10
|
||||||
|
|
||||||
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
|
PKG_MAINTAINER:=Roger D <rogerdammit@gmail.com>
|
||||||
PKG_LICENSE:=GPL-2.0
|
PKG_LICENSE:=GPL-2.0
|
||||||
|
|
|
@ -15,7 +15,7 @@ append_string() {
|
||||||
eval "$varname=\$new"
|
eval "$varname=\$new"
|
||||||
}
|
}
|
||||||
|
|
||||||
timetoseconds() {
|
time_to_seconds() {
|
||||||
local time=$1
|
local time=$1
|
||||||
unset seconds
|
unset seconds
|
||||||
|
|
||||||
|
@ -30,12 +30,13 @@ load_watchcat() {
|
||||||
config_get period $1 period
|
config_get period $1 period
|
||||||
config_get mode $1 mode
|
config_get mode $1 mode
|
||||||
config_get pinghosts $1 pinghosts
|
config_get pinghosts $1 pinghosts
|
||||||
config_get pingperiod $1 pingperiod
|
config_get ping_period $1 ping_period
|
||||||
config_get nopingtime $1 nopingtime
|
config_get no_ping_time $1 no_ping_time
|
||||||
config_get forcedelay $1 forcedelay
|
config_get force_delay $1 force_delay
|
||||||
|
config_get ping_size $1 ping_size "standard"
|
||||||
|
|
||||||
local nopingtime_dflt="900"
|
local nopingtime_dflt="900"
|
||||||
local forcedelay_dflt="60"
|
local force_delay_default="60"
|
||||||
|
|
||||||
# Fix potential typo in mode (backward compatibility).
|
# Fix potential typo in mode (backward compatibility).
|
||||||
[ "$mode" = "allways" ] && mode="periodic_reboot"
|
[ "$mode" = "allways" ] && mode="periodic_reboot"
|
||||||
|
@ -48,7 +49,7 @@ load_watchcat() {
|
||||||
if [ -z "$period" ]; then
|
if [ -z "$period" ]; then
|
||||||
append_string "error" "period is not set! Use time value(ex: '30'; '4m'; '6h'; '2d')." "; "
|
append_string "error" "period is not set! Use time value(ex: '30'; '4m'; '6h'; '2d')." "; "
|
||||||
else
|
else
|
||||||
timetoseconds "$period"
|
time_to_seconds "$period"
|
||||||
period="$seconds"
|
period="$seconds"
|
||||||
[ "$period" -ge 1 ] ||
|
[ "$period" -ge 1 ] ||
|
||||||
append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
|
append_string "error" "period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d')" "; "
|
||||||
|
@ -57,13 +58,13 @@ load_watchcat() {
|
||||||
[ "$mode" = "periodic_reboot" -o "$mode" = "ping_reboot" ] ||
|
[ "$mode" = "periodic_reboot" -o "$mode" = "ping_reboot" ] ||
|
||||||
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot'" "; "
|
append_string "error" "mode must be 'periodic_reboot' or 'ping_reboot'" "; "
|
||||||
|
|
||||||
if [ -z "$forcedelay" ]; then
|
if [ -z "$force_delay" ]; then
|
||||||
forcedelay="$forcedelay_dflt"
|
force_delay="$force_delay_default"
|
||||||
append_string "warn" "forcedelay is not configured! Defaulted to $forcedelay seconds" "; "
|
append_string "warn" "force_delay is not configured! Defaulted to $force_delay seconds" "; "
|
||||||
else
|
else
|
||||||
[ "$forcedelay" -ge 0 ] || {
|
[ "$force_delay" -ge 0 ] || {
|
||||||
forcedelay="$forcedelay_dflt"
|
force_delay="$force_delay_default"
|
||||||
append_string "warn" "forcedelay is invalid! Defaulted to $forcedelay seconds" "; "
|
append_string "warn" "force_delay is invalid! Defaulted to $force_delay seconds" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
@ -71,33 +72,33 @@ load_watchcat() {
|
||||||
[ -z "$pinghosts" ] &&
|
[ -z "$pinghosts" ] &&
|
||||||
append_string "error" "pinghosts must be set in 'ping_reboot' mode! Use space separated address list (ex: '8.8.8.8 9.9.9.9')" "; "
|
append_string "error" "pinghosts must be set in 'ping_reboot' mode! Use space separated address list (ex: '8.8.8.8 9.9.9.9')" "; "
|
||||||
|
|
||||||
if [ -z "$nopingtime" ]; then
|
if [ -z "$no_ping_time" ]; then
|
||||||
nopingtime="$nopingtime_dflt"
|
no_ping_time="$nopingtime_dflt"
|
||||||
append_string "warn" "nopingtime is not configured! Defaulted to $nopingtime seconds" "; "
|
append_string "warn" "no_ping_time is not configured! Defaulted to $no_ping_time seconds" "; "
|
||||||
else
|
else
|
||||||
timetoseconds "$nopingtime"
|
time_to_seconds "$no_ping_time"
|
||||||
nopingtime="$seconds"
|
no_ping_time="$seconds"
|
||||||
[ "$nopingtime" -ge 0 ] || {
|
[ "$no_ping_time" -ge 0 ] || {
|
||||||
nopingtime="$nopingtime_dflt"
|
no_ping_time="$nopingtime_dflt"
|
||||||
append_string "warn" "nopingtime invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $nopingtime seconds" "; "
|
append_string "warn" "no_ping_time invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $no_ping_time seconds" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
|
|
||||||
local pingperiod_dflt="$((period / 5))"
|
local ping_period_default="$((period / 5))"
|
||||||
|
|
||||||
if [ -z "$pingperiod" ]; then
|
if [ -z "$ping_period" ]; then
|
||||||
pingperiod="$pingperiod_dflt"
|
ping_period="$ping_period_default"
|
||||||
append_string "warn" "pingperiod is not configured! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "ping_period is not configured! Defaulted to $ping_period seconds(1/5 of period)" "; "
|
||||||
else
|
else
|
||||||
timetoseconds "$pingperiod"
|
time_to_seconds "$ping_period"
|
||||||
pingperiod="$seconds"
|
ping_period="$seconds"
|
||||||
[ "$pingperiod" -ge 0 -a "$pingperiod" -ge "$period" ] && {
|
[ "$ping_period" -ge 0 -a "$ping_period" -ge "$period" ] && {
|
||||||
pingperiod="$pingperiod_dflt"
|
ping_period="$ping_period_default"
|
||||||
append_string "warn" "pingperiod is invalid value(greater than period)! Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "ping_period is invalid value(greater than period)! Defaulted to $ping_period seconds(1/5 of period)" "; "
|
||||||
}
|
}
|
||||||
[ "$pingperiod" -ge 0 ] || {
|
[ "$ping_period" -ge 0 ] || {
|
||||||
pingperiod="$pingperiod_dflt"
|
ping_period="$ping_period_default"
|
||||||
append_string "warn" "pingperiod has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $pingperiod seconds(1/5 of period)" "; "
|
append_string "warn" "ping_period has invalid format! Use time value(ex: '30'; '4m'; '6h'; '2d'). Defaulted to $ping_period seconds(1/5 of period)" "; "
|
||||||
}
|
}
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
@ -110,12 +111,12 @@ load_watchcat() {
|
||||||
|
|
||||||
case "$mode" in
|
case "$mode" in
|
||||||
periodic_reboot)
|
periodic_reboot)
|
||||||
/usr/bin/watchcat.sh "periodic_reboot" "$period" "$forcedelay" &
|
/usr/bin/watchcat.sh "periodic_reboot" "$period" "$force_delay" &
|
||||||
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
|
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;force_delay=$force_delay)"
|
||||||
;;
|
;;
|
||||||
ping_reboot)
|
ping_reboot)
|
||||||
/usr/bin/watchcat.sh "ping_reboot" "$period" "$forcedelay" "$ping_hosts" "$ping_period" "$nopingtime" "$ping_size" &
|
/usr/bin/watchcat.sh "ping_reboot" "$period" "$force_delay" "$ping_hosts" "$ping_period" "$no_ping_time" "$ping_size" &
|
||||||
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;ping_hosts=$ping_hosts;ping_period=$ping_period;forcedelay=$forcedelay;nopingtime=$nopingtime)"
|
logger -p user.info -t "watchcat" "started task (mode=$mode;period=$period;ping_hosts=$ping_hosts;ping_period=$ping_period;force_delay=$force_delay;no_ping_time=$no_ping_time;ping_size=$ping_size)"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Error starting Watchcat service. Invalid mode selection: $mode"
|
echo "Error starting Watchcat service. Invalid mode selection: $mode"
|
||||||
|
|
|
@ -5,6 +5,35 @@
|
||||||
# This is free software, licensed under the GNU General Public License v2.
|
# This is free software, licensed under the GNU General Public License v2.
|
||||||
#
|
#
|
||||||
|
|
||||||
|
get_ping_size() {
|
||||||
|
ps=$1
|
||||||
|
case "$ps" in
|
||||||
|
small)
|
||||||
|
ps="1"
|
||||||
|
;;
|
||||||
|
windows)
|
||||||
|
ps="32"
|
||||||
|
;;
|
||||||
|
standard)
|
||||||
|
ps="56"
|
||||||
|
;;
|
||||||
|
big)
|
||||||
|
ps="248"
|
||||||
|
;;
|
||||||
|
huge)
|
||||||
|
ps="1492"
|
||||||
|
;;
|
||||||
|
jumbo)
|
||||||
|
ps="9000"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "Error: invalid ping_size. ping_size should be either: small, windows, standard, big, huge or jumbo"
|
||||||
|
echo "Cooresponding ping packet sizes (bytes): small=1, windows=32, standard=56, big=248, huge=1492, jumbo=9000"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
echo $ps
|
||||||
|
}
|
||||||
|
|
||||||
reboot_now() {
|
reboot_now() {
|
||||||
reboot &
|
reboot &
|
||||||
|
|
||||||
|
@ -17,27 +46,29 @@ reboot_now() {
|
||||||
|
|
||||||
watchcat_periodic() {
|
watchcat_periodic() {
|
||||||
local period="$1"
|
local period="$1"
|
||||||
local forcedelay="$2"
|
local force_delay="$2"
|
||||||
|
|
||||||
sleep "$period" && reboot_now "$forcedelay"
|
sleep "$period" && reboot_now "$force_delay"
|
||||||
}
|
}
|
||||||
|
|
||||||
watchcat_ping() {
|
watchcat_ping() {
|
||||||
local period="$1"
|
local period="$1"
|
||||||
local forcedelay="$2"
|
local force_delay="$2"
|
||||||
local pinghosts="$3"
|
local ping_hosts="$3"
|
||||||
local pingperiod="$4"
|
local ping_period="$4"
|
||||||
local nopingtime="$5"
|
local no_ping_time="$5"
|
||||||
|
local ping_size="$6"
|
||||||
|
|
||||||
local time_now="$(cat /proc/uptime)"
|
local time_now="$(cat /proc/uptime)"
|
||||||
time_now="${time_now%%.*}"
|
time_now="${time_now%%.*}"
|
||||||
|
|
||||||
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime - time_now))"
|
[ "$time_now" -lt "$no_ping_time" ] && sleep "$((no_ping_time - time_now))"
|
||||||
|
|
||||||
time_now="$(cat /proc/uptime)"
|
time_now="$(cat /proc/uptime)"
|
||||||
time_now="${time_now%%.*}"
|
time_now="${time_now%%.*}"
|
||||||
local time_lastcheck="$time_now"
|
local time_lastcheck="$time_now"
|
||||||
local time_lastcheck_withinternet="$time_now"
|
local time_lastcheck_withinternet="$time_now"
|
||||||
|
local ping_size="$(get_ping_size "$ping_size")"
|
||||||
|
|
||||||
while true; do
|
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.
|
# 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.
|
||||||
|
@ -45,21 +76,21 @@ watchcat_ping() {
|
||||||
time_now="${time_now%%.*}"
|
time_now="${time_now%%.*}"
|
||||||
local time_diff="$((time_now - time_lastcheck))"
|
local time_diff="$((time_now - time_lastcheck))"
|
||||||
|
|
||||||
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod - time_diff))"
|
[ "$time_diff" -lt "$ping_period" ] && sleep "$((ping_period - time_diff))"
|
||||||
|
|
||||||
time_now="$(cat /proc/uptime)"
|
time_now="$(cat /proc/uptime)"
|
||||||
time_now="${time_now%%.*}"
|
time_now="${time_now%%.*}"
|
||||||
time_lastcheck="$time_now"
|
time_lastcheck="$time_now"
|
||||||
|
|
||||||
for host in $pinghosts; do
|
for host in $ping_hosts; do
|
||||||
if ping -c 1 "$host" &>/dev/null; then
|
if ping -s "$ping_size" -c 1 "$host" &>/dev/null; then
|
||||||
time_lastcheck_withinternet="$time_now"
|
time_lastcheck_withinternet="$time_now"
|
||||||
else
|
else
|
||||||
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now - time_lastcheck_withinternet)). Reseting when reaching $period"
|
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now - time_lastcheck_withinternet)). Reseting when reaching $period"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
|
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$force_delay"
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,7 +99,7 @@ periodic_reboot)
|
||||||
watchcat_periodic "$2" "$3"
|
watchcat_periodic "$2" "$3"
|
||||||
;;
|
;;
|
||||||
ping_reboot)
|
ping_reboot)
|
||||||
watchcat_ping "$2" "$3" "$4" "$5" "$6"
|
watchcat_ping "$2" "$3" "$4" "$5" "$6" "$7"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
echo "Error: invalid mode selected: $mode"
|
echo "Error: invalid mode selected: $mode"
|
||||||
|
|
Loading…
Reference in a new issue