Skip to content

Commit aaf18a3

Browse files
committed
Clean up
1 parent 60ccf25 commit aaf18a3

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ 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
1817
-H APISERVER API URL to query, kubectl is used if this option is not set
1918
-T TOKEN Authorization token for API
2019
-t TOKENFILE Path to file with token in it
@@ -31,15 +30,16 @@ Nagios-style checks against Kubernetes API. Designed for usage with Nagios, Icin
3130
- Unbound Persistent Volumes in unboundpvs mode; default is 5
3231
- Job failed count in jobs mode; default is 2
3332
-b Brief mode (more suitable for Zabbix)
33+
-M EXIT_CODE Exit code when resource is missing; default is 2 (CRITICAL)
3434
-h Show this help and exit
3535

3636
Modes are:
3737
apiserver Not for kubectl, should be used for each apiserver independently
3838
components Check for health of k8s components (etcd, controller-manager, scheduler etc.)
39+
nodes Check for active nodes
3940
daemonsets Check for daemonsets readiness
4041
deployments Check for deployments availability
4142
jobs Check for failed jobs
42-
nodes Check for active nodes
4343
pods Check for restart count of containters in the pods
4444
replicasets Check for replicasets readiness
4545
statefulsets Check for statefulsets readiness

check_kubernetes.sh

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ usage() {
1616
1717
Options are:
1818
-m MODE Which check to perform
19-
-M EXIT_CODE Override default exit code when resource is missing
2019
-H APISERVER API URL to query, kubectl is used if this option is not set
2120
-T TOKEN Authorization token for API
2221
-t TOKENFILE Path to file with token in it
@@ -33,15 +32,16 @@ usage() {
3332
- Unbound Persistent Volumes in unboundpvs mode; default is 5
3433
- Job failed count in jobs mode; default is 2
3534
-b Brief mode (more suitable for Zabbix)
35+
-M EXIT_CODE Exit code when resource is missing; default is 2 (CRITICAL)
3636
-h Show this help and exit
3737
3838
Modes are:
3939
apiserver Not for kubectl, should be used for each apiserver independently
4040
components Check for health of k8s components (etcd, controller-manager, scheduler etc.)
41+
nodes Check for active nodes
4142
daemonsets Check for daemonsets readiness
4243
deployments Check for deployments availability
4344
jobs Check for failed jobs
44-
nodes Check for active nodes
4545
pods Check for restart count of containters in the pods
4646
replicasets Check for replicasets readiness
4747
statefulsets Check for statefulsets readiness
@@ -68,7 +68,7 @@ while getopts ":m:M:H:T:t:K:N:n:o:c:w:bh" arg; do
6868
case $arg in
6969
h) usage ;;
7070
m) MODE="$OPTARG" ;;
71-
M) MISSING="${OPTARG}" ;;
71+
M) MISSING_EXITCODE="${OPTARG}" ;;
7272
o) TIMEOUT="${OPTARG}" ;;
7373
H) APISERVER="${OPTARG%/}" ;;
7474
T) TOKEN="$OPTARG" ;;
@@ -84,6 +84,7 @@ while getopts ":m:M:H:T:t:K:N:n:o:c:w:bh" arg; do
8484
done
8585

8686
[ -z "$MODE" ] && usage
87+
MISSING_EXITCODE="${MISSING_EXITCODE:-2}"
8788

