Skip to content

Commit 75fc7c2

Browse files
committed
Allow exit code override when resource is missing
1 parent 5b503df commit 75fc7c2

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Nagios-style checks against Kubernetes API. Designed for usage with Nagios, Icin
1414

1515
Options are:
1616
-m MODE Which check to perform
17+
-M EXIT_CODE Override default exit code when resource is missing
1718
-H APISERVER API URL to query, kubectl is used if this option is not set
1819
-T TOKEN Authorization token for API
1920
-t TOKENFILE Path to file with token in it

check_kubernetes.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ usage() {
1616
1717
Options are:
1818
-m MODE Which check to perform
19+
-M EXIT_CODE Override default exit code when resource is missing
1920
-H APISERVER API URL to query, kubectl is used if this option is not set
2021
-T TOKEN Authorization token for API
2122
-t TOKENFILE Path to file with token in it
@@ -63,10 +64,11 @@ die() {
6364
exit "${2:-2}"
6465
}
6566

66-
while getopts ":m:H:T:t:K:N:n:o:c:w:bh" arg; do
67+
while getopts ":m:M:H:T:t:K:N:n:o:c:w:bh" arg; do
6768
case $arg in
6869
h) usage ;;
6970
m) MODE="$OPTARG" ;;
71+
M) MISSING="${OPTARG}" ;;
7072
o) TIMEOUT="${OPTARG}" ;;
7173
H) APISERVER="${OPTARG%/}" ;;
7274
T) TOKEN="$OPTARG" ;;
@@ -180,7 +182,7 @@ mode_nodes() {
180182
if [ $EXITCODE = 0 ]; then
181183
if [ -z "${nodes[*]}" ]; then
182184
OUTPUT="No nodes found"
183-
EXITCODE=2
185+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
184186
else
185187
OUTPUT="OK. ${#nodes[@]} nodes are Ready"
186188
BRIEF_OUTPUT="${#nodes[@]}"
@@ -213,7 +215,7 @@ mode_components() {
213215
if [ $EXITCODE = 0 ]; then
214216
if [ -z "${components[*]}" ]; then
215217
OUTPUT="No components found"
216-
EXITCODE=2
218+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
217219
else
218220
OUTPUT="OK. Healthy: $healthy_comps"
219221
fi
@@ -312,7 +314,7 @@ mode_tls() {
312314
if [ $EXITCODE = 0 ]; then
313315
if [ -z "$ns" ]; then
314316
OUTPUT="No TLS certs found"
315-
EXITCODE=2
317+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
316318
else
317319
if [ $count_ok -gt 1 ]; then
318320
OUTPUT="OK. $count_ok TLS secrets are OK"
@@ -397,7 +399,7 @@ mode_pods() {
397399

398400
if [ -z "$ns" ]; then
399401
OUTPUT="No pods found"
400-
EXITCODE=2
402+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
401403
else
402404
if [ "$max_restart_count" -ge "$WARN" ]; then
403405
OUTPUT="Container $bad_container: $max_restart_count restarts. "
@@ -448,7 +450,7 @@ mode_deployments() {
448450
if [ $EXITCODE = 0 ]; then
449451
if [ -z "$ns" ]; then
450452
OUTPUT="No deployments found"
451-
EXITCODE=2
453+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
452454
else
453455
if [ $count_avail -gt 1 ]; then
454456
OUTPUT="OK. $count_avail deploymens are available"
@@ -509,7 +511,7 @@ mode_daemonsets() {
509511
if [ $EXITCODE = 0 ]; then
510512
if [ -z "$ns" ]; then
511513
OUTPUT="No daemonsets found"
512-
EXITCODE=2
514+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
513515
else
514516
if [ $count_avail -gt 1 ]; then
515517
OUTPUT="OK. $count_avail daemonsets are ready"
@@ -571,7 +573,7 @@ mode_replicasets() {
571573
if [ $EXITCODE = 0 ]; then
572574
if [ -z "$ns" ]; then
573575
OUTPUT="No replicasets found"
574-
EXITCODE=2
576+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
575577
else
576578
if [ $count_avail -gt 1 ]; then
577579
OUTPUT="OK. $count_avail replicasets are ready"
@@ -634,7 +636,7 @@ mode_statefulsets() {
634636
if [ $EXITCODE = 0 ]; then
635637
if [ -z "$ns" ]; then
636638
OUTPUT="No statefulsets found"
637-
EXITCODE=2
639+
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
638640
else
639641
if [ $count_avail -gt 1 ]; then
640642
OUTPUT="OK. $count_avail statefulsets are ready"

0 commit comments

Comments
 (0)