|
@@ -0,0 +1,108 @@
|
|
1
|
+#!/bin/sh /etc/rc.common
|
|
2
|
+
|
|
3
|
+START=97
|
|
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
|
|
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 a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
|
|
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 when in 'ping' mode" "; "
|
|
43
|
+ [ "$mode" = "ping" ] && {
|
|
44
|
+ if [ -n "$pingperiod" ]
|
|
45
|
+ then
|
|
46
|
+ timetoseconds "$pingperiod"
|
|
47
|
+ pingperiod="$seconds"
|
|
48
|
+ if [ "$pingperiod" -ge 0 ]
|
|
49
|
+ then
|
|
50
|
+ [ "$pingperiod" -le "$period" ] \
|
|
51
|
+ || append_string "error" "pingperiod must be less than period" "; "
|
|
52
|
+ else
|
|
53
|
+ append_string "error" 'pingperiod is not a valid time value (ex: "30"; "4m"; "6h"; "2d")' "; "
|
|
54
|
+ fi
|
|
55
|
+ else
|
|
56
|
+ pingperiod="$((period/20))"
|
|
57
|
+ fi
|
|
58
|
+ }
|
|
59
|
+ [ "$pingperiod" -lt "$period" -o "$mode" = "allways" ] \
|
|
60
|
+ || append_string "error" "pingperiod is not recognized" "; "
|
|
61
|
+ [ "$forcedelay" -ge 0 ] \
|
|
62
|
+ || append_string "error" "forcedelay must be a integer greater or equal than 0, where 0 means disabled" "; "
|
|
63
|
+
|
|
64
|
+ [ -n "$error" ] && { logger -p user.err -t "watchcat" "reboot program $1 not started - $error"; return; }
|
|
65
|
+
|
|
66
|
+ if [ "$mode" = "allways" ]
|
|
67
|
+ then
|
|
68
|
+ /usr/bin/watchcat.sh "allways" "$period" "$forcedelay" &
|
|
69
|
+ logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;forcedelay=$forcedelay)"
|
|
70
|
+ else
|
|
71
|
+ /usr/bin/watchcat.sh "period" "$period" "$forcedelay" "$pinghosts" "$pingperiod" &
|
|
72
|
+ logger -p user.info -t "wathchat" "started task (mode=$mode;period=$period;pinghosts=$pinghosts;pingperiod=$pingperiod;forcedelay=$forcedelay)"
|
|
73
|
+ fi
|
|
74
|
+
|
|
75
|
+ echo $! >> "${PIDFILE}.pids"
|
|
76
|
+}
|
|
77
|
+
|
|
78
|
+stop() {
|
|
79
|
+ if [ -f "${PIDFILE}.pids" ]
|
|
80
|
+ then
|
|
81
|
+ logger -p user.info -t "watchcat" "stopping all tasks"
|
|
82
|
+
|
|
83
|
+ while read pid
|
|
84
|
+ do
|
|
85
|
+ kill "$pid"
|
|
86
|
+ done < "${PIDFILE}.pids"
|
|
87
|
+
|
|
88
|
+ rm "${PIDFILE}.pids"
|
|
89
|
+
|
|
90
|
+ logger -p user.info -t "watchcat" "all tasks stopped"
|
|
91
|
+ else
|
|
92
|
+ logger -p user.info -t "watchcat" "no tasks running"
|
|
93
|
+ fi
|
|
94
|
+}
|
|
95
|
+
|
|
96
|
+start() {
|
|
97
|
+ [ -f "${PIDFILE}.pids" ] && stop
|
|
98
|
+
|
|
99
|
+ config_load system
|
|
100
|
+ if [ -n "$(uci show system.@watchcat[0])" ] # at least one watchcat section exists
|
|
101
|
+ then
|
|
102
|
+ logger -p user.info -t "watchcat" "starting all tasks"
|
|
103
|
+ config_foreach load_watchcat watchcat
|
|
104
|
+ logger -p user.info -t "watchcat" "all tasks started"
|
|
105
|
+ else
|
|
106
|
+ logger -p user.info -t "watchcat" "no tasks defined"
|
|
107
|
+ fi
|
|
108
|
+}
|