-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactor_internet_check.sh
227 lines (210 loc) · 7.27 KB
/
reactor_internet_check.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/bin/sh
#
# Copyright (C) 2020 Patrick H. Rigney, All Rights Reserved
# This file is part of Reactor.
# See https://www.toggledbits.com/static/reactor/docs/Installation/#license-and-use-restrictions
#
# THIS FILE IS AUTOMATICALLY OVERWRITTEN BY REACTOR PLUGIN UPGRADES. IF YOU CUSTOMIZE,
# KEEP A BACKUP!
# See: https://www.toggledbits.com/static/reactor/docs/3.9/Network-Status/
VERSION="21129.0925"
LOGFILE=/tmp/reactor_internet_check.log
CONF=
PING=ping
PINGOPTS="-q -c 3 -W 3" # quiet, three pings, three seconds max wait per ping
UID=`id -u`
install() {
[ "$UID" -eq 0 ] || { echo "$0: must be run as root (sudo?)" >&2; exit 255; }
if [ -L /sbin/init ]; then
# init is a link to systemd on those systems
echo "Installing daemon for systemd/systemctl..."
systemctl disable reactor-internet-check.service >/dev/null 2>&1
systemctl stop reactor-internet-check.service >/dev/null 2>&1
cat <<SYSD >/etc/systemd/system/reactor-internet-check.service
[Unit]
Description=Reactor Internet Check Daemon
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStartPre=/bin/sleep 10
WorkingDirectory=$(dirname $(realpath $0))
ExecStart=$(realpath $0) -L /tmp/reactor_internet_check.log
Restart=on-failure
RestartSec=60
StandardOutput=syslog
StandardError=syslog
[Install]
WantedBy=multi-user.target
SYSD
systemctl daemon-reload
systemctl enable reactor-internet-check.service
systemctl start reactor-internet-check.service
return 0
elif [ -d /etc/init.d -a -x /sbin/procd ]; then
echo "Installing for sysvinit with procd..."
[ -f /etc/init.d/reactor_internet_check ] && /etc/init.d/reactor_internet_check stop >/dev/null 2>&1 && sleep 5
cat <<PROCD >/etc/init.d/reactor_internet_check
#!/bin/sh /etc/rc.common
# THIS FILE IS AUTOMATICALLY GENERATED -- DO NOT EDIT -- $VERSION
START=85
STOP=10
USE_PROCD=1
PROG=/etc/cmh-ludl/reactor_internet_check.sh
LOGFILE=/tmp/reactor_internet_check.log
start_service () {
rm -f /var/run/reactor_internet_check.stop
procd_open_instance
procd_set_param command \$PROG -L "\$LOGFILE"
procd_set_param limits core="unlimited"
procd_set_param respawn 3600 90 10
procd_set_param pidfile /var/run/reactor_internet_check.pid
procd_close_instance
}
PROCD
chmod ug+rx,o= /etc/init.d/reactor_internet_check
/etc/init.d/reactor_internet_check disable
rm -f /etc/rc.d/S*reactor_internet_check /etc/rc.d/K*reactor_internet_check
/etc/init.d/reactor_internet_check enable
/etc/init.d/reactor_internet_check start
return 0
elif [ -d /etc/init.d ]; then
# sysvinit no procd
echo "Installing for sysvinit without procd..."
[ -f /etc/init.d/reactor_internet_check ] && /etc/init.d/reactor_internet_check stop >/dev/null 2>&1 && sleep 5
cat <<PROCS >/etc/init.d/reactor_internet_check
#!/bin/sh /etc/rc.common
# THIS FILE IS AUTOMATICALLY GENERATED -- DO NOT EDIT -- $VERSION
START=85
STOP=10
# Non-procd version compatible with older systems.
PROG=/etc/cmh-ludl/reactor_internet_check.sh
LOGFILE=/tmp/reactor_internet_check.log
get_pid() {
PID=\$(pgrep -f "\$PROG")
}
start() {
get_pid
[ -n "\$PID" ] && { echo "already running"; exit 255; }
rm -f /var/run/reactor_internet_check.stop
\$PROG -L "\$LOGFILE" &
sleep 2
get_pid
echo \$PID >/var/run/reactor_internet_check.pid
}
stop() {
get_pid
if [ -n "\$PID" ]; then
touch /var/run/reactor_internet_check.stop
pkill -P \$PID sleep
kill -TERM \$PID
fi
rm -f /var/run/reactor_internet_check.pid
}
PROCS
chmod ug+rx,o= /etc/init.d/reactor_internet_check
/etc/init.d/reactor_internet_check disable
rm -f /etc/rc.d/S*reactor_internet_check /etc/rc.d/K*reactor_internet_check
/etc/init.d/reactor_internet_check enable
/etc/init.d/reactor_internet_check start
return 0
else
echo "Don't know how to install for this platform. Please install manually."
echo "Refer to docs at https://www.toggledbits.com/static/reactor/docs/3.9/Network-Status/"
return 255
fi
}
uninstall() {
[ "$UID" -eq 0 ] || { echo "$0: must be run as root (sudo?)" >&2; exit 255; }
if [ -f /etc/systemd/system/reactor-internet-check.service ]; then
echo "Uninstalling from systemd..."
systemctl stop reactor-internet-check.service
systemctl disable reactor-internet-check.service
elif [ -d /etc/init.d ]; then
echo "Uninstalling from Vera (${PLATFORM})..."
if [ -f /etc/init.d/reactor_internet_check ]; then
chmod +x /etc/init.d/reactor_internet_check
/etc/init.d/reactor_internet_check stop
/etc/init.d/reactor_internet_check disable
fi
rm -f /etc/rc.d/S*reactor_internet_check /etc/rc.d/K*reactor_internet_check
return 0
else
echo "Don't know how to uninstall for this PLATFORM (${PLATFORM}). Please uninstall manually."
echo "Refer to docs at https://www.toggledbits.com/static/reactor/docs/3.9/Network-Status/"
return 255
fi
}
while getopts "c:L:IU" opt; do
case "${opt}" in
c) CONF=${OPTARG}
;;
L) LOGFILE=${OPTARG}
;;
I) install ; exit $?
;;
U) uninstall ; exit $?
;;
:) echo "Error: ${opt} requires an argument"
exit 255
;;
*) echo "Usage: $0 [-c config] [-L logfile] run daemon"
echo " default config is /tmp/reactor_internet_check.conf"
echo " $0 -I install daemon"
echo " $0 -U uninstall daemon"
exit 255
;;
esac
done
sleep=60
newstate=""
lastn=1
check_target() {
echo "Check #${n}: $1..."
if ${PING} ${PINGOPTS} $1; then
newstate=1
else
newstate=0
fi
}
attempt() {
n=0
while [ $n -lt 3 ]; do
target=`echo $SERVERS | cut -d ',' -f $lastn`
if [ -z "$target" ]; then
lastn=1
else
n=`expr $n + 1`
lastn=`expr $lastn + 1`
check_target $target
if [ $newstate -eq 1 ]; then
return
fi
fi
done
echo "$n targets failed"
newstate=0
}
check() {
echo "Checking Internet at $(date)..."
echo "Server list is ${SERVERS}; next is ${lastn}"
sleep=60
attempt
echo "Updating Reactor state to $newstate"
if curl -s -o /dev/null -m 15 'http://127.0.0.1:3480/data_request?id=lr_Reactor&action=internet&state='$newstate ; then
[ $newstate -eq 1 ] && sleep=$INTERVAL # longer sleep
echo "Successfully updated Reactor; next attempt in ${sleep}s"
else
echo "Reactor update failed; luup busy? (will retry) in ${sleep}s"
fi
}
# NOTE: IF YOU NEED/WANT TO CHANGE THE SERVERS PROBED, CHANGE THE CONF FILE, NOT THIS SCRIPT!
while [ ! -f /var/run/reactor_internet_check.stop ]; do
[ -f "${CONF:=/tmp/reactor_internet_check.conf}" ] && source "$CONF"
SERVERS="${SERVERS:-8.8.8.8,8.8.4.4,1.1.1.1,www.facebook.com,www.google.com,www.amazon.com}"
INTERVAL=${INTERVAL:-300}
[ $INTERVAL -eq 0 ] && { echo "Check disabled" >$LOGFILE ; exit 0; }
check 2>&1 >$LOGFILE
sleep $sleep
done
exit 0