#!/bin/bash
# ======================================================================
#
# Check ONEHOST
#
# requirements:
# - sudo onehost
#
# ----------------------------------------------------------------------
# 2023-06-09  v1.0  <axel.hahn@unibe.ch>  initial version
# 2023-06-12  v1.1  <axel.hahn@unibe.ch>  show message if no sudo permissions on onehost command exist
# 2023-08-23  v1.2  <axel.hahn@unibe.ch>  update help; show help without requirements
# 2023-09-15  v1.3  <axel.hahn@unibe.ch>  add detection for disabled hosts; update texts
# 2023-10-20  v1.4  <axel.hahn@unibe.ch>  harden sudo command execution
# 2025-02-10  v1.5  <axel.hahn@unibe.ch>      harden sourcing files
# ======================================================================

# shellcheck source=/dev/null
. "$( dirname "$0" )/inc_pluginfunctions" || exit 1

self_APPVERSION=1.5

# ----------------------------------------------------------------------
# functions
# ----------------------------------------------------------------------

function showHelp(){
    local _self; _self="$( basename "$0" )"
cat <<EOF
$( ph.showImlHelpHeader )

Show count of hosts in OpenNebula and warn if a host is disabled or on error.
It switches to critical if more than a given critical level are not running.

SYNTAX:
$_self [ -w value -c value -h ]

    -w VALUE       warning level  (default: 1)
    -c VALUE       critical level (default: 2)
    -h or --help   show this help.

PARAMETERS:

    None.

EXAMPLE:
$_self -c 1   set to critical if the 1st host is off.

EOF
}
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------

# --- check param -h
case "$1" in
    "--help"|"-h")
        showHelp
        exit 0
        ;;
    *)
esac

# --- check required tools
ph.require onehost

# --- set optional limits
typeset -i iWarnLimit=$(     ph.getValueWithParam 1 w "$@")
typeset -i iCriticalLimit=$( ph.getValueWithParam 2 c "$@")


# --- get data
if ! cmdout=$( sudo -n onehost list --csv 2>&1 ); then
    ph.abort "UNKNOWN: No sudo permissions to execute onehost commands."
fi

if ! grep "ID,NAME" <<< "$cmdout" >/dev/null; then
        ph.setStatus "unknown"
        echo "UNKNOWN: sudo onehost failed."
        echo "$cmdout"
        ph.exit
fi

# header=$( head -1 <<< "$cmdout" )
csvdata=$( echo "$cmdout" | sed -n '2,$p' )

# --- get result

out=""
typeset -i iTotal; iTotal=$( echo "$csvdata" | wc -l )
typeset -i iOn;    iOn=$(    echo "$csvdata" | grep -c "on$" )
typeset -i iDis;   iDis=$(   echo "$csvdata" | grep -c "dsbl$" )
typeset -i iNotOn; iNotOn=$iTotal-$iOn
typeset -i iOther; iOther=$iTotal-$iOn-$iDis

ph.perfadd "total"     "${iTotal}"
ph.perfadd "on"        "${iOn}"
ph.perfadd "disabled"  "${iDis}"
ph.perfadd "other"     "${iOther}"

if [ $iNotOn -ge $iCriticalLimit ]; then
        out="ERROR: not all hosts are up - count $iNotOn reached critical value $iCriticalLimit"
        ph.setStatus "critical"
elif [ $iNotOn -ge $iWarnLimit ]; then
        out="WARNING: not all hosts are up - count $iNotOn reached warning value $iWarnLimit"
        ph.setStatus "warning"        
fi

ph.status "ONEHOST - Total: $iTotal .. on: $iOn .. disabled: $iDis ... other: $iOther"
echo "$cmdout"
echo "$out"
ph.exit