diff --git a/check_cronstatus b/check_cronstatus index 0cb3266a408b53fe8a5a05697626d31c7aed6536..e6752649828742276ba6fcc0342bde86e0af9fa3 100755 --- a/check_cronstatus +++ b/check_cronstatus @@ -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 - - # ----- 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}" +OPTIONS: - 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 -else - echo "OK: $iJobs cronjob(s) run fine" - cat $outputOk -fi +# --- check param -h +case "$1" in + "--help"|"-h") + showHelp + exit 0 + ;; + *) +esac +test "$1" && statusScript="$1" -# --- exit -if [ $iErrJobs -ne 0 ]; then - ph.setStatus "critical" +if [ ! -x "${statusScript}" ]; then + ph.setStatus "unknown" + ph.status Cronjob status - script not found/ not executable: "${statusScript}" else - ph.setStatus "ok" -fi -rm -f $outputOk + out=$( $statusScript ) + rc=$? + if [ $rc -ne 0 ]; then + ph.setStatus "critical" + else + ph.setStatus "ok" + fi + 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 # ----------------------------------------------------------------------