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

rewrite check using cronstatus.sh

parent 93271f92
Branches
No related tags found
1 merge request!405731 simplify cronjob check
...@@ -22,166 +22,84 @@ ...@@ -22,166 +22,84 @@
# 2020-02-28 v1.3 ah,ds ouput with separated error jobs and OK jobs # 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 # 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-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 . $( 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
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 "=" function showHelp(){
# param: label cat <<EOF
# global: $logfile ______________________________________________________________________
function getLogValue(){
grep "^$1=" $logfile | cut -f 2- -d "="
}
$self_APPNAME
v$self_APPVERSION
(c) Institute for Medical Education - University of Bern
Licence: GNU GPL 3
______________________________________________________________________
# ---------------------------------------------------------------------- Show status of all Cronjobs using Axels Cronwrapper
# MAIN 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
# ---------------------------------------------------------------------- SYNTAX:
# check all logs $(basename $0) [-h] [SCRIPT]
# ----------------------------------------------------------------------
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
# ----- check ttl value OPTIONS:
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
# ----- show jobdetail and put to OK file or error file -h or --help show this help.
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
done PARAMETERS:
SCRIPT optional: custom status script
default: $statusScript
EOF
}
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
# output # MAIN
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
if [ -f $errfile ]; then # --- check param -h
iErrJobs=$( wc -l $errfile | cut -f 1 -d " " ) case "$1" in
echo "ERROR: $iErrJobs of $iJobs jobs [`cat $failfile | sed ':a;N;$!ba;s/\n/, /g'`] have a problem" "--help"|"-h")
echo showHelp
echo "********** Jobs with problems:" exit 0
cat $outputError ;;
echo *)
echo esac
echo "********** OK:"
cat $outputOk test "$1" && statusScript="$1"
rm -f $errfile $failfile $outputError
if [ ! -x "${statusScript}" ]; then
ph.setStatus "unknown"
ph.status Cronjob status - script not found/ not executable: "${statusScript}"
else else
echo "OK: $iJobs cronjob(s) run fine"
cat $outputOk
fi
out=$( $statusScript )
rc=$?
# --- exit if [ $rc -ne 0 ]; then
if [ $iErrJobs -ne 0 ]; then
ph.setStatus "critical" ph.setStatus "critical"
else else
ph.setStatus "ok" ph.setStatus "ok"
fi fi
ph.status Cronjob status $( echo "$out" | tail -1 )
rm -f $outputOk echo "$out" | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2})?)?[mGK]//g"
fi
ph.exit ph.exit
# ---------------------------------------------------------------------- # ----------------------------------------------------------------------
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment