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_node
executable file
·101 lines (81 loc) · 1.97 KB
/
check_openshift_node
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
usage() {
echo "Usage: $0 -f <path> -n <node>"
echo
echo 'Check node conditions. Checked are:'
echo ' - Ready'
echo ' - DiskPressure'
echo ' - MemoryPressure'
echo ' - OutOfDisk'
echo
echo 'Options:'
echo ' -f Config file path'
echo ' -n Node name'
}
opt_cfgfile=
opt_name=
while getopts 'hf:n:' opt; do
case "$opt" in
h)
usage
exit 0
;;
f) opt_cfgfile="$OPTARG" ;;
n) opt_name="$OPTARG" ;;
*)
usage >&2
exit 1
;;
esac
done
shift $((OPTIND - 1))
if [[ "$#" -gt 0 ]]; then
usage >&2
exit 1
fi
if [[ -z "$opt_cfgfile" || -z "$opt_name" ]]; 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 node "$opt_name" 2>&1 >"$tmpdir/node.json"); then
echo "$msg"
exit "$state_critical"
fi
exit_status=$state_ok
output=()
get_condition() {
local name="$1"
jq -r "(.status.conditions // [])[] | select(.type == \"$name\") | [.status, .message] | @sh"
}
check_condition() {
local cond="$1"
local expected="$2"
local values
if values=$(get_condition "$cond" < "$tmpdir/node.json"); then
eval "set -- $values"
local status_lc="${1,,*}"
local msg="$2"
if [[ "$status_lc" != "$expected" ]]; then
output+=( "condition \"$cond\": $msg" )
exit_status=$(merge_status "$exit_status" "$state_critical")
fi
else
output+=( "missing \"$cond\" condition" )
exit_status=$(merge_status "$exit_status" "$state_critical")
fi
}
# Important: Keep usage description synchronized with the list of conditions!
check_condition Ready true
check_condition DiskPressure false
check_condition MemoryPressure false
check_condition OutOfDisk false
finish "$exit_status" \
"$(join_args ', ' ${output[@]+"${output[@]}"})" \
''
# vim: set sw=2 sts=2 et :