-
Notifications
You must be signed in to change notification settings - Fork 6
/
dnscheck.sh
executable file
·76 lines (62 loc) · 1.98 KB
/
dnscheck.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
#!/bin/sh
until (varnishtop -1 >/dev/null) ; do
echo "dnscheck.sh: Waiting for varnish to start"
sleep 1
done
get_backends()
{
BACKEND_LIST=$(varnishadm vcl.show $(varnishadm vcl.list | grep active | awk '{print $4}') | egrep '^[ \t]+\.host[ \t]*=' | grep -v nodnscheck | cut -d '"' -f 2 | cut -d: -f1 | sort | uniq )
> /tmp/backend.list
for backend in ${BACKEND_LIST}; do
if [[ $(getent ahostsv4 ${backend} | awk '{print $1}' |head -n 1) = $backend ]] 2>/dev/null ; then
echo "dnscheck.sh: WARNING: Backend appears to be an IP address, no need to watch its dns"
continue
fi
if ! getent hosts ${backend} >/dev/null; then
echo "dnscheck.sh: ERROR: ${backend} is not a valid address"
exit 1
fi
echo $backend >> /tmp/backend.list
touch "/tmp/lookup_${backend}.curr"
done
}
do_reload()
{
new_config="reload_$(date +%FT%H:%M:%S)"
if varnishadm vcl.load $new_config $VARNISH_CONFIG_FILE; then
[[ ! -z $DEBUG ]] && echo "varnishadm vcl.load succeded"
else
echo "varnishadm vcl.load failed"
exit 1
fi
if varnishadm vcl.use $new_config; then
[[ ! -z $DEBUG ]] && echo "varnishadm vcl.use succeded"
else
echo "varnishadm vcl.use failed"
exit 1
fi
}
while true; do
get_backends
sleep ${VARNISH_DNS_TTL:-17}
reload_needed=0
# Check DNS
for backend in $(cat /tmp/backend.list); do
getent ahostsv4 "${backend}" |awk '{print $1}' | head -n 1 > "/tmp/lookup_${backend}.new"
# Compare old vs new
cmp -s "/tmp/lookup_${backend}.new" "/tmp/lookup_${backend}.curr"
if [[ 1 -eq $? ]]; then
if [[ -s "/tmp/lookup_${backend}.curr" ]]; then
# DNS has changed!
echo "dnscheck.sh: DNS changed for ${backend}"
reload_needed=1
else
echo "dnscheck.sh: First check for ${backend} - skipping reload"
fi
mv "/tmp/lookup_${backend}.new" "/tmp/lookup_${backend}.curr"
fi
done
if [[ $reload_needed -ne 0 ]]; then
do_reload
fi
done