-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSLA.sh
199 lines (178 loc) · 7.17 KB
/
SLA.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
#!/bin/bash
################################################################################
# SLA CHECK@SBTAP-AS59715 | Thanks to @mphilosopher for help and inspiration. #
# #
# ./SLA.sh [amount of ICMP packets to send] [round trip delay threshold value] #
# [packet loss value] #
# Example: sudo ./SLA.sh 1000 90 0.03 #
# It means: send 1k ICMP and ICMPv6 packets to the two defined anchor hosts, #
# do the math and check the SLA assuming that the average RTD must be less #
# than 90ms and that the average PL must be equal to or less than 0.03%. #
# ICMP packets value must be between 1 and 100k. #
################################################################################
# list IPs of the anchor hosts: RIPE ANCHOR at MIX (217.29.76.27) and RIPE
# ANCHOR at NAMEX (193.201.40.210)
# Feel free to edit or add hosts in the next two lines following the used syntax
AHv4=( "193.201.40.210" "217.29.76.27" );
AHv6=( "2001:7f8:10:f00c::210" "2001:1ac0:0:200:0:a5d1:6004:27" );
################################################################################
# NO NEED TO CHANGE CODE BELOW THIS LINE #
################################################################################
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m'
# let's check if needed commands are available
if ! [ -x "$(command -v bc)" ]; then
echo 'bc is needed to do the math' >&2;
exit 1;
fi
if ! [ -x "$(command -v ping)" ]; then
echo 'ping is needed to send ICMP' >&2;
exit 1;
fi
if ! [ -x "$(command -v ping6)" ]; then
echo 'ping6 is needed to send ICMPv6' >&2;
exit 1;
fi
# be sure that commands are executed as root
if [ "$(whoami)" != "root" ] && [ "$(id -un)" != "root" ]; then
echo 'You must be root, use sudo.' >&2;
exit 1;
fi
# check arguments
if ! [ $# -ge 3 ]; then
echo "Usage: ./SLA.sh [amount of ICMP packets to send] [round trip delay"
echo "threshold value] [packet loss value]" >&2;
echo "Example: sudo ./SLA.sh 1000 90 0.03" >&2;
echo "It means: send 1k ICMP and ICMPv6 packets to the two defined anchor"
echo "hosts, do the math and check the" >&2;
echo "SLA assuming that the average RTD must be less than 90ms and that"
echo "the average PL must be equal to or less than 0.03%." >&2;
echo "Recommended values are 10000 100 0.02. But be patient: probe can"
echo "take more than one hour." >&2;
exit 1;
else
PP=$1;
RTD=$2;
PL=$3;
fi
# Let's check if PL value is a correct percentage
if
[[ "$PL" =~ ^[0-9]+$ && "$PP" -ge 1 && "$PP" -le 100 && "$PL" -ge 0 && "$PL" \
-le 100 ]] || (( "$PP" >= 101 && "$PP" <= 1000 && (( $(echo "$PL >= 0.1" \
| bc -l) )) && (( $(echo "$PL <= 100" | bc -l) )) )) || (( "$PP" >= 1001 \
&& "$PP" <= 10000 && (( $(echo "$PL >= 0.01" | bc -l) )) && (( $(echo "$PL \
<= 100" | bc -l) )) )) || (( "$PP" >= 10001 && "$PP" <= 100000 && (( $(echo \
"$PL >= 0.001" | bc -l) )) && (( $(echo "$PL <= 100" | bc -l) )) )); then
# here we go
RTD_v4_SUM=0;
PL_v4_SUM=0.00;
RTD_v6_SUM=0;
PL_v6_SUM=0.00;
echo "Let's start with IPv4 SLA check" >&2;
AH4=${#AHv4[@]}
AH6=${#AHv6[@]}
# is there any IPv4 connectivity?;
echo '##################################' >&2;
echo 'Is there any IPv4 connectivity here?' >&2;
echo 'Trying to reach RIPE anchor at MIX' >&2;
echo '##################################' >&2;
((count = 3))
while [[ $count -ne 0 ]]; do
ping -qc 1 217.29.76.27
rc=$?
if [[ $rc -eq 0 ]]; then
((count = 1))
fi
((count = count - 1))
done
if [[ $rc -eq 0 ]]; then
echo '##################################' >&2;
echo "Good, there's IPv4 connectivity available" >&2;
echo "Let's go ahead with IPv4 SLA check" >&2;
echo '##################################' >&2;
# v4 probes
for i in "${AHv4[@]}"; do
ping -W 200 -i 0.11 -s 56 -c "$PP" "${i}">>PING_RESULTS_v4.txt;
RTD_v4_VALUE=$(tail -n1 PING_RESULTS_v4.txt|cut -d'/' -f5|cut -d. -f1);
PL_v4_VALUE=$(grep loss PING_RESULTS_v4.txt|cut -d, -f3|cut -d% -f1| \
cut -d' ' -f2);
echo "RTD ${RTD_v4_VALUE} on ${i}" >&2;
echo "PL ${PL_v4_VALUE}% on ${i}" >&2;
rm -f PING_RESULTS_v4.txt;
echo '##################################' >&2;
RTD_v4_SUM=$(( RTD_v4_SUM + RTD_v4_VALUE ));
PL_v4_SUM=$(echo $PL_v4_SUM + "$PL_v4_VALUE" | bc );
done
# do the math
(( RTD_v4_AVG="${RTD_v4_SUM} / $AH4" ))
PL_v4_AVG=`echo "scale=2;$PL_v4_SUM/$AH4"|bc -l`
# verify if we meet our v4 SLA
if (( RTD_v4_AVG > RTD )) || (( $(echo "$PL_v4_AVG > $PL" |bc -l) )); \
then
echo -e "${RED}==========> v4 SLA KO <==========${NC}" >&2;
else
echo -e "${GREEN}==========> v4 SLA OK <==========${NC}" >&2;
fi
else
echo '##################################' >&2;
echo 'NO IPv4 connectivity available' >&2;
echo '##################################' >&2;
fi
else
echo "During ping, packets can get lost. Here we count the percentage of"
echo "the loss." >&2;
echo "So, for ICMP between 1 and 100, PL value must be between 0 and 100"
echo "For ICMP between 101 and 1000, PL value must be between 0.1 and 100"
echo "For ICMP between 1001 and 10000, PL value must be between 0.01 and"
echo "100" >&2;
echo "For ICMP between 10001 and 100000, PL value must be between 0.001"
echo "and 100" >&2;
exit 1;
fi
# is there any IPv6 connectivity?;
echo '##################################' >&2;
echo 'Is there any IPv6 connectivity here?' >&2;
echo 'Trying to reach RIPE anchor at MIX' >&2;
echo '##################################' >&2;
((count = 3))
while [[ $count -ne 0 ]]; do
ping6 -oqc 1 2001:1ac0:0:200:0:a5d1:6004:27
rc=$?
if [[ $rc -eq 0 ]]; then
((count = 1))
fi
((count = count - 1))
done
if [[ $rc -eq 0 ]]; then
echo '##################################' >&2;
echo "Good, there's IPv6 connectivity available" >&2;
echo "Let's go ahead with IPv6 SLA check" >&2;
echo '##################################' >&2;
# v6 probes
for i in "${AHv6[@]}"; do
ping6 -i 0.11 -s 56 -c "$PP" "${i}">>PING_RESULTS_v6.txt;
RTD_v6_VALUE=$(tail -n1 PING_RESULTS_v6.txt|cut -d'/' -f5|cut -d. -f1);
PL_v6_VALUE=$(grep loss PING_RESULTS_v6.txt|cut -d, -f3|cut -d% -f1 \
|cut -d' ' -f2);
echo "RTD ${RTD_v6_VALUE} on ${i}" >&2;
echo "PL ${PL_v6_VALUE}% on ${i}" >&2;
rm -f PING_RESULTS_v6.txt;
echo '##################################' >&2;
RTD_v6_SUM=$(( RTD_v6_SUM + RTD_v6_VALUE ));
PL_v6_SUM=$( echo $PL_v6_SUM + "$PL_v6_VALUE" | bc );
done
# do the math
(( RTD_v6_AVG="${RTD_v6_SUM} / ${#AHv6[@]}" ));
PL_v6_AVG=`echo "scale=2;$PL_v6_SUM/$AH6"|bc -l`
# verify if we meet our v6 SLA
if (( RTD_v6_AVG > RTD )) || (( $(echo "$PL_v6_AVG > $PL" |bc -l) )); then
echo -e "${RED}==========> v6 SLA KO <==========${NC}" >&2;
else
echo -e "${GREEN}==========> v6 SLA OK <==========${NC}" >&2;
fi
else
echo '##################################' >&2;
echo 'NO IPv6 connectivity available' >&2;
echo '##################################' >&2;
fi