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_pod_node_alloc
executable file
·114 lines (92 loc) · 2.36 KB
/
check_openshift_pod_node_alloc
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
#!/bin/bash
set -e -u -o pipefail
. /usr/lib/nagios-plugins-openshift/utils
default_namespace=default
usage() {
echo "Usage: $0 -f <path> [-n <namespace>] [-s <selector>]"
echo
echo 'Check whether all pods matching given selector are running'\
'on disparate nodes'
echo
echo 'Options:'
echo ' -f Config file path'
echo " -n Namespace (default: \"$default_namespace\")"
echo ' -s Selector for pods, e.g. "deploymentconfig=router"'
}
opt_cfgfile=
opt_namespace=$default_namespace
opt_selector=
while getopts 'hf:n:s:w:c:' opt; do
case "$opt" in
h)
usage
exit 0
;;
f) opt_cfgfile="$OPTARG" ;;
n) opt_namespace="$OPTARG" ;;
s) opt_selector="$OPTARG" ;;
*)
usage >&2
exit 1
;;
esac
done
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 pod \
--output=json \
--namespace="$opt_namespace" \
--selector="$opt_selector" \
2>&1 >"$tmpdir/pod.json"); then
echo "$msg"
exit "$state_critical"
fi
jq -r '
(
.items |
map(select((.status.conditions // [])[] | select(.type == "Ready") | .status == "True"))
) as $pods |
(
@sh "node_name= total_pod_count=\($pods | length)",
(
$pods | group_by(.spec.nodeName)[] |
@sh "node_name=\(.[0].spec.nodeName) pod_names=( \(map(.metadata.name) | sort) )"
)
)
' < "$tmpdir/pod.json" > "$tmpdir/pod.txt"
exit_status="$state_ok"
output=()
limit=
while read line; do
eval "$line"
if [[ -z "${node_name:-}" ]]; then
# Line with information for all pods
# Calculate limit to complain if >50% are on single node
(( limit=((total_pod_count + 1) / 2) ))
# Apply arbitrary upper limit
if (( limit > 3 )); then
limit=3
fi
continue
fi
pod_count="${#pod_names[*]}"
if (( pod_count > limit )); then
output+=( "${pod_count} of ${total_pod_count} instances running on ${node_name} (${pod_names[*]})" )
exit_status=$(merge_status "$exit_status" "$state_critical")
fi
done < "$tmpdir/pod.txt"
finish "$exit_status" \
"$(join_args ', ' ${output[@]+"${output[@]}"})" \
''
# vim: set sw=2 sts=2 et :