[packages] watchcat: bug fix
[openwrt/svn-archive/archive.git] / utils / watchcat / files / initd_watchcat
1 #!/bin/sh /etc/rc.common
2
3 START=01
4
5 PIDFILE="/tmp/run/watchcat"
6
7 append_string() {
8 local varname="$1"; local add="$2"; local separator="${3:- }"; local actual
9 eval "actual=\$$varname"
10
11 new="${actual:+$actual$separator}$add"
12 eval "$varname=\$new"
13 }
14
15 timetoseconds() {
16 local time=$1
17 unset seconds
18
19 { [ "$time" -ge 1 ] 2> /dev/null && seconds="$time"; } || \
20 { [ "${time%s}" -ge 1 ] 2> /dev/null && seconds="${time%s}"; } || \
21 { [ "${time%m}" -ge 1 ] 2> /dev/null && seconds=$((${time%m}*60)); } || \
22 { [ "${time%h}" -ge 1 ] 2> /dev/null && seconds=$((${time%h}*3600)); } || \
23 { [ "${time%d}" -ge 1 ] 2> /dev/null && seconds=$((${time%d}*86400)); }
24 }
25
26 load_watchcat() {
27 config_get period $1 period
28 config_get mode $1 mode "allways"
29 config_get pinghosts $1 pinghosts "8.8.8.8"
30 config_get pingperiod $1 pingperiod "600"
31 config_get forcedelay $1 forcedelay "0"
32
33 error=""
34
35 timetoseconds "$period"
36 period="$seconds"
37 [ "$period" -ge 1 ] \
38 || append_string "error" "period is not set or not recognized" "; "
39 [ "$mode" = "allways" -o "$mode" = "ping" ] \
40 || append_string "error" "mode must be 'allways' or 'ping'" "; "
41 [ -n "$pinghosts" -o "$mode" = "allways" ] \
42 || append_string "error" "pinghosts must be set in 'ping' mode" "; "
43 timetoseconds "$pingperiod"
44 pingperiod="$seconds"
45 [ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \
46 || append_string "error" "pingperiod is not recognized" "; "
47 [ "$forcedelay" -ge 0 ] \
48 || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
49
50 [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
51
52 if [ "$mode" = "allways" ]
53 then
54 /usr/bin/watchcat.sh "allways" "$period" "$forcedelay" &
55 logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
56 else
57 /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
58 logger -p user.info -t "wathchat" "started task (mode=$mode;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
59 fi
60
61 echo $! >> "${PIDFILE}.pids"
62 }
63
64 stop() {
65 if [ -f "${PIDFILE}.pids" ]
66 then
67 logger -p user.info -t "watchcat" "stopping all tasks"
68
69 while read pid
70 do
71 kill "$pid"
72 done < "${PIDFILE}.pids"
73
74 rm "${PIDFILE}.pids"
75
76 logger -p user.info -t "watchcat" "all tasks stopped"
77 else
78 logger -p user.info -t "watchcat" "no tasks running"
79 fi
80 }
81
82 start() {
83 [ -f "${PIDFILE}.pids" ] && stop
84
85 config_load system
86 if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
87 then
88 logger -p user.info -t "watchcat" "starting all tasks"
89 config_foreach load_watchcat watchcat
90 logger -p user.info -t "watchcat" "all tasks started"
91 else
92 logger -p user.info -t "watchcat" "no tasks defined"
93 fi
94 }