-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpihole-ha.sh
374 lines (313 loc) · 12.1 KB
/
pihole-ha.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/bin/bash
# This script is designed to be executed via cron on standby pihole instance
# User configurable options
# ----------------------------
# Configuration more likely to change is located in "pihole-ha-conf.sh" file that contains
# user credentials for notification services as well as details of 'master' PiHole instance
# Most configuration here would not need to be changed and are more 'hacky' waiting
# for more smarter / dynamic ways to implement what they state.
# lifetime of DHCP leases when issued by 'backup' pihole instance
# number specified here is in hours. Recommend to keep shorter to allow for faster failing back to 2 DNS servers
dhcp_lifetime=1
# Location of pihole executable
# specified as originally hacking issues executing pihole commands like 'restart'
# could likely be improved but for time being it works!
pihole_app=/usr/local/bin/pihole
# folder where we store files
#dir=/home/pihole-gs # defaults to where script is running
#logfile=$dir/piholeha.log # log output managed in cron and logrotate.d
#logfileerr=/var/log/piholeha-err.log #future use
#logfilestd/var/log/piholeha.log #future use
# configuration file to import environment-specific settings.
# possibly for future use to allow changing from CLI?
configimport=pihole-ha-conf.sh
# servername to use in logs for current (backup) instance
# by default use hostname
servername=$(hostname)
# Start of script! Nothing to see down here...
# ------------------------------
# debug flag. if unset, debug is not active. changed by command line option
piholeha_debug=
# set debug mode if requested
if [ "$1" = "--debug" ] || [ "$2" = "--debug" ] ; then
piholeha_debug=true
fi
# finds directory of the script
dir="${BASH_SOURCE%/*}"
if [[ ! -d "$dir" ]]; then dir="$PWD"; fi
# import required functions
. "$dir/pihole-ha-func.sh"
# capture request for help/info
if [ "$1" == "--help" ] || [ "$1" == "-h" ] || [ "$1" == "/?" ] ; then
helpoptions
fi
# checks if config file exists and if not creates a 'template'
configfile="${dir}/${configimport}"
if [ ! -z "$piholeha_debug" ] ; then echo "Using config file ${configfile}" ; fi
if [ ! -e ${configfile} ]
then
BuildDefaultConfig
echo "Template configuration has been created at ${configfile}"
echo "This script will not run until this has been updated"
exit 1
fi
# imports the required dependent conf script (Secrets, IPs, etc)
. "$dir/pihole-ha-conf.sh"
if [ "$sampleconfig" == "true" ]
then
echo "Detected sample/template configuration. ${configfile}"
echo "Please make any required changes to the configuration and try again."
exit 1
fi
# perform config file sanity checks
ConfigFileCheck
#heading
echo ""
echo "======================================"
echo Pi-hole HA sync update for ${servername}
date
echo "--------------------------------------"
# check local status
# -------------------------------------------------------------
echo ""
echo "Performing self-test to confirm health..."
selftest
if [ ! -z "$piholeha_debug" ] ; then echo "Check Result=${selfcheck}" ; fi
echo "...Done!"
case "$selfcheck" in
# unable to validate state
"INVALID")
if [ -e ${dir}/localAPI.err ]
then
echo "...Local API error file present. Exiting!"
exit_success
else
echo "...Local API in error state."
echo ""
echo "Running additional check ..."
sleep 2
selftest
# while not technically accuate, for simplicity even if it isn't the same error on the second test it will flag as the first error
if [ -z "$selfcheck" ] || [ "$selfcheck"=="INVALID" ]
then
echo "...local API still in error mode."
echo ""
echo "Creating lock file for local error state..."
touch ${dir}/localAPI.err
echo ""
# send notification
notificationmessage="Internet redundancy failure for Pi-hole (standby API)"
sendnotification
exit_success
else
echo ""
echo "Second check. Local API up and running."
fi
fi
;;
# in error state
"BAD")
if [ -e ${dir}/FTL.err ]
then
echo "...FTL error file present. Exiting!"
exit_success
else
echo "...Local FTL in error state."
echo ""
echo "Running additional check ..."
sleep 2
selftest
# while not technically accuate, for simplicity even if it isn't the same error on the second test it will flag as the first error
if [ -z "$selfcheck" ] || [ "$selfcheck"=="INVALID" ]
then
echo "...FTL still in error mode."
echo ""
echo "Creating lock file for local error state..."
touch ${dir}/FTL.err
echo ""
# send notification
notificationmessage="Internet redundancy failure for Pi-hole (standby FTL)"
sendnotification
exit_success
else
echo ""
echo "Second check. FTL up and running."
fi
fi
;;
# functioning as expected
# this should in theory only see "disabled" or "enabled". Either option indicate serving DNS (and in theory DHCP)
*)
localnotificationcheck=
# clear previous error if FTL
if [ -e ${dir}/FTL.err ]
then
echo "Clearing local error lock file (FTL)..."
rm -f ${dir}/FTL.err
echo "...Done!"
localnotificationcheck=true
else
# if in debug, explicit notification
if [ ! -z "$piholeha_debug" ] ; then echo "Local FTL flag file doesn't exist. No action performed." ; fi
fi
# clear previous error if API
if [ -e ${dir}/localAPI.err ]
then
echo "Clearing local error lock file (API)..."
rm -f ${dir}/localAPI.err
echo "...Done!"
localnotificationcheck=true
else
# if in debug, explicit notification
if [ ! -z "$piholeha_debug" ] ; then echo "Local API flag file doesn't exist. No action performed." ; fi
fi
# if either or both errors cleared, send a notification
if [ ! -z $localnotificationcheck ]
then
# send notification
notificationmessage='Internet redundancy resolved for Pi-hole'
sendnotification
fi
;;
esac
# -------------------------------------------------------------
# check partner status - ping
# -------------------------------------------------------------
echo ""
echo "Performing ping test to remote Pihole instance at ${targetname}..."
partnerping
if [ ! -z "$piholeha_debug" ] ; then echo "Check Result=${count}" ; fi
echo "...Done!"
# detect if number of returned pings are less than what was requested
if [ $count -lt $countping ]
then
# provide feedback regarding the number of pings lost
if [ $count -eq 0 ]
then
echo "${targetname} is not pinging!"
else
echo "${targetname} is not reliable! $count of $countping pings replied"
fi
echo ""
echo "Running additional check ..."
sleep 2
partnerping
if [ ! -z "$piholeha_debug" ] ; then echo "Check Result=${count}" ; fi
echo "...Done!"
if [ $count -lt $expectping ]
then
echo "${targetname} is not reliable! $count of $countping pings replied"
# determine if already in failover state or if need to set flag and fail over
if [ -e ${dir}/dhcp.on ]
then
echo ""
echo "DHCP server already enabled. No changes or notifications performed."
exit_success
else
# activate DHCP server
dhcp_enable
# create flag file to inform subsequent runs already failed over
flag_dhcpon
# send notification
notificationmessage='Internet failover (primary connectivity ${count} of ${countping})'
sendnotification
exit_success
fi
else
# if in debug mode explicitly notify
if [ ! -z "$piholeha_debug" ] ; then echo "${targetname} is responding to pings" ; fi
# we do not clear any error states here until passes application tests
fi
else
# if in debug mode explicitly notify
if [ ! -z "$piholeha_debug" ] ; then echo "${targetname} is responding to pings" ; fi
# we do not clear any error states here until passes application tests
fi
# -------------------------------------------------------------
# check partner status - app status
# -------------------------------------------------------------
echo ""
echo "Performing application tests to remote Pi-hole instance at ${targetname}..."
partneractivetest
if [ ! -z "$piholeha_debug" ] ; then echo "Check Result=${partnercheck}" ; fi
echo "...Done!"
# detect if check failed
case "$partnercheck" in
# unable to validate state
"INVALID")
# if in debug mode explicitly notify
if [ ! -z "$piholeha_debug" ] ; then echo "${targetname} status is unknown (API error)" ; fi
echo "Unable to determine state of primary instance due to possible invalid API key"
if [ -e ${dir}/primaryAPI.err ]
then
if [ ! -z "$piholeha_debug" ] ; then echo "Remote API error file flag already set, no changes." ; fi
exit_success
else
echo ""
echo "Generating lock file for remote API state"
touch ${dir}/primaryAPI.err
echo "...Done!"
# send notification
notificationmessage='Internet redundancy for Pi-hole (primary API)'
sendnotification
exit_success
fi
;;
# returning bad or no state
"BAD")
# if in debug mode explicitly notify
if [ ! -z "$piholeha_debug" ] ; then echo "${targetname} is NOT operational" ; fi
if [ -e ${dir}/dhcp.on ]
then
echo "DHCP server already enabled. No changes or notifications performed."
exit_success
else
echo "${targetname} is not in a good state!"
# create flag file to inform subsequent runs already failed over
flag_dhcpon
# activate DHCP server
dhcp_enable
# send notification
notificationmessage='Internet failover for Pi-hole has started'
sendnotification
fi
;;
# functioning as expected
# this should in theory only see "disabled" or "enabled". Either option indicate serving DNS (and in theory DHCP)
*)
# if in debug mode explicitly notify
if [ ! -z "$piholeha_debug" ] ; then echo "${targetname} is operational" ; fi
# clear APIerror flag if set
if [ -e ${dir}/primaryAPI.err ]
then
echo "Clearing error lock file (primary API)..."
rm -f ${dir}/primaryAPI.err
echo "...Done!"
# send notification
notificationmessage='Internet redundancy for Pi-hole resolved (primary API)'
sendnotification
fi
if [ -e ${dir}/dhcp.on ]
then
echo "${targetname} is Alive!"
# deactivate DHCP server
dhcp_disable
# clear dhcp active flag
flag_dhcpoff
# send notification
notificationmessage='Internet for Pi-hole has returned to normal'
sendnotification
else
# if in debug, explicit notification
if [ ! -z "$piholeha_debug" ] ; then echo "DHCP flag file doesn't exist. No action performed." ; fi
fi
echo ""
echo "Sync from primary Pi-hole instance ${targetname}..."
# pull current leases off primary pihole
dhcp_backupconf
echo "...Done!"
;;
esac
# -------------------------------------------------------------
# if script didn't bomb out, exit nicely and poll healthcheck.io instance to confirm successful run
exit_success