#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: check deployment
#
# ----------------------------------------------------------------------
# 2023-12-14  v0.1  first lines
# 2025-02-10  v1.0  <axel.hahn@unibe.ch>      harden sourcing files
# ======================================================================

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

export self_APPVERSION=1.0

sInstalldir=/opt/imldeployment-installer

# ----------------------------------------------------------------------
# FUNCTIONS
# ----------------------------------------------------------------------

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

Show status of rollouts with IML deployment client on this system.
See https://os-docs.iml.unibe.ch/imldeployment-client/

SYNTAX:
$_self [-d DIRECTORY]

OPTIONS:
    -h or --help   show this help.
    -d or --dir    set installation dir of iml deployment to find its check skript
                   default: ${sInstalldir}

EXAMPLE:
$_self -d /home/deployuser/imldeployment-installer
                   set a custom directory und run the backup check

EOF
}

# --- check param -h
while [[ "$#" -gt 0 ]]; do case $1 in
    -h|--help)      showHelp; exit 0;;
    -d|--dir)       sInstalldir=$2; shift ;shift;;
    *) echo "ERROR: Unknown parameter: $1"; showHelp; exit 1;
esac; done

sChecker=$sInstalldir/check_deployment.sh

if [ ! -x "$sChecker" ]; then
    ph.abort "$sChecker not found - maybe deployment is not installed here. Use param -d to set the install directory."
fi

result=$( $sChecker )
rc=$?

if [ $rc -eq 0 ]; then
    if echo "$result" | grep "^UNKNOWN" >/dev/null  
    then
        ph.setStatus "unknown"
    else
        ph.setStatus "ok"
    fi
else
    ph.setStatus "critical"
fi

echo "$result"

ph.exit

# ----------------------------------------------------------------------