#!/bin/bash
#
# check_k8s_deployments
#
# Author        : Nohaj
# Contact       : johan@slashroot.fr
# Date          : 31/10/19
# Version       : 1.1 (09/06/21)
# Description   : Basic script to check the deployments status of a k8s cluster 
# Require       : kubectl utility with a configured access to a k8s cluster
#

#
# CHANGELOG
#
# v1.1 : on prend egalement en compte les statefulset 
#

#
# 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 deployments &> /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_deployments [-n NAMESPACE[,NAMESPACE2,NAMESPACE3]] [-w WARN_THRESOLD] [-c CRIT_THRESOLD]

Options:
    -h                      Print help
    -n NAMESPACE            Namespace(s) inside which we'll check the deployments (if not precised, all deployments will be checked)
    -w WARN_THRESOLD        Warning threshold for problematics deployments (default : $warn_thresold)
    -c CRIT_THRESOLD        Critical threshold for problematics deployments (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 deployments -n $namespace --no-headers | awk -F " " '{print $1,$2}' >> $tmpfile
        kubectl get sts -n $namespace --no-headers | awk -F " " '{print $1,$2}' >> $tmpfile
    done
else
    kubectl get deployments -A --no-headers | awk -F " " '{print $2,$3}' > $tmpfile
    kubectl get sts -A --no-headers | awk -F " " '{print $2,$3}' >> $tmpfile
fi

deps_ok=0
deps_err=0

while read line ; do
    dep=$(echo $line | awk -F " " '{print $1}')
    state=$(echo $line | awk -F " " '{print $2}')
    running=$(echo $state | cut -d '/' -f 1)
    available=$(echo $state | cut -d '/' -f 2)
    if [[ $running -eq $available ]] && [[ $available -gt 0 ]] ; then
        deps_ok=$((deps_ok+1))
        message_ok="$message_ok$dep:$state\n"
    else
        deps_err=$((deps_err+1))
        message_err="$message_err$dep:$state\n"
    fi   
done < $tmpfile

rm -f $tmpfile

deps=$((deps_ok+deps_err))

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

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

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