watchcat: add support for specifying ping packet size

Signed-off-by: Nicholas Smith <nicholas@nbembedded.com>
This commit is contained in:
Nicholas Smith
2020-12-13 16:59:08 +10:00
parent ddd2729313
commit a4ce06de1c
3 changed files with 81 additions and 49 deletions
+43 -12
View File
@@ -5,6 +5,35 @@
# 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 &
@@ -17,27 +46,29 @@ reboot_now() {
watchcat_periodic() {
local period="$1"
local forcedelay="$2"
local force_delay="$2"
sleep "$period" && reboot_now "$forcedelay"
sleep "$period" && reboot_now "$force_delay"
}
watchcat_ping() {
local period="$1"
local forcedelay="$2"
local pinghosts="$3"
local pingperiod="$4"
local nopingtime="$5"
local force_delay="$2"
local ping_hosts="$3"
local ping_period="$4"
local no_ping_time="$5"
local ping_size="$6"
local time_now="$(cat /proc/uptime)"
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="${time_now%%.*}"
local time_lastcheck="$time_now"
local time_lastcheck_withinternet="$time_now"
local ping_size="$(get_ping_size "$ping_size")"
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.
@@ -45,21 +76,21 @@ watchcat_ping() {
time_now="${time_now%%.*}"
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="${time_now%%.*}"
time_lastcheck="$time_now"
for host in $pinghosts; do
if ping -c 1 "$host" &>/dev/null; then
for host in $ping_hosts; do
if ping -s "$ping_size" -c 1 "$host" &>/dev/null; then
time_lastcheck_withinternet="$time_now"
else
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now - time_lastcheck_withinternet)). Reseting when reaching $period"
fi
done
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
[ "$((time_now - time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$force_delay"
done
}
@@ -68,7 +99,7 @@ periodic_reboot)
watchcat_periodic "$2" "$3"
;;
ping_reboot)
watchcat_ping "$2" "$3" "$4" "$5" "$6"
watchcat_ping "$2" "$3" "$4" "$5" "$6" "$7"
;;
*)
echo "Error: invalid mode selected: $mode"