This repository has been archived by the owner on May 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
check_openshift_pv_avail
executable file
·337 lines (288 loc) · 8.98 KB
/
check_openshift_pv_avail
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
usage() {
echo "Usage: $0 -f <path> [-w <warn>] [-c <crit>]"
echo ' [-l [<class>,]<label>=<value> [-w <warn>] [-c <crit>] ...]'
echo ' [-s [<class>,]<capacity> [-w <warn>] [-c <crit>] ...]'
echo ' [-i [<class>]'
echo
echo 'Options:'
echo ' -f Config file path'
echo ' -w Exit with WARNING status if fewer physical volumes than the number'
echo ' given are available'
echo ' -c Exit with CRITICAL status if fewer physical volumes than the number'
echo ' given are available'
echo ' -l Select using label, value and optional storage class.'
echo ' -s Select using volume capacity and optional storage class.'
echo ' -i Ignore volumes of a storageclass.'
echo
echo 'Volumes selected using the "-l" or "-s" options are not considered for the'
echo 'generic or later selection-specific limits. The first selector matching a'
echo 'volume applies.'
echo
echo 'The "-l" and "-s" selectors support specifying a storage class name, i.e.'
echo '"-s bulk,5Gi". By default the limits following apply to all storage'
echo 'classes. Use the empty string (i.e. "-s ,1Gi") to select the default'
echo 'storage class'
echo
echo 'Examples:'
echo " $0 -w 10"
echo " Warn if fewer than 10 volumes are available"
echo " $0 -c 33 -l customer=foocorp -w 50"
echo ' Count volumes with the label "customer" set to "foocorp" separately and'
echo ' warn if less than 50 are available. Fail if less than 33 volumes without'
echo ' said label value are available.'
echo " $0 -w 10 -s 1Gi -w 3"
echo ' Warn if fewer than 3 volumes with a capacity of 1Gi are available or if'
echo ' fewer than 10 of other capacities are available.'
echo " $0 -l foo=bar -w 7 -s 100Gi"
echo ' Ignore volumes with a capacity of 100Gi (i.e. ignore them) unless they'
echo ' have a label "foo" with value "bar". A warning is emitted if fewer than'
echo ' 7 volumes with label "foo" and value "bar" are available.'
echo " $0 -l foo=bar -w 7 -i baz"
echo ' Ignore volumes with storageclass "baz". All volumes with storageclass'
echo ' "bazz" will be ignored, regardless of other selectors.'
}
opt_cfgfile=
opt_storageclass='*'
opt_label=
opt_capacity=
opt_warn=
opt_crit=
opt_ignore_storageclass=()
limits=()
default_limits=
push_limit() {
local values="${opt_warn};${opt_crit}"
if [[ -z "$opt_label" && -z "$opt_capacity" ]]; then
if [[ "$opt_storageclass" != '*' ]]; then
echo 'Storage class can not be given for default limits' >&2
exit 1
fi
default_limits="$values"
else
limits+=( "${opt_storageclass};${opt_label};${opt_capacity};${values}" )
fi
}
validate_key_value() {
if [[ "$1" != ?*=* ]]; then
echo "Label must be at least one character followed by an equal sign' \
'and a value: $1" >&2
exit "$state_critical"
fi
}
validate_capacity() {
local re='^[0-9]+(\.[0-9]+)?([MGTE]i?)?$'
if ! [[ "$1" =~ $re ]]; then
echo "Capacity must match regular expression \"${re}\": $1" >&2
exit "$state_critical"
fi
}
reset_limit() {
local varname="$1" value="$2" validatefn="$3"
local first second
opt_storageclass='*'
opt_label=
opt_capacity=
opt_warn=
opt_crit=
IFS=, read -r first second <<<"$value"
if [[ -n "$second" ]]; then
opt_storageclass="$first"
value="$second"
else
value="$first"
fi
if [[ -n "$validatefn" ]]; then
"$validatefn" "$value"
fi
eval "${varname}=\"\$value\""
}
while getopts 'hf:w:c:l:s:i:' opt; do
case "$opt" in
h)
usage
exit 0
;;
f) opt_cfgfile="$OPTARG" ;;
w)
validate_integer "$OPTARG"
opt_warn="$OPTARG"
;;
c)
validate_integer "$OPTARG"
opt_crit="$OPTARG"
;;
l)
push_limit
reset_limit opt_label "$OPTARG" validate_key_value
;;
s)
push_limit
reset_limit opt_capacity "$OPTARG" validate_capacity
;;
i)
opt_ignore_storageclass+=( "$OPTARG" )
;;
*)
usage >&2
exit 1
;;
esac
done
push_limit
shift $((OPTIND - 1))
if [[ "$#" -gt 0 ]]; then
usage >&2
exit 1
fi
if [[ -z "$opt_cfgfile" ]]; then
usage >&2
exit 1
fi
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
# Capture stderr in variable and redirect stdout to file
# shellcheck disable=SC2069
if ! msg=$(run_oc "$opt_cfgfile" get --output=json persistentvolumes 2>&1 >"$tmpdir/pv.json"); then
echo "$msg"
exit "$state_critical"
fi
exit_status=$state_ok
output=()
metrics=()
#
# Select volumes from a v1.PersistentVolumeList
# https://docs.openshift.com/enterprise/3.2/rest_api/kubernetes_v1.html#v1-persistentvolumelist
#
# Arguments:
# - storageclass: "*" to consider volumes of all storage classes, "" for the
# default storage class or an explicit class name
# - labelval: Empty string to select all volumes or "label=value" to select
# volumes with given label set to given value
# - capacity: Empty string to select all capacities or value to select volumes
# with given capacity
# - *args: Volume UIDs to exclude
#
filter_volumes() {
local storageclass="$1"; shift
local labelval="$1"; shift
local capacity="$1"; shift
local label=
local value=
local exclude_uid=()
for uid; do
# TODO: Implement proper value escaping
exclude_uid+=( "\"$uid\"" )
done
jqfilter='[
.items[] |
. as $cur |
select(
(
($storageclass == "*") or
($cur.spec.storageClassName // "") == $storageclass
) and (
(
($cur.metadata.annotations["pv.kubernetes.io/provisioned-by"] // null) or
(($exclude_uid | index($cur.metadata.uid)) != null)
) | not
)
)
'
if [[ -n "$labelval" ]]; then
IFS='=' read label value <<< "$labelval"
jqfilter+=" | select(.metadata.labels | .[\"$label\"] == \"$value\")"
fi
if [[ -n "$capacity" ]]; then
# FIXME: Don't use string comparison to compare quantities
jqfilter+=" | select(.spec.capacity.storage == \"$capacity\")"
fi
for class in ${opt_ignore_storageclass[@]+"${opt_ignore_storageclass[@]}"}; do
jqfilter+=" | select(.spec.storageClassName != \"$class\")"
done
jqfilter+=']'
jq --raw-output \
--arg storageclass "$storageclass" \
--argjson exclude_uid "[$(join_args ', ' ${exclude_uid[@]+"${exclude_uid[@]}"})]" \
"$jqfilter"
}
#
# Extract UIDs from a list of objects with v1.ObjectMeta
# https://docs.openshift.com/enterprise/3.2/rest_api/kubernetes_v1.html#v1-objectmeta
#
get_obj_uids() {
jq -r '.[] | .metadata.uid | @text'
}
#
# Count volumes (v1.PersistentVolume) grouped by capacity and storage class
#
calc_stats() {
jq -r '
group_by(.spec.storageClassName, .spec.capacity.storage) |
map({
storageclass: (.[0].spec.storageClassName // ""),
capacity: .[0].spec.capacity.storage,
total: length,
avail: [.[] | select(.status.phase == "Available")] | length,
}) |
sort_by(.storageclass, .capacity) |
.[] |
@sh "storageclass=\(.storageclass) capacity=\(.capacity) total=\(.total) avail=\(.avail)"
'
}
#
# Apply user-provided limits and calculate metrics
#
check() {
local labelval="$1" capacity="$2" warn="$3" crit="$4"
local status scmetric scoutput
calc_stats > "$tmpdir/stats.txt"
while read line; do
eval "local $line"
used=$(( total - avail ))
if [[ -z "$storageclass" ]]; then
scmetric=
scoutput=" without storage class"
else
scmetric="${storageclass}-"
scoutput=" in class \"${storageclass}\""
fi
# http://docs.icinga.org/latest/en/perfdata.html#perfdata-format
metrics+=(
"'${scmetric}${labelval:+${labelval//=/-}-}${capacity}'=${used};${warn};${crit};0;${total}"
)
if [[ -n "$crit" && "$avail" -lt "$crit" ]]; then
status=$state_critical
elif [[ -n "$warn" && "$avail" -lt "$warn" ]]; then
status=$state_warning
else
status=$state_ok
fi
if [[ "$status" != $state_ok ]]; then
output+=(
"${labelval:+[$labelval] }$avail of $total volumes with $capacity available${scoutput}"
)
fi
exit_status=$(merge_status "$exit_status" "$status")
done < "$tmpdir/stats.txt"
}
processed=()
for line in ${limits[@]+"${limits[@]}"}; do
IFS=';' read storageclass labelval capacity warn crit _ <<< "$line"
# Find matching disks
filter_volumes "$storageclass" "$labelval" "$capacity" ${processed[@]+"${processed[@]}"} \
< "$tmpdir/pv.json" > "$tmpdir/matching.json"
processed+=( $(get_obj_uids < "$tmpdir/matching.json") )
check "$labelval" "$capacity" "$warn" "$crit" < "$tmpdir/matching.json"
done
# Process remaining volumes, i.e. those not explicitly requested by label value
filter_volumes '*' '' '' ${processed[@]+"${processed[@]}"} \
< "$tmpdir/pv.json" > "$tmpdir/matching.json"
IFS=';' read warn crit _ <<< "$default_limits"
check '' '' "$warn" "$crit" < "$tmpdir/matching.json"
finish "$exit_status" \
"$(join_args ', ' ${output[@]+"${output[@]}"})" \
"${metrics[*]+${metrics[*]}}"
# vim: set sw=2 sts=2 et :