forked from appuio/nagios-plugins-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils
executable file
·172 lines (151 loc) · 3.54 KB
/
utils
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
#!/bin/bash
readonly state_ok=0
readonly state_warning=1
readonly state_critical=2
readonly state_unknown=3
readonly OPENSHIFT_CLIENT_BINARY=/usr/bin/oc
#
# Print arguments joined by delimiter
#
# Arguments:
# - delim: Delimiter; may have multiple characters
# - args...: Values
#
# Source: https://stackoverflow.com/a/17841619
#
join_args() {
local delim="$1"; shift
if [[ "$#" -gt 0 ]]; then
echo -n "$1"; shift
if [[ "$#" -gt 0 ]]; then
printf "%s" "${@/#/$delim}"
fi
fi
}
#
# Check whether the arguments are integers
#
validate_integer() {
for i; do
if ! [[ "$i" =~ ^-?[0-9]+$ ]]; then
echo "\"$i\" not recognized as an integer" >&2
return 1
fi
done
return 0
}
#
# Sort arguments into array variable given as first argument
#
# Arguments:
# - var: Name of output variable
# - args...: Values
#
# Source: https://stackoverflow.com/a/7442583
#
array_sort() {
local var="$1"; shift
readarray -t "$var" < <(printf '%s\0' "$@" | LC_ALL=C sort -z | xargs -0n1)
}
#
# Copy unique arguments into array variable given as first argument. The input
# elements must be sorted.
#
# Arguments:
# - var: Name of output variable
# - args...: Values
#
array_uniq() {
local var="$1"; shift
readarray -t "$var" < <(printf '%s\0' "$@" | LC_ALL=C uniq -z | xargs -0n1)
}
#
# Subtract matching elements in second array from first array and store
# the result into array variable given as first argument (var = b - a).
# The input arrays must be sorted. Elements must not contain newline
# characters.
#
# Arguments:
# - var: Name of output variable
# - a: Name of first input array
# - b: Name of second input array
#
# Source: https://unix.stackexchange.com/a/104848
#
array_sub() {
# Use indirect referencing with array, see <https://stackoverflow.com/a/4582492>
local var="$1" a="$2[@]" b="$3[@]"
readarray -t "$var" < <(comm -13 <(printf '%s\n' ${!a+"${!a}"}) <(printf '%s\n' ${!b+"${!b}"}))
}
#
# Compute common elements between two arrays and store the result into array
# variable given as first argument (var = b & a). The input arrays must be
# sorted. Elements must not contain newline characters.
#
# Arguments:
# - var: Name of output variable
# - a: Name of first input array
# - b: Name of second input array
#
array_and() {
# Use indirect referencing with array, see <https://stackoverflow.com/a/4582492>
local var="$1" a="$2[@]" b="$3[@]"
readarray -t "$var" < <(comm -12 <(printf '%s\n' ${!a+"${!a}"}) <(printf '%s\n' ${!b+"${!b}"}))
}
#
# Merge exit status
#
# Warning wins over OK, critical wins over warning
#
# Arguments:
# - Current value
# - Desired value
#
merge_status() {
local cur="$1" update="$2"
if (( cur < update )); then
echo "$update"
else
echo "$cur"
fi
}
run_oc() {
local cfgfile="$1"; shift
# See https://github.com/openshift/origin/issues/9581
HOME=/ \
"$OPENSHIFT_CLIENT_BINARY" \
--config="$cfgfile" \
--namespace=default \
"$@"
}
#
# Print result in Nagios-compatible format and terminate process
#
# Arguments:
# - exit_status: Status code
# - output: Human-readable output
# - metrics: Metrics for Nagios-compatible monitoring systems
#
finish() {
local exit_status="$1"
local output="$2"
local metrics="$3"
local prefix=UNKNOWN
case "$exit_status" in
$state_ok)
prefix=OK
;;
$state_warning)
prefix=WARNING
;;
$state_critical)
prefix=CRITICAL
;;
*)
exit_status=$state_unknown
;;
esac
echo "$prefix${output:+ $output}${metrics:+ | $metrics}"
exit "$exit_status"
}
# vim: set sw=2 sts=2 et :