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_pvc_phase
executable file
·171 lines (137 loc) · 3.63 KB
/
check_openshift_pvc_phase
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
readonly default_maxpending=3600
usage() {
echo "Usage: $0 -f <path>"
echo
echo 'Options:'
echo ' -f Config file path'
echo ' -c Maximum number of seconds a claim may remain pending before' \
"going critical (default: ${default_maxpending} seconds)"
}
opt_cfgfile=
opt_maxpending="$default_maxpending"
while getopts 'hf:c:' opt; do
case "$opt" in
h)
usage
exit 0
;;
f) opt_cfgfile="$OPTARG" ;;
c)
validate_integer "$OPTARG"
opt_maxpending="$OPTARG"
;;
*)
usage >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ -z "$opt_cfgfile" || "$#" -gt 0 ]]; 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 pvc \
--output=json \
--all-namespaces=true \
2>&1 >"$tmpdir/data.json"); then
echo "$msg"
exit "$state_critical"
fi
jq -r '.items[] |
@sh " \\
name=\(.metadata.namespace + "/" + .metadata.name) \\
creation=\(.metadata.creationTimestamp // "") \\
phase=\(.status.phase) \\
volname=\(.spec.volumeName // "") \\
reqstorage=\(.spec.resources.requests.storage // "") \\
storageclass=\(.spec.storageClassName // "") \\
capacity=\(.status.capacity.storage // "") \\
"
' \
< "$tmpdir/data.json" > "$tmpdir/parsed.sh"
exit_status="$state_ok"
output=()
metrics=()
lost_claims=()
pending_claims=()
found_old=
now=$(date +%s)
declare -A phase_count=(
[bound]=0
[pending]=0
[lost]=0
)
while read line; do
eval "$line"
lc_phase="${phase,,*}"
let "++phase_count[$lc_phase]"
info=()
if [[ -n "$reqstorage" ]]; then
info+=( "req size: ${reqstorage}" )
fi
if [[ -n "$storageclass" ]]; then
info+=( "class: ${storageclass}" )
fi
if [[ -n "$capacity" ]]; then
info+=( "capacity: ${capacity}" )
fi
if [[ -n "$volname" ]]; then
info+=( "PV name: ${volname}" )
fi
if [[ -n "$creation" ]]; then
info+=( "created ${creation}" )
fi
if [[ -n "${info[*]+${info[*]}}" ]]; then
infostr="${name} ($(join_args ', ' "${info[@]}"))"
else
infostr="$name"
fi
# https://github.com/kubernetes/kubernetes/blob/v1.3.4/pkg/api/v1/types.go#L514
case "$lc_phase" in
bound) ;;
pending)
pending_claims+=( "$infostr" )
if [[ -n "$creation" && -z "$found_old" ]] && \
creation_ts=$(date -d "$creation" +%s) &&
(( (creation_ts + opt_maxpending) < now )); then
found_old=yes
fi
;;
lost)
lost_claims+=( "$infostr" )
;;
*)
output+=( "\"$name\" reports unknown phase \"$phase\"" )
exit_status=$(merge_status "$exit_status" "$state_warning")
;;
esac
done < "$tmpdir/parsed.sh"
if [[ -n "${pending_claims[*]+${pending_claims[*]}}" ]]; then
output+=( "pending: $(join_args ', ' "${pending_claims[@]}")" )
exit_status=$(merge_status "$exit_status" "$state_warning")
fi
if [[ -n "${lost_claims[*]+${lost_claims[*]}}" ]]; then
output+=( "lost: $(join_args ', ' "${lost_claims[@]}")" )
exit_status=$(merge_status "$exit_status" "$state_critical")
fi
if [[ -n "$found_old" ]]; then
exit_status="$state_critical"
fi
if [[ -n "${!phase_count[*]}" ]]; then
for phase in "${!phase_count[@]}"; do
# http://docs.icinga.org/latest/en/perfdata.html#perfdata-format
metrics+=( "'$phase'=${phase_count[$phase]};;;0" )
done
fi
finish "$exit_status" \
"$(join_args ', ' ${output[@]+"${output[@]}"})" \
"${metrics[*]+${metrics[*]}}"
# vim: set sw=2 sts=2 et :