#!/bin/bash
# ======================================================================
#
# NAGIOS CLIENT CHECK :: check status of cronjobs executed with cronwrapper
#
# REQUIREMENTS
# - client uses a cronjob with cronwrapper (it creates parsable logs)
#
# It returns 
# - UNKNOWN if no job was found
# - OK if all jobs are ok
# - ERROR if minimum one job failes (wrong exitcode or is expired) 
#
# ----------------------------------------------------------------------
#
# ah=axel.hahn@iml.unibe.ch
# ds=daniel.schueler@iml.unibe.ch
#
# 2017-10-13  v1.0  ah,ds
# 2017-10-17  v1.1  ah,ds                     remove PIPESTATUS for Debian8 compatibility
# 2019-04-30  v1.2  ah,ds                     show scriptlabel of failed jobs in 1st line
# 2020-02-28  v1.3  ah,ds                     ouput with separated error jobs and OK jobs
# 2020-03-05  v1.4  <axel.hahn@iml.unibe.ch>  switch to ph.* helper functions
# 2022-02-28  v1.5  <axel.hahn@iml.unibe.ch>  fix output of error counter
# 2022-08-23  v2.0  <axel.hahn@iml.unibe.ch>  simplify it: use cronstatus.sh (it has exitcode >0 on errors now)
# 2023-07-27  v2.1  <axel.hahn@unibe.ch>      update help page
# 2023-09-18  v2.2  <axel.hahn@unibe.ch>      replace pipes in systemctl status output
# 2024-04-03  v2.3  <axel.hahn@unibe.ch>      add NO_COLOR=1 to supress color and emojis
# 2024-04-24  v2.4  <axel.hahn@unibe.ch>      add short status on top of details
# 2025-02-10  v2.5  <axel.hahn@unibe.ch>      harden sourcing files
# ======================================================================

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

export self_APPVERSION=2.5

statusScript=/opt/cronwrapper/cronstatus.sh

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

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

Show status of all Cronjobs using Axels Cronwrapper
https://github.com/axelhahn/cronwrapper

The last run of each job is verified to these conditions:
- exitcode was 0
- last run is younger than given TTL


SYNTAX:

    $_self [-h] [SCRIPT]

OPTIONS:

    -h or --help   show this help.

PARAMETERS:

    SCRIPT         optional: custom status script
                   default: $statusScript

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

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

test "$1" && statusScript="$1"

if [ ! -x "${statusScript}" ]; then
        ph.setStatus "unknown"
        ph.status Cronjob status - script not found/ not executable: "${statusScript}"
else

        export NO_COLOR=1
        out=$( $statusScript )
        rc=$?

        if [ $rc -ne 0 ]; then
                ph.setStatus "critical"
        else
                ph.setStatus "ok"
        fi
        ph.status Cronjob status $( echo "$out" | tail -1 )
        $statusScript -s | sed "s#\[0m##g"

        echo "$out" |  sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g" | tr '|' ':' 
fi
ph.exit

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