From 459215b85df61991f64db24f7b4cca7cbc65516e Mon Sep 17 00:00:00 2001 From: nunojpg Date: Wed, 5 Jan 2011 15:33:39 +0000 Subject: [PATCH] [packages] watchcat: update to release 5 git-svn-id: svn://svn.openwrt.org/openwrt/packages@24909 3c298f89-4303-0410-b956-a3cf2f4a3e73 --- utils/watchcat/Makefile | 2 +- utils/watchcat/files/initd_watchcat | 28 ++++++++++++++++------ utils/watchcat/files/uci_defaults_watchcat | 15 +++++++----- utils/watchcat/files/watchcat.sh | 38 +++++++++++++++++++----------- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/utils/watchcat/Makefile b/utils/watchcat/Makefile index 7a2fa2e46..f03c6e769 100644 --- a/utils/watchcat/Makefile +++ b/utils/watchcat/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=watchcat PKG_VERSION:=1 -PKG_RELEASE:=3 +PKG_RELEASE:=5 include $(INCLUDE_DIR)/package.mk diff --git a/utils/watchcat/files/initd_watchcat b/utils/watchcat/files/initd_watchcat index 2d04806ab..36e353f5f 100644 --- a/utils/watchcat/files/initd_watchcat +++ b/utils/watchcat/files/initd_watchcat @@ -27,7 +27,7 @@ load_watchcat() { config_get period $1 period config_get mode $1 mode "allways" config_get pinghosts $1 pinghosts "8.8.8.8" - config_get pingperiod $1 pingperiod "600" + config_get pingperiod $1 pingperiod config_get forcedelay $1 forcedelay "0" error="" @@ -35,13 +35,27 @@ load_watchcat() { timetoseconds "$period" period="$seconds" [ "$period" -ge 1 ] \ - || append_string "error" "period is not set or not recognized" "; " + || append_string "error" 'period is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; " [ "$mode" = "allways" -o "$mode" = "ping" ] \ || append_string "error" "mode must be 'allways' or 'ping'" "; " [ -n "$pinghosts" -o "$mode" = "allways" ] \ - || append_string "error" "pinghosts must be set in 'ping' mode" "; " - timetoseconds "$pingperiod" - pingperiod="$seconds" + || append_string "error" "pinghosts must be set when in 'ping' mode" "; " + [ "$mode" = "ping" ] && { + if [ -n "$pingperiod" ] + then + timetoseconds "$pingperiod" + pingperiod="$seconds" + if [ "$pingperiod" -ge 0 ] + then + [ "$pingperiod" -le "$period" ] \ + || append_string "error" "pingperiod must be less than period" "; " + else + append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; " + fi + else + pingperiod="$((period/20))" + fi + } [ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \ || append_string "error" "pingperiod is not recognized" "; " [ "$forcedelay" -ge 0 ] \ @@ -55,7 +69,7 @@ load_watchcat() { logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)" else /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" & - logger -p user.info -t "wathchat" "started task (mode=$mode;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)" + logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)" fi echo $! >> "${PIDFILE}.pids" @@ -87,7 +101,7 @@ start() { then logger -p user.info -t "watchcat" "starting all tasks" config_foreach load_watchcat watchcat - logger -p user.info -t "watchcat" "all tasks started" + logger -p user.info -t "watchcat" "all tasks started" else logger -p user.info -t "watchcat" "no tasks defined" fi diff --git a/utils/watchcat/files/uci_defaults_watchcat b/utils/watchcat/files/uci_defaults_watchcat index 5b01e699b..1c457d7a9 100644 --- a/utils/watchcat/files/uci_defaults_watchcat +++ b/utils/watchcat/files/uci_defaults_watchcat @@ -1,7 +1,10 @@ #!/bin/sh -uci add system watchcat -uci set system.@watchcat[0].period=6h -uci set system.@watchcat[0].mode=ping -uci set system.@watchcat[0].pinghosts=8.8.8.8 -uci set system.@watchcat[0].forcedelay=30 -uci commit + +[ -n "$(uci -q show system.@watchcat[0])" ] || { + uci add system watchcat + uci set system.@watchcat[0].period=6h + uci set system.@watchcat[0].mode=ping + uci set system.@watchcat[0].pinghosts=8.8.8.8 + uci set system.@watchcat[0].forcedelay=30 + uci commit +} diff --git a/utils/watchcat/files/watchcat.sh b/utils/watchcat/files/watchcat.sh index 73aad9e5b..ee9b6bdfa 100644 --- a/utils/watchcat/files/watchcat.sh +++ b/utils/watchcat/files/watchcat.sh @@ -6,7 +6,7 @@ shutdown_now() { local forcedelay="$1" reboot & - + [ "$forcedelay" -ge 1 ] && { sleep "$forcedelay" @@ -15,39 +15,49 @@ shutdown_now() { } watchcat_allways() { - local period="$1"; local forcedelay="$2" - + local period="$1"; local forcedelay="$2" + sleep "$period" && shutdown_now "$forcedelay" } watchcat_ping() { local period="$1"; local forcedelay="$2"; local pinghosts="$3"; local pingperiod="$4" - + time_now="$(cat /proc/uptime)" time_now="${time_now%%.*}" - time_last="$time_now" + time_lastcheck="$time_now" + time_lastcheck_withinternet="$time_now" while true do - sleep "$pingperiod" - + # 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%%.*}" - - for host in "$pinghosts" + time_diff="$((time_now-time_lastcheck))" + + [ "$time_diff" -lt "$pingperiod" ] && { + sleep_time="$((pingperiod-time_diff))" + sleep "$sleep_time" + } + + 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 - time_last="$time_now" + time_lastcheck_withinternet="$time_now" else - time_diff="$((time_now-time_last))" - logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period" + time_diff="$((time_now-time_lastcheck_withinternet))" + logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period" fi done - time_diff="$((time_now-time_last))" + time_diff="$((time_now-time_lastcheck_withinternet))" [ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay" - + done } -- 2.11.0