8889
if [ "$APISERVER" ]; then
8990
[ -z "$TOKEN" ] && [ -z "$TOKENFILE" ] && usage
@@ -182,7 +183,7 @@ mode_nodes() {
182183
if [ $EXITCODE = 0 ]; then
183184
if [ -z "${nodes[*]}" ]; then
184185
OUTPUT="No nodes found"
185-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
186+
EXITCODE="$MISSING_EXITCODE"
186187
else
187188
OUTPUT="OK. ${#nodes[@]} nodes are Ready"
188189
BRIEF_OUTPUT="${#nodes[@]}"
@@ -215,7 +216,7 @@ mode_components() {
215216
if [ $EXITCODE = 0 ]; then
216217
if [ -z "${components[*]}" ]; then
217218
OUTPUT="No components found"
218-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
219+
EXITCODE="$MISSING_EXITCODE"
219220
else
220221
OUTPUT="OK. Healthy: $healthy_comps"
221222
fi
@@ -314,7 +315,7 @@ mode_tls() {
314315
if [ $EXITCODE = 0 ]; then
315316
if [ -z "$ns" ]; then
316317
OUTPUT="No TLS certs found"
317-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
318+
EXITCODE="$MISSING_EXITCODE"
318319
else
319320
if [ $count_ok -gt 1 ]; then
320321
OUTPUT="OK. $count_ok TLS secrets are OK"
@@ -399,7 +400,7 @@ mode_pods() {
399400

400401
if [ -z "$ns" ]; then
401402
OUTPUT="No pods found"
402-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
403+
EXITCODE="$MISSING_EXITCODE"
403404
else
404405
if [ "$max_restart_count" -ge "$WARN" ]; then
405406
OUTPUT="Container $bad_container: $max_restart_count restarts. "
@@ -450,7 +451,7 @@ mode_deployments() {
450451
if [ $EXITCODE = 0 ]; then
451452
if [ -z "$ns" ]; then
452453
OUTPUT="No deployments found"
453-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
454+
EXITCODE="$MISSING_EXITCODE"
454455
else
455456
if [ $count_avail -gt 1 ]; then
456457
OUTPUT="OK. $count_avail deploymens are available"
@@ -495,7 +496,7 @@ mode_daemonsets() {
495496
select(.metadata.namespace==\"$ns\" and .metadata.name==\"$ds\") | \
496497
.status | to_entries | map(\"\(.key)=\(.value)\") | \
497498
.[]")
498-
if [ $EXITCODE == 0 ]; then
499+
if [ "$EXITCODE" == 0 ]; then
499500
OUTPUT="Daemonset $ns/$ds ${statusArr[numberReady]}/${statusArr[desiredNumberScheduled]} ready"
500501
fi
501502
if [ "${statusArr[numberReady]}" != "${statusArr[desiredNumberScheduled]}" ]; then
@@ -511,7 +512,7 @@ mode_daemonsets() {
511512
if [ $EXITCODE = 0 ]; then
512513
if [ -z "$ns" ]; then
513514
OUTPUT="No daemonsets found"
514-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
515+
EXITCODE="$MISSING_EXITCODE"
515516
else
516517
if [ $count_avail -gt 1 ]; then
517518
OUTPUT="OK. $count_avail daemonsets are ready"
@@ -573,7 +574,7 @@ mode_replicasets() {
573574
if [ $EXITCODE = 0 ]; then
574575
if [ -z "$ns" ]; then
575576
OUTPUT="No replicasets found"
576-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
577+
EXITCODE="$MISSING_EXITCODE"
577578
else
578579
if [ $count_avail -gt 1 ]; then
579580
OUTPUT="OK. $count_avail replicasets are ready"
@@ -636,7 +637,7 @@ mode_statefulsets() {
636637
if [ $EXITCODE = 0 ]; then
637638
if [ -z "$ns" ]; then
638639
OUTPUT="No statefulsets found"
639-
[ -z ${MISSING} ] && EXITCODE=2 || EXITCODE=${MISSING}
640+
EXITCODE="$MISSING_EXITCODE"
640641
else
641642
if [ $count_avail -gt 1 ]; then
642643
OUTPUT="OK. $count_avail statefulsets are ready"
@@ -684,8 +685,8 @@ mode_jobs() {
684685
fi
685686
for job in "${jobs[@]}"; do
686687
((total_jobs++))
687-
job_fail_count=$(echo $data | jq -r ".items[] | select(.status.failed and .metadata.name==\"$job\") | .status.failed")
688-
let "total_failed_count= $total_failed_count + $job_fail_count"
688+
job_fail_count=$(echo "$data" | jq -r ".items[] | select(.status.failed and .metadata.name==\"$job\") | .status.failed")
689+
total_failed_count="$((total_failed_count+job_fail_count))"
689690
if [ "$job_fail_count" -ge "${WARN}" ]; then
690691
OUTPUT="${OUTPUT}Job $job has $job_fail_count failures. "
691692
EXITCODE=1

0 commit comments

Comments
 (0)