-
Notifications
You must be signed in to change notification settings - Fork 13
/
wfs
executable file
·333 lines (275 loc) · 6.6 KB
/
wfs
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env bash
#
# Linux WAN failover script.
#
# Copyright 2010 Louwrentius
#
# Licence = GNU GPL
#
VERSION=2.05
CONFIG=/etc/wfs
CONFIG_FILE="$CONFIG/wfs.conf"
if [ -e "$CONFIG_FILE" ]
then
. $CONFIG_FILE
else
TARGETS_FILE="$CONFIG/targets.txt"
PRIMARY_GW=10.0.0.1
SECONDARY_GW=192.168.1.1
MAX_LATENCY=1
INTERVAL=20
TEST_COUNT=2
THRESHOLD=3
COOLDOWNDELAY=20
TTL=""
COOLDOWNDELAY01=3600
COOLDOWNDELAY02=600
MAIL_TARGET=""
DAEMON=1
QUIET=0
PIDFILE=/var/run/wfs.pid
#
# If some command must be run after a failover or restore, please specify
# the commands within these variables.
#
PRIMARY_CMD=""
SECONDARY_CMD=""
fi
TARGETS_FAILED=0
ACTIVE_CONNECTION=""
TEST_INTERVAL="$INTERVAL"
NO_OF_TARGETS=ERROR
# --- do not change anything below ---
route -n | grep -w "$PRIMARY_GW" | grep -w "0.0.0.0" >> /dev/null 2>&1
if [ "$?" = "0" ]
then
ACTIVE_CONNECTION=PRIMARY
else
ACTIVE_CONNECTION=SECONDARY
fi
log () {
TYPE="$1"
MSG="$2"
DATE=`date +%b\ %d\ %H:%M:%S`
case "$TYPE" in
"ERROR" )
log2syslog "$TYPE" "$TYPE $MSG"
;;
"DEBUG" )
if [ "$DEBUG" = "1" ]
then
if [ "$QUIET" = "0" ]
then
echo "$DATE" "$MSG"
fi
log2syslog "$TYPE" "$TYPE $MSG"
fi
;;
"INFO" )
if [ "$QUIET" = "0" ] && [ "$DEBUG" = "1" ]
then
echo "$DATE $MSG"
fi
log2syslog "$TYPE" "$TYPE $MSG"
;;
esac
}
log2mail () {
SUBJECT="$1"
BODY="$2"
DATE=`date +%b\ %d\ %H:%M:%S`
if [ ! -z "$MAIL_TARGET" ]
then
echo "$DATE - $BODY" | mail -s "$SUBJECT" "$MAIL_TARGET" &
fi
}
log2syslog () {
TYPE=`echo "$1" | awk '{print tolower($0)}'`
MSG="$2"
echo "$MSG" | logger -t "WFS" -p daemon."$TYPE"
}
init_wfs () {
if [ ! -e "$TARGETS_FILE" ]
then
log ERROR "Targets file $TARGETS_FILE does not exist."
exit 1
else
TARGETS=`cat "$TARGETS_FILE"`
TMPVAR=( $TARGETS )
NO_OF_TARGETS=${#TMPVAR[@]}
fi
if [ -z "$TARGETS" ]
then
log ERROR "No targets to test availability, targets file $TARGETS_FILE empty?."
exit 1
else
for x in $TARGETS
do
log DEBUG "Adding static route for host $x"
route add -host "$x" gw "$PRIMARY_GW" >> /dev/null 2>&1
done
fi
}
check_for_pid () {
if [ -e "$PIDFILE" ]
then
log ERROR "PID file $PIDFILE exists. Aborting."
exit 1
fi
}
route_del () {
route del default gw "$1"
}
display_header () {
log INFO "------------------------------"
log INFO " WAN Failover Script $VERSION"
log INFO "------------------------------"
log INFO " Primary gateway: $PRIMARY_GW"
log INFO " Secondary gateway: $SECONDARY_GW"
log INFO " Max latency in ms: $MAX_LATENCY"
log INFO " Threshold before failover: $THRESHOLD"
log INFO " Number of target hosts: $NO_OF_TARGETS"
log INFO " Tests per host: $TEST_COUNT"
log INFO "------------------------------"
}
#
# This route allows testing if the failed primary link
# Is available again, when in failover mode.
#
test_single_target () {
TARGET="$1"
log DEBUG "Test interval between hosts is $TEST_INTERVAL"
ping -W "$MAX_LATENCY" -c "$TEST_COUNT" "$TARGET" >> /dev/null 2>&1
if [ ! "$?" = "0" ]
then
log INFO "Host $TARGET UNREACHABLE"
if [ "$TARGETS_FAILED" -lt "$THRESHOLD" ]
then
((TARGETS_FAILED++))
fi
TEST_INTERVAL=1
else
if [ "$TARGETS_FAILED" -gt "0" ]
then
((TARGETS_FAILED--))
fi
log DEBUG "Host $TARGET OK"
if [ "$ACTIVE_CONNECTION" = "PRIMARY" ]
then
TEST_INTERVAL="$INTERVAL"
fi
fi
}
test_wan_status () {
for x in $TARGETS
do
test_single_target $x
if [ "$TARGETS_FAILED" -gt "0" ]
then
log INFO "Failed targets is $TARGETS_FAILED, threshold is $THRESHOLD."
fi
check_wan_status
sleep "$TEST_INTERVAL"
done
}
route_add () {
route add default gw "$1"
}
switch_to_primary () {
route_del "$SECONDARY_GW"
route_add "$PRIMARY_GW"
ACTIVE_CONNECTION="PRIMARY"
}
switch_to_secondary () {
route_del "$PRIMARY_GW"
route_add "$SECONDARY_GW"
ACTIVE_CONNECTION="SECONDARY"
}
check_wan_status () {
if [ "$TARGETS_FAILED" -ge "$THRESHOLD" ] && [ "$ACTIVE_CONNECTION" = "PRIMARY" ]
then
switch
elif [ "$ACTIVE_CONNECTION" = "SECONDARY" ]
then
if [ "$TARGETS_FAILED" = "0" ]
then
switch
fi
else
log INFO "WAN Link: $ACTIVE_CONNECTION"
fi
}
switch () {
if [ "$ACTIVE_CONNECTION" = "PRIMARY" ]
then
switch_to_secondary
if [ ! -z "$SECONDARY_CMD" ]
then
eval "$SECONDARY_CMD"
fi
sleep "5"
MSG="Primary WAN link failed. Switched to secondary link."
BODY=`route -n`
log2mail "$MSG" "$BODY"
log INFO "$MSG"
log DEBUG "Failover Cooldown started, sleeping for $COOLDOWNDELAY01 seconds."
sleep "$COOLDOWNDELAY01"
elif [ "$ACTIVE_CONNECTION" = "SECONDARY" ]
then
switch_to_primary
if [ ! -z "$PRIMARY_CMD" ]
then
eval "$PRIMARY_CMD"
fi
sleep "10"
MSG="Primary WAN link OK. Switched back to primary link."
BODY=`route -n`
log2mail "$MSG" "$BODY"
log INFO "$MSG"
log DEBUG "Failback Cooldown started, sleeping for $COOLDOWNDELAY02 seconds."
sleep "$COOLDOWNDELAY02"
fi
}
start_wfs () {
init_wfs
display_header
log INFO "Starting monitoring of WAN link."
while true
do
test_wan_status
done
}
stop_wfs () {
log INFO "Got SIGTERM, cleaning and exiting."
TARGETS=`cat "$TARGETS_FILE"`
kill `cat $PIDFILE`
rm $PIDFILE
if [ ! -z "$TARGETS" ]
then
for x in $TARGETS
do
log DEBUG "Removing static route for host $x"
route del -host "$x" gw "$PRIMARY_GW" >> /dev/null 2>&1
done
fi
exit 0
}
# cleanup works only if $DAEMON is 1
if [ "$DAEMON" = "1" ]
then
trap stop_wfs SIGINT SIGTERM
fi
if [ "$1" == "--stop" ]
then
stop_wfs
exit 0
fi
if [ "$DAEMON" = "0" ]
then
check_for_pid
start_wfs
else
check_for_pid
start_wfs &
echo "$!" > "$PIDFILE"
fi