Skip to content

Commit 60ccf25

Browse files
authored
Merge pull request #14 from hydrapolic/feature/missing
Allow to override exit code when resource is missing
2 parents 5b503df + 402a8d9 commit 60ccf25

File tree

2 files changed

+20
-17
lines changed

2 files changed

+20
-17
lines changed

README.md

Lines changed: 5 additions & 4 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
@@ -35,15 +36,15 @@ Nagios-style checks against Kubernetes API. Designed for usage with Nagios, Icin
3536
Modes are:
3637
apiserver Not for kubectl, should be used for each apiserver independently
3738
components Check for health of k8s components (etcd, controller-manager, scheduler etc.)
39+
daemonsets Check for daemonsets readiness
40+
deployments Check for deployments availability
41+
jobs Check for failed jobs
3842
nodes Check for active nodes
3943
pods Check for restart count of containters in the pods
40-
deployments Check for deployments availability
41-
daemonsets Check for daemonsets readiness
42-
unboundpvs Check for unbound persistent volumes.
4344
replicasets Check for replicasets readiness
4445
statefulsets Check for statefulsets readiness
4546
tls Check for tls secrets expiration dates
46-
jobs Check for failed jobs
47+
unboundpvs Check for unbound persistent volumes.
4748

4849
## Examples:
4950

check_kubernetes.sh

Lines changed: 15 additions & 13 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
@@ -37,15 +38,15 @@ usage() {
3738
Modes are:
3839
apiserver Not for kubectl, should be used for each apiserver independently
3940
components Check for health of k8s components (etcd, controller-manager, scheduler etc.)
41+
daemonsets Check for daemonsets readiness
42+
deployments Check for deployments availability
43+
jobs Check for failed jobs
4044
nodes Check for active nodes
4145
pods Check for restart count of containters in the pods
42-
deployments Check for deployments availability
43-
daemonsets Check for daemonsets readiness
44-
unboundpvs Check for unbound persistent volumes.
4546
replicasets Check for replicasets readiness
4647
statefulsets Check for statefulsets readiness
4748
tls Check for tls secrets expiration dates
48-
jobs Check for failed jobs
49+
unboundpvs Check for unbound persistent volumes.
4950
EOF
5051

5152
exit 2
@@ -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)