Skip to content
Snippets Groups Projects
Commit edb41b4a authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

Merge branch '5731-simplify-cronjob-check' into 'master'

5731 simplify cronjob check

See merge request !40
parents 93271f92 4e944952
No related branches found
No related tags found
1 merge request!405731 simplify cronjob check
......@@ -22,166 +22,84 @@
# 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)
# ======================================================================
. `dirname $0`/inc_pluginfunctions
LOGDIR=/var/tmp/cronlogs
errfile=/tmp/cronjob_status.$$.err
failfile=/tmp/cronjob_status.$$.fails
outputOk=/tmp/cronjob_status_out_ok.$$.txt
outputError=/tmp/cronjob_status_out_error.$$.txt
typeset -i iMaxAge=`date +%s`
typeset -i iJobs=0
typeset -i iErrJobs=0
. $( dirname $0 )/inc_pluginfunctions
self_APPNAME=$( basename $0 | tr [:lower:] [:upper:] )
self_APPVERSION=2.0
statusScript=/opt/cronwrapper/cronstatus.sh
# ----------------------------------------------------------------------
# FUNCTIONS
# functions
# ----------------------------------------------------------------------
# get a value from logfile (everything behind "="
# param: label
# global: $logfile
function getLogValue(){
grep "^$1=" $logfile | cut -f 2- -d "="
}
function showHelp(){
cat <<EOF
______________________________________________________________________
$self_APPNAME
v$self_APPVERSION
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
______________________________________________________________________
# ----------------------------------------------------------------------
# MAIN
# ----------------------------------------------------------------------
Show status of all Cronjobs using Axels Cronwrapper
https://github.com/axelhahn/cronwrapper
iJobs=`ls -1 $LOGDIR/*log 2>/dev/null | fgrep -v "/__" | fgrep ".log" | wc -l`
The last run of each job is verified to these conditions:
- exitcode was 0
- last run is younger than given TTL
if [ $iJobs -eq 0 ]; then
ph.abort "SKIP: no cronjobs with cronwrapper were found"
fi
# ----------------------------------------------------------------------
# check all logs
# ----------------------------------------------------------------------
ls -1t $LOGDIR/*log | fgrep -v "/__" | while read logfile
do
iJobs=$iJobs+1
typeset -i iErr=0
sTmpOutfile=$outputOk
server=`basename $logfile | cut -f 1 -d "_"`
jobname=`basename $logfile | cut -f 2 -d "_" | sed "s#\.log##"`
sPre=" "
sCmd=`getLogValue SCRIPTNAME`
sLastStart=`getLogValue SCRIPTSTARTTIME`
typeset -i iJobExpire=`getLogValue JOBEXPIRE`
typeset -i rc=`getLogValue 'SCRIPTRC' | head -1`
typeset -i iEcectime=`getLogValue 'SCRIPTEXECTIME' | head -1 | cut -f 1 -d " "`
sTTL=`getLogValue 'SCRIPTTTL'`
# ----- check return code
statusRc='OK'
if [ $rc -ne 0 ]; then
iErr=$iErr+1
statusRc='ERROR'
fi
SYNTAX:
$(basename $0) [-h] [SCRIPT]
# ----- check ttl value
typeset -i iTTL=$sTTL
typeset -i iTTLsec=0
iTTL=$iTTL
iTTLsec=$iTTL*60
ttlstatus="OK"
if [ -z $sTTL ]; then
iErr=$iErr+1
statusTtl="ERROR: ttl value is empty"
else
# human readable ttl in min/ hours/ days
statusTtl="$iTTL min"
if [ $iTTL -gt 60 ]; then
iTTL=$iTTL/60;
statusTtl="$sTTL - $iTTL h"
if [ $iTTL -gt 24 ]; then
iTTL=$iTTL/24;
statusTtl="$sTTL - $iTTL d"
fi
fi
if [ $iTTLsec -lt $iEcectime ]; then
iErr=$iErr+1
statusTtl="ERROR: $iTTL min = $iTTLsec s - is too low; exec time is $iEcectime s - set a higher TTL for this cronjob"
iErr=$iErr+1
else
statusTtl="$statusTtl OK"
fi
fi
# ----- check expire
statusExpire="`date -d @$iJobExpire '+%Y-%m-%d %H:%M:%S'`"
if [ $iJobExpire -lt $iMaxAge ]; then
statusExpire="${statusExpire} ERROR"
iErr=$iErr+1
else
statusExpire="${statusExpire} OK"
fi
OPTIONS:
# ----- show jobdetail and put to OK file or error file
sTmpOutfile=$outputOk
test $iErr -gt 0 && sTmpOutfile=$outputError
(
echo
echo --- $logfile
echo "${sPre}${sCmd}"
echo "${sPre}last start: ${sLastStart}"
echo "${sPre}returncode: ${rc} ${statusRc}"
echo "${sPre}duration: ${iEcectime} s"
echo "${sPre}ttl: ${statusTtl}"
echo "${sPre}expires: ${iJobExpire} ${statusExpire}"
if [ $iErr -gt 0 ]; then
echo "${sPre}CHECK FAILED"
iErrJobs=$iErrJobs+1
echo "$logfile" > $errfile
getLogValue SCRIPTLABEL >> $failfile
fi
)>>$sTmpOutfile
-h or --help show this help.
done
PARAMETERS:
SCRIPT optional: custom status script
default: $statusScript
EOF
}
# ----------------------------------------------------------------------
# output
# MAIN
# ----------------------------------------------------------------------
if [ -f $errfile ]; then
iErrJobs=$( wc -l $errfile | cut -f 1 -d " " )
echo "ERROR: $iErrJobs of $iJobs jobs [`cat $failfile | sed ':a;N;$!ba;s/\n/, /g'`] have a problem"
echo
echo "********** Jobs with problems:"
cat $outputError
echo
echo
echo "********** OK:"
cat $outputOk
rm -f $errfile $failfile $outputError
# --- 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
echo "OK: $iJobs cronjob(s) run fine"
cat $outputOk
fi
out=$( $statusScript )
rc=$?
# --- exit
if [ $iErrJobs -ne 0 ]; then
if [ $rc -ne 0 ]; then
ph.setStatus "critical"
else
ph.setStatus "ok"
fi
rm -f $outputOk
ph.status Cronjob status $( echo "$out" | tail -1 )
echo "$out" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g"
fi
ph.exit
# ----------------------------------------------------------------------
......@@ -22,7 +22,7 @@ If there are processes with status D they will be shown (to analyze cpu waits).
## Syntax
``$ check_NAME [-c CRITICAL] [-w WARING] [-i CRITICAL_IO]``
``$ check_cpu [-c CRITICAL] [-w WARING] [-i CRITICAL_IO]``
### Parameters
......
# Check Cronstatus
## Introduction
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
`$ check_cronstatus [-h] [SCRIPT]`
### Parameters
```text
OPTIONS:
-h or --help show this help.
PARAMETERS:
SCRIPT optional: custom status script
default: /opt/cronwrapper/cronstatus.sh
```
## Examples
`check_cronstatus`
The Check of cronjobs with the default loction does not require any parameter
`check_cronstatus /usr/share/cronwrapper/cronstatus.sh`
The Check of cronjobs with custom loction.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment