mirror of
https://github.com/novatiq/packages.git
synced 2026-04-30 07:28:39 +01:00
watchcat: add nopingtime option + refactoring
nopingtime UCI option rationale: I want relatively fast reaction(i.e. 1m or 2m) for 'no internet' condition, but i don't want my router to reboot every 1 minute if there is still no internet after reboot. initd_watchcat: * add: nopingtime uci option support * add: defaults to all non-critical options * add: log warnings for non-critical errors(when option is missed and default is applyed) * fix: error handling and config_get defaults are somtimes in conflict because of config_get defaults. They are gone now, error handling improved. * fix: calling watchcat.sh with 'period' mode instead of 'ping'. Typo? * fix: pingperiod default changed from period/20 to more reasonable period/5 watchcat.sh: * add: nopingtime uci option support( sleep if uptime < nopingtime ) * remove: [ "$mode" = "allways" ] && mode="always" - not needed, already done by initd_watchcat in load_watchcat() func * add: echo 1 > /proc/sys/kernel/sysrq before sysrq-trigger * refactor: eliminated once used not needed variables, code size reduced. * PKG_RELEASE bumped up Signed-off-by: Vasily Trotzky <trotzky.vas@gmail.com>
This commit is contained in:
@@ -5,19 +5,12 @@
|
||||
# This is free software, licensed under the GNU General Public License v2.
|
||||
#
|
||||
|
||||
mode="$1"
|
||||
|
||||
# Fix potential typo in mode (backward compatibility).
|
||||
[ "$mode" = "allways" ] && mode="always"
|
||||
|
||||
shutdown_now() {
|
||||
local forcedelay="$1"
|
||||
|
||||
reboot_now() {
|
||||
reboot &
|
||||
|
||||
[ "$forcedelay" -ge 1 ] && {
|
||||
sleep "$forcedelay"
|
||||
|
||||
[ "$1" -ge 1 ] && {
|
||||
sleep "$1"
|
||||
echo 1 > /proc/sys/kernel/sysrq
|
||||
echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
|
||||
}
|
||||
}
|
||||
@@ -25,31 +18,29 @@ shutdown_now() {
|
||||
watchcat_always() {
|
||||
local period="$1"; local forcedelay="$2"
|
||||
|
||||
sleep "$period" && shutdown_now "$forcedelay"
|
||||
sleep "$period" && reboot_now "$forcedelay"
|
||||
}
|
||||
|
||||
watchcat_ping() {
|
||||
local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"
|
||||
local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4"; local nopingtime="$5"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
local time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
||||
|
||||
[ "$time_now" -lt "$nopingtime" ] && sleep "$((nopingtime-time_now))"
|
||||
|
||||
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
||||
local time_lastcheck="$time_now"
|
||||
local time_lastcheck_withinternet="$time_now"
|
||||
|
||||
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)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_diff="$((time_now-time_lastcheck))"
|
||||
time_now="$(cat /proc/uptime)"; time_now="${time_now%%.*}"
|
||||
local time_diff="$((time_now-time_lastcheck))"
|
||||
|
||||
[ "$time_diff" -lt "$pingperiod" ] && {
|
||||
sleep_time="$((pingperiod-time_diff))"
|
||||
sleep "$sleep_time"
|
||||
}
|
||||
[ "$time_diff" -lt "$pingperiod" ] && sleep "$((pingperiod-time_diff))"
|
||||
|
||||
time_now="$(cat /proc/uptime)"
|
||||
time_now="${time_now%%.*}"
|
||||
time_now="$(cat /proc/uptime)";time_now="${time_now%%.*}"
|
||||
time_lastcheck="$time_now"
|
||||
|
||||
for host in $pinghosts
|
||||
@@ -57,21 +48,18 @@ watchcat_ping() {
|
||||
if ping -c 1 "$host" &> /dev/null
|
||||
then
|
||||
time_lastcheck_withinternet="$time_now"
|
||||
else
|
||||
time_diff="$((time_now-time_lastcheck_withinternet))"
|
||||
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period"
|
||||
else
|
||||
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $((time_now-time_lastcheck_withinternet)). Reseting when reaching $period"
|
||||
fi
|
||||
done
|
||||
|
||||
time_diff="$((time_now-time_lastcheck_withinternet))"
|
||||
[ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay"
|
||||
|
||||
|
||||
[ "$((time_now-time_lastcheck_withinternet))" -ge "$period" ] && reboot_now "$forcedelay"
|
||||
done
|
||||
}
|
||||
|
||||
if [ "$mode" = "always" ]
|
||||
then
|
||||
watchcat_always "$2" "$3"
|
||||
else
|
||||
watchcat_ping "$2" "$3" "$4" "$5"
|
||||
fi
|
||||
if [ "$1" = "always" ]
|
||||
then
|
||||
watchcat_always "$2" "$3"
|
||||
else
|
||||
watchcat_ping "$2" "$3" "$4" "$5" "$6"
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user