[packages] watchcat: bug fix

git-svn-id: svn://svn.openwrt.org/openwrt/packages@24808 3c298f89-4303-0410-b956-a3cf2f4a3e73
This commit is contained in:
nunojpg 2010-12-22 23:14:56 +00:00
parent d5391e6226
commit 0e355e196b
2 changed files with 33 additions and 76 deletions

View File

@ -34,6 +34,8 @@ endef
define Package/watchcat/install define Package/watchcat/install
$(INSTALL_DIR) $(1)/etc/init.d $(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/initd_watchcat $(1)/etc/init.d/watchcat $(INSTALL_BIN) ./files/initd_watchcat $(1)/etc/init.d/watchcat
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) ./files/watchcat.sh $(1)/usr/bin/watchcat.sh
$(INSTALL_DIR) $(1)/etc/defaults $(INSTALL_DIR) $(1)/etc/defaults
$(INSTALL_BIN) ./files/uci_defaults_watchcat $(1)/etc/defaults/50-watchcat $(INSTALL_BIN) ./files/uci_defaults_watchcat $(1)/etc/defaults/50-watchcat
endef endef

View File

@ -5,58 +5,13 @@ START=01
PIDFILE="/tmp/run/watchcat" PIDFILE="/tmp/run/watchcat"
append_string() { append_string() {
local varname="$1"; local actual="${!varname}"; local add="$2"; local separator="${3:- }" local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
eval "actual=\$$varname"
new="${actual:+$actual$separator}$add" new="${actual:+$actual$separator}$add"
eval "$varname=\$new" eval "$varname=\$new"
} }
shutdown_now() {
local forcedelay="$1"
reboot &
[ "$forcedelay" -ge 1 ] && {
sleep $forcedelay
echo b > /proc/sysrq-trigger # Will immediately reboot the system without syncing or unmounting your disks.
}
}
watchcat_allways() {
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=$(cat /proc/uptime) && time=${time%%.*}
last=$time
while true
do
sleep $pingperiod
time=$(cat /proc/uptime) && time=${time%%.*}
for host in "$pinghosts"
do
if ping -c 1 "$host" &> /dev/null
then
last=$time
else
diff=$((time-last))
logger -p daemon.info -t "watchcat[$$]" "no internet connectivity for $diff seconds. Reseting when reaching $period"
fi
done
diff=$((time-last))
[ $diff -ge $period ] && shutdown_now "$forcedelay"
done
}
timetoseconds() { timetoseconds() {
local time=$1 local time=$1
unset seconds unset seconds
@ -79,15 +34,15 @@ 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 set or not recognized" "; "
[ "$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 in 'ping' mode" "; "
timetoseconds "$pingperiod" timetoseconds "$pingperiod"
pingperiod="$seconds" pingperiod="$seconds"
[ "$pingperiod" -l "$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 ] \
|| append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; " || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
@ -96,44 +51,44 @@ load_watchcat() {
if [ "$mode" = "allways" ] if [ "$mode" = "allways" ]
then then
watchcat_allways "$period" "$forcedelay" & /usr/bin/watchcat.sh "allways" "$period" "$forcedelay" &
logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
else else
watchcat_ping "$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)"
fi fi
echo $! >> "$PIDFILE".pids echo $! >> "${PIDFILE}.pids"
logger -p user.info -t "sshtunnel" "started tunnels to $server (pid=$!;retrydelay=$retrydelay)"
} }
stop() { stop() {
if [ -f "$PIDFILE".pids ] if [ -f "${PIDFILE}.pids" ]
then then
logger -p user.info -t "watchcat" "stopping all reboot programs" logger -p user.info -t "watchcat" "stopping all tasks"
while read pid while read pid
do do
kill $pid kill "$pid"
done < "$PIDFILE".pids done < "${PIDFILE}.pids"
rm "$PIDFILE".pids rm "${PIDFILE}.pids"
logger -p user.info -t "watchcat" "all reboot programs stopped" logger -p user.info -t "watchcat" "all tasks stopped"
else else
logger -p user.info -t "watchcat" "no reboot programs running" logger -p user.info -t "watchcat" "no tasks running"
fi fi
} }
start() { start() {
[ -f "$PIDFILE".pids ] && stop [ -f "${PIDFILE}.pids" ] && stop
config_load system config_load system
if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
then then
logger -p user.info -t "watchcat" "starting all reboot programs" 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 reboot programs started" logger -p user.info -t "watchcat" "all tasks started"
else else
logger -p user.info -t "watchcat" "no reboot programs defined" logger -p user.info -t "watchcat" "no tasks defined"
fi fi
} }