[packages] watchcat: update to release 5

git-svn-id: svn://svn.openwrt.org/openwrt/packages@24909 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
nunojpg 2011-01-05 15:33:39 +00:00
parent 2b1b5338c9
commit 459215b85d
4 changed files with 55 additions and 28 deletions

View File

@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk
PKG_NAME:=watchcat PKG_NAME:=watchcat
PKG_VERSION:=1 PKG_VERSION:=1
PKG_RELEASE:=3 PKG_RELEASE:=5
include $(INCLUDE_DIR)/package.mk include $(INCLUDE_DIR)/package.mk

View File

@ -27,7 +27,7 @@ load_watchcat() {
config_get period $1 period config_get period $1 period
config_get mode $1 mode "allways" config_get mode $1 mode "allways"
config_get pinghosts $1 pinghosts "8.8.8.8" 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" config_get forcedelay $1 forcedelay "0"
error="" error=""
@ -35,13 +35,27 @@ load_watchcat() {
timetoseconds "$period" timetoseconds "$period"
period="$seconds" period="$seconds"
[ "$period" -ge 1 ] \ [ "$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" ] \ [ "$mode" = "allways" -o "$mode" = "ping" ] \
|| append_string "error" "mode must be 'allways' or 'ping'" "; " || append_string "error" "mode must be 'allways' or 'ping'" "; "
[ -n "$pinghosts" -o "$mode" = "allways" ] \ [ -n "$pinghosts" -o "$mode" = "allways" ] \
|| append_string "error" "pinghosts must be set in 'ping' mode" "; " || append_string "error" "pinghosts must be set when in 'ping' mode" "; "
timetoseconds "$pingperiod" [ "$mode" = "ping" ] && {
pingperiod="$seconds" 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" ] \ [ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \
|| append_string "error" "pingperiod is not recognized" "; " || append_string "error" "pingperiod is not recognized" "; "
[ "$forcedelay" -ge 0 ] \ [ "$forcedelay" -ge 0 ] \
@ -55,7 +69,7 @@ load_watchcat() {
logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)" logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
else else
/usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" & /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 fi
echo $! >> "${PIDFILE}.pids" echo $! >> "${PIDFILE}.pids"
@ -87,7 +101,7 @@ start() {
then then
logger -p user.info -t "watchcat" "starting all tasks" logger -p user.info -t "watchcat" "starting all tasks"
config_foreach load_watchcat watchcat config_foreach load_watchcat watchcat
logger -p user.info -t "watchcat" "all tasks started" logger -p user.info -t "watchcat" "all tasks started"
else else
logger -p user.info -t "watchcat" "no tasks defined" logger -p user.info -t "watchcat" "no tasks defined"
fi fi

View File

@ -1,7 +1,10 @@
#!/bin/sh #!/bin/sh
uci add system watchcat
uci set system.@watchcat[0].period=6h [ -n "$(uci -q show system.@watchcat[0])" ] || {
uci set system.@watchcat[0].mode=ping uci add system watchcat
uci set system.@watchcat[0].pinghosts=8.8.8.8 uci set system.@watchcat[0].period=6h
uci set system.@watchcat[0].forcedelay=30 uci set system.@watchcat[0].mode=ping
uci commit uci set system.@watchcat[0].pinghosts=8.8.8.8
uci set system.@watchcat[0].forcedelay=30
uci commit
}

View File

@ -6,7 +6,7 @@ shutdown_now() {
local forcedelay="$1" local forcedelay="$1"
reboot & reboot &
[ "$forcedelay" -ge 1 ] && { [ "$forcedelay" -ge 1 ] && {
sleep "$forcedelay" sleep "$forcedelay"
@ -15,39 +15,49 @@ shutdown_now() {
} }
watchcat_allways() { watchcat_allways() {
local period="$1"; local forcedelay="$2" local period="$1"; local forcedelay="$2"
sleep "$period" && shutdown_now "$forcedelay" sleep "$period" && shutdown_now "$forcedelay"
} }
watchcat_ping() { 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"
time_now="$(cat /proc/uptime)" time_now="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_last="$time_now" time_lastcheck="$time_now"
time_lastcheck_withinternet="$time_now"
while true while true
do 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="$(cat /proc/uptime)"
time_now="${time_now%%.*}" time_now="${time_now%%.*}"
time_diff="$((time_now-time_lastcheck))"
for host in "$pinghosts"
[ "$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 do
if ping -c 1 "$host" &> /dev/null if ping -c 1 "$host" &> /dev/null
then then
time_last="$time_now" time_lastcheck_withinternet="$time_now"
else else
time_diff="$((time_now-time_last))" time_diff="$((time_now-time_lastcheck_withinternet))"
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period" logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $time_diff seconds. Reseting when reaching $period"
fi fi
done done
time_diff="$((time_now-time_last))" time_diff="$((time_now-time_lastcheck_withinternet))"
[ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay" [ "$time_diff" -ge "$period" ] && shutdown_now "$forcedelay"
done done
} }