#!/bin/bash
#
# check_k8s_storageos_node
#
# Author        : Nohaj
# Contact       : johan@slashroot.fr
# Date          : 12/11/19
# Version       : 1.0
# Description   : Basic script to check the storageos node usage inside a kubernetes cluster
# Require       : Access to the storageos API of all storageos nodes
#

#
# Variables and checks
#

# Defaults thresolds
warn_thresold=80
crit_thresold=90

# count variables
warn_node=0
crit_node=0

# tmp file to store results
tmpfile=$(mktemp /tmp/abc-script.XXXXXX)

usage (){
cat <<EOF

Usage : check_k8s_storageos_node -n NODE1,NODE2,NODE3... [-w WARN_THRESOLD] [-c CRIT_THRESOLD] 

Options:
    -h                        Print help
    -n NODE1,NODE2,NODE3...   StorageOS nodes to check
    -w WARN_THRESOLD          Warning usage threshold (default : $warn_thresold)
    -c CRIT_THRESOLD          Critical usage threshold (default : $crit_thresold)
EOF
exit 3
}


#
# Let's go
#

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

# On verifie que les noeuds storageos sont bien passes en parametre
if [[ -z $nodes ]] ; then
    echo "UNKNOWN: No nodes specified. Please check the command usage"
    exit 3
fi

# On parcours chaque noeud et on recupere l'utilisation total
OLDIFS="$IFS"
IFS=$','
for node in $nodes ; do
    IFS="$OLDIFS"
    curl -s $node:5705/metrics > $tmpfile
    size=$(grep ^storageos_node_device_capacity_bytes $tmpfile | cut -d " " -f 2)
    size_b=$(echo $size | perl -ne 'printf "%d\n", $_;')
    size_t=$(echo "scale=2; $size_b / 1024 / 1024 / 1024 / 1024" | bc )
    free=$(grep ^storageos_node_device_free_bytes $tmpfile | cut -d " " -f 2)
    free_b=$(echo $free | perl -ne 'printf "%d\n", $_;')
    free_t=$(echo "scale=2; $free_b / 1024 / 1024 / 1024 / 1024" | bc -l | sed 's/^\./0./')
    usage_b=$((size_b-free_b))
    usage_t=$(echo "scale=2; $usage_b / 1024 / 1024 / 1024 / 1024" | bc -l | sed 's/^\./0./')
    perc_util=$(echo "scale=2; $usage_b * 100 / $size_b" | bc -l | sed 's/^\./0./')
    perc_util_nofloat=$(echo $perc_util |cut -d '.' -f 1)
    if [[ $perc_util_nofloat -gt $crit_thresold ]] ; then
        crit=1
        crit_node=$((crit_node+1))
        err_message=""$err_message"$node: $perc_util% ("$usage_t"TiB/"$size_t"TiB)\n"
    elif [[ $perc_util_nofloat -gt $warn_thresold ]] ; then
        warn=1
        warn_node=$((warn_node+1))
        warn_message=""$warn_message"$node: $perc_util% ("$usage_t"TiB/"$size_t"TiB)\n"
    else
        message=""$message"$node: $perc_util% ("$usage_t"TiB/"$size_t"TiB)\n"
        ok=$((ok+1))
    fi
done

# On affiche le resutat selon les donnees recuperees
if [[ $crit -eq 1 ]] ; then
    if [[ $crit_node -eq 1 ]] ; then
        echo "CRITICAL: 1 node is above the critical thresold"
    else
        echo "CRITICAL: $crit_node node(s) are above the critical thresold"
    fi
    exit_code=2
    echo -e $err_message |sort -nr -k 2
fi

if [[ $warn -eq 1 ]] ; then
    if [[ $warn_node -eq 1 ]] ; then
        echo "WARNING: 1 node is above the warning thresold"
    else
        echo "WARNING: $warn_node node(s) are above the warning thresold"
    fi
    if [[ $exit_code -ne 2 ]] ; then
        exit_code=1
    fi
    echo -e $warn_message | sort -nr -k 2
else
    echo "OK: $ok nodes are below thresolds"
    echo -e $message | sort -nr -k 2
fi
