#!/bin/bash
#
# check_k8s_daemonsets
#
# Author        : Nohaj
# Contact       : johan@slashroot.fr
# Date          : 31/10/19
# Version       : 1.0
# Description   : Basic script to check the daemonsets status of a k8s cluster 
# Require       : kubectl utility with a configured access to a k8s cluster
#

#
# Variables and checks
#

# This script needs kubectl
type kubectl &> /dev/null || { echo >&2 "UNKNOWN: This script need the kubectl utility to run."; exit 3; }

# Check kubectl command
if ( ! kubectl get daemonset &> /dev/null ) ; then
    echo "UNKNOWN: Kubectl command doesn't seems to be working"
    exit 3
fi

# Defaults thresolds
warn_thresold=1
crit_thresold=5

# tmpfile to restore results
tmpfile=$(mktemp /tmp/$(basename $0).XXXXXX)

usage (){
cat <<EOF

Usage : check_k8s_daemonsets [-n NAMESPACE[,NAMESPACE2,NAMESPACE3]] [-w WARN_THRESOLD] [-c CRIT_THRESOLD]

Options:
    -h                      Print help
    -n NAMESPACE            Namespace(s) inside which we'll check the daemonsets (if not precised, all daemonsets will be checked)
    -w WARN_THRESOLD        Warning threshold for problematics daemonsets (default : $warn_thresold)
    -c CRIT_THRESOLD        Critical threshold for problematics daemonsets (default : $crit_thresold)

EOF
exit 3
}

#
# Let's go
#

while getopts ":n:h:w:c:" opt; do
    case "$opt" in
        n)
            namespaces="$OPTARG"
            ;;
        w)
            warn_thresold="$OPTARG"
            ;;
        c)
            crit_thresold="$OPTARG"
            ;;
        h) 
            usage 
            ;;
        *) 
            usage
            ;;
    esac
done

# Retrive all the states
if [[ -n $namespaces ]] ; then
    for namespace in $(echo $namespaces | tr ',' ' ') ; do
        kubectl get daemonsets -n $namespace --no-headers | awk -F " " '{print $1,$2,$3,$4}' >> $tmpfile
    done
else
    kubectl get daemonsets -A --no-headers | awk -F " " '{print $2,$3,$4,$5}' > $tmpfile
fi

ds_ok=0
ds_err=0

while read line ; do
    ds=$(echo $line | awk -F " " '{print $1}')
    desired=$(echo $line | awk -F " " '{print $2}')
    current=$(echo $line | awk -F " " '{print $3}')
    ready=$(echo $line | awk -F " " '{print $4}')
    state="$ready/$current/$desired"
    if [[ $desired -eq $current ]] && [[ $current -eq $ready ]] ; then
        ds_ok=$((ds_ok+1))
        message_ok="$message_ok$ds:$state\n"
    else
        ds_err=$((ds_err+1))
        message_err="$message_err$ds:$state\n"
    fi   
done < $tmpfile

rm -f $tmpfile

ds=$((ds_ok+ds_err))

if [[ $ds_err -ge $crit_thresold ]] ; then
    echo "CRITICAL: $ds_err daemonsets are in a failed condition"
    echo -e $message_err
    exit 2
fi

if [[ $ds_err -ge $warn_thresold ]] ; then
    echo "WARNING: $ds_err daemonsets are in a failed condition"
    echo -e $message_err
    exit 1
fi

if [[ $ds_err -lt $warn_thresold ]] ; then
    echo "OK: All the daemonsets are in good condition"
    echo -e $message_ok
    exit 0
else
    echo "UNKNWON:"
    exit 3
fi