forked from RobertKrawitz/OpenShift4-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clusterbuster-connstat
executable file
·359 lines (332 loc) · 10 KB
/
clusterbuster-connstat
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
#!/bin/bash
declare jq_cmd
read -r -d '' jq_cmd <<\EOF
[foreach .items[]? as $item
([[],[]];0;
(if ($item.kind == "Pod") then
([foreach $item.spec.containers[]? as $container ([[],[]];0; [$item.metadata.namespace, $item.metadata.name, $container.name,
(if ($item.status.containerStatuses? == null) then
($item.status.phase + ": " + $item.status.conditions[0].reason?)
else
[foreach $item.status.containerStatuses[]? as $status ([[],[]]; 0; if ($container.name == $status.name) then $status.state | keys[0] else null end)] end)] | flatten | map (select (. != null)) | join(" "))])
else null end))] | flatten | map (select (. != null))[]
EOF
declare -i parallel=1
declare -i summarize=0
declare -i timeout=-1
declare -i verbose=0
declare basename=clusterbuster
declare -A expected_rows=()
declare -A cached_stats=()
declare statsfile=
declare OC
OC=$(type -p oc)
OC=${OC:-$(type -p kubectl)} # kubectl might not work, though...
while getopts ':p:B:st:f:vq' opt ; do
case "$opt" in
B) basename=$OPTARG ;;
p) parallel=$OPTARG ;;
t) timeout=$OPTARG ;;
s) summarize=1 ;;
f) statsfile=$OPTARG ;;
v) verbose=$((verbose+1)) ;;
q) verbose=0 ;;
*) ;;
esac
done
(( parallel <= 0 )) && parallel=1
trap 'wait; exit' INT EXIT
function prettyprint() {
local -a colwidths
local -a rows
local -a rowfields
local -i i
local f
local -a rowzero=(+ NAMESPACE POD + CONTAINER STATE REF FAIL BLOCKS + CREATED START + CONNECTED DSTART FINISH TRANSFERTIME USER SYSTEM DATA XFERTIME RATE SLEEPMEAN SLEEPSTDEV)
local -a formats=(.0s s s .0s s s d d d .0s .3f .3f .0s .3f .3f .3f .6f .3f .3f d .3f .3f .6f .6f)
local -ri nfields=$((${#rowzero[@]} - 1))
for i in $(seq 0 "$nfields") ; do
f=${rowzero[$i]}
f=${f%+}
colwidths[$i]=${#f}
done
readarray -t rows
for row in "${rows[@]}" ; do
# shellcheck disable=SC2206
rowfields=($row)
for i in $(seq 0 $((${#rowfields[@]} - 1)) ) ; do
if (( ${colwidths[$i]} > 0 && ${#rowfields[$i]} > ${colwidths[$i]} )) ; then
colwidths[$i]=${#rowfields[$i]}
fi
done
done
local printfstr0=
local printfstr1=
local -i width
for i in $(seq 0 "$nfields") ; do
f=${rowzero[$i]}
width=${colwidths[$i]}
fmt=${formats[$i]}
if [[ -n $printfstr0 && $printfstr0 != *'.0s' ]] ; then
printfstr0+=' '
printfstr1+=' '
fi
printfstr0+="%-${width}.${width}s"
if [[ $fmt = *'s' ]] ; then
printfstr1+="%-${width}$fmt"
else
printfstr1+="%${width}$fmt"
fi
done
printfstr0+='\n'
printfstr1+='\n'
# shellcheck disable=SC2059
printf "$printfstr0" "${rowzero[@]}"
for row in "${rows[@]}" ; do
# shellcheck disable=SC2059
# shellcheck disable=SC2086
printf "$printfstr1" $row
done
}
function process_summary() {
local -i line_count=0
local -i total=0
local -i first_start=0
local -i last_end=0
local -i total_et=0
local -i total_cpu=0
local -i total_cpu_util=0
local -i total_iterations=0
local -i pod_start=0
local -i pod_first_start=0
local -i pod_last_start=0
local -i read_ops=0
local -i write_ops=0
local -i fsync_ops=0
local -i readrate=0
local -i writerate=0
local -a output
local mode=
while read -r -a output ; do
[[ -n "${output[*]}" ]] || continue
if [[ -z "$mode" ]] ; then
case "${output[2]}" in
*'soaker'*) mode=soaker ;;
*'client'*) mode=server ;;
*'sysbench'*) mode=sysbench ;;
*) ;;
esac
fi
local -i start
local -i end
local -i et
if (( verbose )) ; then
echo "${output[*]}"
fi
case "$mode" in
server)
total+=${output[19]}
start=$(clean "${output[14]}")
end=$(clean "${output[15]}")
et=$(clean "${output[20]}")
if (( start < first_start || first_start == 0 )) ; then first_start=start; fi
if (( end > last_end || last_end == 0 )) ; then last_end=end; fi
;;
soaker)
pod_start=$(($(clean "${output[12]}") - $(clean "${output[11]}")))
if (( pod_start < pod_first_start || pod_first_start == 0 )) ; then pod_first_start=pod_start; fi
if (( pod_start > pod_last_start )) ; then pod_last_start=pod_start; fi
start=$(clean "${output[13]}")
end=$(clean "${output[15]}")
et=$(clean "${output[14]}")
if (( start < first_start || first_start == 0 )) ; then first_start=start; fi
if (( end > last_end || last_end == 0 )) ; then last_end=end; fi
total_cpu+=$(clean "${output[16]}")
total_cpu_util+=$(clean "${output[17]}")
total_iterations+=$(clean "${output[18]}")
;;
sysbench)
start=$(clean "${output[11]}")
end=$(clean "${output[13]}")
et=$(clean "${output[19]}")
if (( start < first_start || first_start == 0 )) ; then first_start=start; fi
if (( end > last_end || last_end == 0 )) ; then last_end=end; fi
read_ops+=$(clean "${output[14]}")
write_ops+=$(clean "${output[15]}")
fsync_ops+=$(clean "${output[16]}")
readrate+=${output[17]}
writerate+=${output[18]}
;;
esac
line_count+=1
total_et+=$et
done
(( line_count )) || return
case "$mode" in
server)
echo "Bytes: $total"
echo "Containers: $line_count"
echo "Average_elapsed: $(bc <<< "scale=3; $total_et/$line_count/1000")"
echo "Elapsed_span: $(bc <<< "scale=3; ($last_end-$first_start)/1000")"
echo "Data rate: $((total*2*1000/(total_et/line_count)/1024/1024))"
;;
soaker)
echo "Iterations: $total_iterations"
echo "Average_elapsed: $(bc <<< "scale=3; $total_et/$line_count/1000")"
echo "Elapsed_span: $(bc <<< "scale=3; ($last_end-$first_start)/1000")"
echo "First start: $(bc <<< "scale=3; ($pod_first_start)/1000")"
echo "Starting span: $(bc <<< "scale=3; ($pod_last_start-$pod_first_start)/1000")"
echo "Total CPU: $(bc <<< "scale=3; $total_cpu/1000")"
echo "CPU utilization: $(bc <<< "scale=3; $total_cpu_util/1000")%"
if (( total_cpu == 0 )) ; then
echo "Iterations/CPU sec: 0"
else
echo "Iterations/CPU sec: $(bc <<< "scale=0; $total_iterations*1000/$total_cpu")"
fi
if (( total_et == 0 )) ; then
echo "Iterations/sec: 0"
else
echo "Iterations/sec: $(bc <<< "scale=0; $total_iterations*1000*$line_count/$total_et")"
fi
;;
sysbench)
echo "Read ops/sec: $(bc <<< "scale=3; $read_ops / $total_et * $line_count * 1000")"
echo "Write ops/sec: $(bc <<< "scale=3; $write_ops / $total_et * $line_count * 1000")"
echo "Fsync ops/sec: $(bc <<< "scale=3; $fsync_ops / $total_et * $line_count * 1000")"
echo "Read rate MB/sec: $(bc <<< "scale=3; $readrate / $total_et * $line_count")"
echo "Write rate MB/sec: $(bc <<< "scale=3; $writerate / $total_et * $line_count")"
;;
*)
echo "Unknown mode $mode"
;;
esac
}
function retrieve_pod_stats() {
local ns=$1
local pod=$2
local container=$3
local status=$4
local -i connection_failures=0
local -i connection_refused=0
local -i blocks=0
local statline=''
local -a statlines=()
local row_prefix='!'
local pname="$ns $pod $container"
case "$status" in
running|completed|terminated)
# This is actually faster than reading lines if there's a lot of data.
local data
data="$(oc logs -n "$ns" "$pod" -c "$container")"
connection_refused=$(grep -c 'Connection refused' <<< "$data")
connection_failures=$(grep -c 'Could not connect' <<< "$data")
blocks=$(grep -c 'Write/Read' <<< "$data")
readarray -t statlines <<< "$(grep -E '^STATS' <<< "$data")"
if [[ $data = *"FINIS"* ]] ; then
row_prefix=
fi
;;
'')
return
;;
*)
;;
esac
if (( ${#statlines[@]} )) ; then
local statline=
for statline in "${statlines[@]}" ; do
echo "${row_prefix}-n $ns $pod -c $container $status $connection_failures $connection_refused $blocks $statline"
done
else
echo "!-n $ns $pod -c $container $status $connection_failures $connection_refused $blocks"
fi
}
declare wait_n=$(( BASH_VERSINFO[0] > 4 || (BASH_VERSINFO[0] > 4 && BASH_VERSINFO[1] >= 3) ))
function get_all_pods() {
local basename=$1
local ns
while read -r ns ; do
ns=${ns##*/}
oc get pods -ojson -l "${basename}-client" -n "$ns"
done <<< "$(oc get ns -l "$basename" --no-headers | awk '{print $1}')"
}
trap exit INT
function process_stats() {
local -i curjobs=0
local ns
local pod
local container
local status
while read -r ns pod container status ; do
local xcache=${cached_stats["$ns $pod $container"]:-}
if [[ -n "$xcache" ]] ; then
echo "$xcache"
continue
fi
if (( parallel > 1 )) ; then
if (( wait_n )) ; then
while (( curjobs >= parallel )) ; do
wait -n
curjobs=$((curjobs - 1))
done
elif (( curjobs >= parallel )) ; then
wait
curjobs=0
fi
retrieve_pod_stats "$ns" "$pod" "$container" "$status"&
curjobs=$((curjobs + 1))
else
retrieve_pod_stats "$ns" "$pod" "$container" "$status"
fi
done
if (( curjobs )) ; then
wait
fi
}
function get_stats() {
process_stats <<< "$(get_all_pods "$basename" | jq -r "$jq_cmd")"
}
function clean() {
local num=${1//./}
while [[ $num = "0"* ]] ; do
num=${num##0}
done
echo "${num:-0}"
}
function do_summarize() {
# shellcheck disable=SC2155
local -i start_time=$(printf '%(%s)T' -1)
local pass=1
while : ; do
local -i found_all=1
while read -r -a output ; do
[[ -n "${output[*]}" ]] || continue
local item="${output[1]} ${output[2]} ${output[4]}"
if [[ -z "${output[10]:-}" || ${output[0]} = '!'* ]] ; then
found_all=0
else
[[ -n "${cached_stats[$item]:-}" ]] || cached_stats[$item]=
cached_stats[$item]+="${output[*]}"$'\n'
unset pod_cache["$item"]
fi
done <<< "$(get_stats)"
if ((found_all)) ; then
echo "$(IFS=$'\n'; echo "${cached_stats[*]}")"
return
fi
pass=$((pass+1))
if (( timeout > 0 && $(printf '%(%s)T' -1) - start_time > timeout )) ; then
echo "Summary timed out ($timeout seconds)" 1>&2
return 1
fi
done
}
if (( summarize )) ; then
case "$statsfile" in
-) process_summary ;;
'') do_summarize | process_summary ;;
*) process_summary < "$statsfile" ;;
esac
else
get_stats |sed -e 's/^!//'
fi