forked from stfc/ral-ceph-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_cluster_connectivity.sh
225 lines (191 loc) · 5.33 KB
/
check_cluster_connectivity.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
#!/bin/bash
# give script a list of hosts and it will ssh to them, find their cluster IPs and they attempt to establish if the cluster connectivity is good
#set -e
TOTAL_PAD=2
FLUSH_ARP=${FLUSH_ARP:-0}
RATE_LIMIT=0.05 # seconds to wait between parallel calls
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
GREY=$(tput setaf 8)
NC=$(tput sgr0)
hn_len=1
hostname_arr=()
publicip_arr=()
clusterip_arr=()
results_dir="$(mktemp -d /tmp/check_cluster_connectivity.XXXXXXXXXX)"
find_ips () {
nice ssh -o "StrictHostKeyChecking=no" "$1" ip -o a 2>/dev/null | grep -o "1\(0\|30\).246.[0-9]\+.[0-9]\+/[0-9]\+" | cut -d / -f 1 | sort -r | xargs echo > "$results_dir/$1/ips"
echo -n '·'
}
flush_arp () {
echo "flushing arp cache on $1"
nice ssh -o "StrictHostKeyChecking=no" "$1" ip neigh flush all
echo -n '·'
}
check_ping () {
host="$1"
shift
network="$1"
shift
nice ssh -o "StrictHostKeyChecking=no" "$host" "fping $@" 2>/dev/null | grep -vF ' error ' 1>>"$results_dir/$host/$network"
echo $? > "$results_dir/$host/exit"
echo -n '.'
}
pass_fail () {
host="$1"
network="$2"
dest="$3"
if [[ $(cat "$results_dir/$host/exit") -le 1 ]]; then
state="$(awk "/^$dest is/ {print \$NF}" "$results_dir/$host/$network")"
if [[ "$state" == "alive" ]]; then
echo -ne "✓ "
elif [[ "$state" == "unreachable" ]]; then
echo -ne "✗ "
else
echo "?"
fi
else
echo -ne "!"
fi
}
pad_print () {
if [[ "$1" == "✓ " ]]; then
printf "${GREEN}%-${TOTAL_PAD}s${NC}" "$1"
elif [[ "$1" == "✗ " ]]; then
printf "${RED}%-${TOTAL_PAD}s${NC}" "$1"
elif [[ "$1" == "!" || "$1" == "?" ]]; then
printf "${YELLOW}%-${TOTAL_PAD}s${NC}" "$1"
elif [[ "$1" == '· ' ]]; then
printf "${GREY}%-${TOTAL_PAD}s${NC}" "$1"
else
printf "%-${TOTAL_PAD}s" "$1"
fi
}
print_header () {
echo
for _ in $(seq 1 "$(tput cols)"); do
echo -n "─"
done
echo -e "\r──┤ $1 ├"
}
# Sort list of hostnames using version sort
hosts="$(echo "$@" | xargs -n 1 echo | sort -V | uniq)"
print_header "Gathering IP addresses"
for host in $hosts; do
mkdir -p "$results_dir/$host"
find_ips "$host" &
sleep $RATE_LIMIT
done
wait
echo
if [[ $FLUSH_ARP -eq 1 ]]; then
print_header "Flushing ARP caches"
for host in $hosts; do
mkdir -p "$results_dir/$host"
flush_arp "$host" &
sleep $RATE_LIMIT
done
wait
echo
fi
print_header "Checking Consistency"
for host in $hosts; do
read -r publicip clusterip < "$results_dir/$host/ips"
if [[ -z "$publicip" ]]; then
pad_print "✗ "
printf "%-12s %s\n" "$host" "no public ip found, exiting"
exit 1
fi
if [[ -z "$clusterip" ]]; then
pad_print "✗ "
printf "%-12s %s\n" "$host" "no cluster ip found, exiting"
exit 1
else
pad_print "✓ "
printf "%-12s %-15s %-15s\n" "$host" "$publicip" "$clusterip"
hostname_arr+=( "$host" )
publicip_arr+=( "$publicip" )
clusterip_arr+=( "$clusterip" )
fi
# find length of hostnames for matrix formatting purposes
hostname_len=$(( ${#host} + 1))
if [[ $hostname_len -gt $hn_len ]]; then
hn_len=$hostname_len
fi
done
echo
pad_print "✓ "
echo "IPs found for all hosts"
# sanity check our arrays against input args and each other
if [[ ${#hostname_arr[@]} -ne $# ]] || [[ ${#hostname_arr[@]} -ne ${#publicip_arr[@]} ]] || [[ ${#clusterip_arr[@]} -ne ${#publicip_arr[@]} ]]
then
declare -p hostname_arr
declare -p publicip_arr
declare -p clusterip_arr
echo "wrong number of hosts, public or private addresses found, something went wrong, exiting"
exit 1
fi
print_header "Running Ping Tests"
printf "Estimated time: %.0f seconds\n" "$(echo "${#hostname_arr[@]} * $RATE_LIMIT * 2" | bc -l)"
for i in "${!hostname_arr[@]}"; do
host="${hostname_arr[i]}"
dest="${publicip_arr[@]}"
check_ping "$host" "public" "$dest" &
sleep $RATE_LIMIT
dest="${clusterip_arr[@]}"
check_ping "$host" "cluster" "$dest" &
sleep $RATE_LIMIT
done
wait
echo
pad_print '✓ '
echo 'Done'
print_header "Public"
# print column labels
for i in $(seq 0 $((hn_len - 1))); do
printf "%${hn_len}s"
for host in "${hostname_arr[@]}"; do
pad_print "${host:$i:1}"
done
echo
done
# print results of public pings
for i in "${!hostname_arr[@]}"; do
host="${hostname_arr[i]}"
printf "%-12s" "$host"
for j in "${!hostname_arr[@]}"; do
if [[ i -eq j ]]; then
pad_print '· '
continue
fi
dest="${publicip_arr[j]}"
pad_print "$(pass_fail "$host" "public" "$dest")"
done
echo
done
print_header "Cluster"
# print column labels
for i in $(seq 0 $((hn_len - 1))); do
printf "%${hn_len}s"
for host in "${hostname_arr[@]}"; do
pad_print "${host:$i:1}"
done
echo
done
# print results of cluster pings
for i in "${!hostname_arr[@]}"; do
host="${hostname_arr[i]}"
printf "%-12s" "$host"
for j in "${!hostname_arr[@]}"; do
if [[ i -eq j ]]; then
pad_print '· '
continue
fi
dest="${clusterip_arr[j]}"
pad_print "$(pass_fail "$host" "cluster" "$dest")"
done
echo
done
echo
rm -rf "$results_dir"