-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_dnsping.sh
executable file
·88 lines (82 loc) · 2.35 KB
/
01_dnsping.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
#!/bin/bash
# different dirs on different hosts:
case $(hostname) in
merz-nimbus)
cd /home/thomas/Documents/Administration/dnspingtest_rrd."$(hostname)"/ || exit 1
;;
ubuntu-*)
cd ~/dev/dnspingtest_rrd."$(hostname)"/ || exit 1
;;
*)
echo "Please add '$(hostname)' in 'case' block in this script: $0."
exit 1
;;
esac
PING=dnsping
COUNT=4
DEADLINE=5
tcp=
dnsping_host() {
# Double quote to prevent globbing and word splitting. [SC2086]
# shellcheck disable=SC2086
output="$($PING $tcp -q -c $COUNT -w $DEADLINE -s "$1" nextwurz.mooo.com 2>&1)"
# notice $output is quoted to preserve newlines
temp=$(echo "$output"| awk '
BEGIN {pl=100; rtt=0.1}
/requests transmitted/ {
match($0, /([0-9]+)% lost/, matchstr)
pl=matchstr[1]
}
/^min/ {
# looking for something like "min=14.553 ms, avg=16.015 ms, max=17.675 ms, stddev=1.571 ms"
match($3, /avg=(.*)/, a)
rtt=a[1]
}
/Name or service not known/ {
# no output at all means network is probably down
pl=100
rtt=0.1
}
END {print pl ":" rtt}
'|cut -d"=" -f2)
RETURN_VALUE="$temp"
}
# -- MAIN --
if [ ! -f dnsresolvers.list ]; then
pwd
echo "dnsresolvers.list: file not found."
exit 2
fi
resolverlist="$(grep -v ^\# dnsresolvers.list)"
[ -z "$resolverlist" ] && exit 1
for resolver in $resolverlist; do
if echo "$resolver"|grep -q 'T'; then
resolver="$(echo "$resolver"|cut -d "-" -f1)"
tcp="-T"
fi
# create rrd-file from scratch if not existing:
if ! [ -f data/dnsping_"${resolver}".rrd ]; then
rrdtool create data/dnsping_"${resolver}".rrd \
--step 60 \
DS:pl:GAUGE:600:0:100 \
DS:rtt:GAUGE:600:0:10000000 \
RRA:AVERAGE:0.5:1:800 \
RRA:AVERAGE:0.5:6:800 \
RRA:AVERAGE:0.5:24:800 \
RRA:AVERAGE:0.5:288:800 \
RRA:MAX:0.5:1:800 \
RRA:MAX:0.5:6:800 \
RRA:MAX:0.5:24:800 \
RRA:MAX:0.5:288:800
fi
# do the dnsping:
dnsping_host "$resolver"
# and update rrd:
rrdtool update \
data/dnsping_"$resolver".rrd \
--template \
pl:rtt \
N:"$RETURN_VALUE"
# https://forum.syncthing.net/t/why-are-rrd-files-transferred-by-time-and-not-immediately/16391
touch data/dnsping_"$resolver".rrd
done