Skip to content

Commit

Permalink
2022-03-09 ahahn added cw.* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Hahn Axel (hahn) committed Mar 9, 2022
1 parent 224bd1f commit e64beef
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 75 deletions.
43 changes: 8 additions & 35 deletions cronstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
#
# ------------------------------------------------------------
# 2022-01-12 ahahn fixes based on shellcheck
# 2022-03-09 ahahn added cw.* functions
# ------------------------------------------------------------


LOGDIR=/var/tmp/cronlogs
# outfile=/tmp/cronjob_status.$$.tmp
# outfile=/tmp/cronjob_status.tmp

test -f $( dirname $0)/cronwrapper.cfg && . $( dirname $0)/cronwrapper.cfg
. $( dirname $0)/inc_cronfunctions.sh

typeset -i iMaxAge
iMaxAge=$(date +%s)
typeset -i iErrJobs=0
Expand All @@ -23,31 +27,6 @@ typeset -i iErrJobs=0
# FUNCTIONS
# ----------------------------------------------------------------------

# set a terminal color by a keyword
# param string keyword to set a color; one of reset | head|cmd|input | ok|warning|error
function color(){
local sColorcode=""
case $1 in
"reset") sColorcode="0"
;;
"head") sColorcode="33" # yellow
;;
"cmd") sColorcode="94" # light blue
;;
"input") sColorcode="92" # green
;;
"ok") sColorcode="92" # green
;;
"warning") sColorcode="33" # yellow
;;
"error") sColorcode="91" # red
;;
esac
if [ -n "${sColorcode}" ]; then
echo -ne "\e[${sColorcode}m"
fi
}

# get a value from logfile (everything behind "="
# param: label
# global: $logfile
Expand All @@ -71,7 +50,7 @@ do
sPre=" "
sCmd=$(getLogValue SCRIPTNAME)
sLastStart=$(getLogValue SCRIPTSTARTTIME)
typeset -i iJobExpire=
typeset -i iJobExpire
iJobExpire=$(getLogValue JOBEXPIRE)
typeset -i rc
rc=$(getLogValue 'SCRIPTRC' | head -1)
Expand Down Expand Up @@ -125,9 +104,7 @@ do

# ----- OUTPUT
echo
color "head"
echo "--- $logfile"
color "reset"
cw.cecho "head" "--- $logfile"

echo "${sPre}${sCmd}"
echo "${sPre}last start: ${sLastStart}"
Expand All @@ -137,15 +114,11 @@ do
echo "${sPre}expires: ${iJobExpire} ${statusExpire}"

if [ $iErr -gt 0 ]; then
color "error"
echo "${sPre}CHECK FAILED"
cw.cecho "error" "${sPre}CHECK FAILED"
iErrJobs=$iErrJobs+1
else
color "ok"
echo "${sPre}CHECK OK"
cw.cecho "ok" "${sPre}CHECK OK"
fi
color "reset"

done

echo
Expand Down
26 changes: 15 additions & 11 deletions cronwrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
# 2021-02-23 ahahn add help and parameter detection
# 2022-01-12 ahahn fixes based on shellcheck
# 2022-01-14 ahahn fix runserver check
# 2022-03-09 ahahn small changes
# ------------------------------------------------------------

# show help
Expand Down Expand Up @@ -99,28 +100,31 @@ function w() {
# ------------------------------------------------------------
# CONFIG
# ------------------------------------------------------------
# . `dirname $0`/config_allgemein.sh

line1="--------------------------------------------------------------------------------"

# --- set vars with required cli params
typeset -i TTL=$1 2>/dev/null
CALLSCRIPT=$2
LABELSTR=$3
LOGFILE=/tmp/call_any_script_$$.log

if [ "${LABELSTR}" = "" ]; then
LABELSTR=$(basename "${CALLSCRIPT}" | cut -f 1 -d " " )
fi
# Label darf keine Unterstriche enthalten
LABELSTR=$(echo ${LABELSTR} | sed "s#_#-#g")
test -z "${LABELSTR}" && LABELSTR=$(basename "${CALLSCRIPT}" | cut -f 1 -d " " )

# replace underscore (because it is used as a delimiter)
# LABELSTR=$(echo ${LABELSTR} | sed "s#_#-#g")
LABELSTR=${LABELSTR//_/-}
TOUCHPART="_flag-${LABELSTR}_expire_"

LOGDIR="/var/tmp/cronlogs"
MYHOST=$( hostname -f )

# WHATAMI=/data/srdrs/admin/bin/what_am_i
# --- log executions of the whole day
JOBBLOGBASE=${MYHOST}_joblog_

# . $0.cfg
test -f $( dirname $0)/cronwrapper.cfg && . $( dirname $0)/cronwrapper.cfg
. $( dirname $0)/inc_cronfunctions.sh


FINALOUTFILE="$LOGDIR/${MYHOST}_${LABELSTR}.log"
JOBLOG="$LOGDIR/${JOBBLOGBASE}$(date +%a).done"
Expand Down Expand Up @@ -199,7 +203,7 @@ if ls "${lastfile}" >/dev/null 2>&1; then

w "REM INFO: expires $expdate - $(date -d @$expdate)"
typeset -i timeleft=$expdate-$iStart
w "REM INFO: job is locked for other servers for $timeleft more seconds"
# w "REM INFO: job is locked for other servers for $timeleft more seconds"
if ! echo "${MYHOST}" | grep -F "$runserver" >/dev/null; then
w "REM INFO: it locked up to $expdate by $runserver"
if [ $timeleft -gt 0 ]; then
Expand All @@ -209,8 +213,8 @@ if ls "${lastfile}" >/dev/null 2>&1; then
else
w REM INFO: OK, job is expired
fi
else
w REM INFO: job was executed on the same machine and can be executed here again.
# else
# w REM INFO: job was executed on the same machine before.
fi
else
w REM OK, executing job the first time
Expand Down
3 changes: 3 additions & 0 deletions docs/20_Usage/30_Cronstatus.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ In this example I have 2 cronjobs using the cronwrapper and both are OK. In that
duration: 0 s
ttl: 5 min OK
expires: 1641985021 2022-01-12 11:57:01 OK
CHECK OK
--- /var/tmp/cronlogs/www.example.com_imlpgcleanup.log
/opt/imlpgcleanup/walarchivecleanup.sh -p /tmp -a 10
Expand All @@ -31,9 +32,11 @@ In this example I have 2 cronjobs using the cronwrapper and both are OK. In that
duration: 0 s
ttl: 1440 - 24 h OK
expires: 1642047121 2022-01-13 05:12:01 OK
CHECK OK
> echo $?
0
```

The exit status of the cronstatus is always zero.
You can grep for "CHECK FAILED" to detect if any of the cronjobs failed.
51 changes: 39 additions & 12 deletions docs/20_Usage/40_Helper_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,62 @@ This adds a variable rcAll and a few functions.

# functions

## exec2
After sourcing inc_cronfunctions.sh you get a list of available function with `cw.help`.

It is a replacement for exec. It shows the executed command and executes it. Finally it calls fetchRc to save the return code.
```txt
cw.cecho
colored echo output using color and reset color afterwards
param string color code ... se cw.color
param string text to display
## fetchRc
cw.color
set a terminal color by a keyword
param string keyword to set a color; one of reset | head|cmd|input | ok|warning|error
This function fetches the returncode from \$?, shows it on stdout and incremets \$rcAll with it.
At the end of the script (or at any point) you have a \$rcAll = 0 if every command was successful. And \$rcAll <> 0 if any of it failed.
cw.exec
execute a given command, show return code (and add it to final exit code)
param string(s) command line to execute
## quit
cw.fetchRc
get last exitcode and store it in global var $rc
no parameter is required
Exit the scipt with the value of \$rcAll.
So your scscript exits with exitcode = 0 if every command was successful. And exitcode <> 0 if any of it failed.
cw.help
show help for available cw.* functions
no parameter required
cw.lock
verify locking and create one if no active lock was found
param string optional: string to create sonething uniq if your script can be started with multiple parameters
cw.quit
quit script with showing the total exitcode.
no parameter is required
cw.timer
get time in sec and milliseconds since start
no parameter is required
cw.unlock
remove an existing locking
no parameter is required
```

## example script

```bash
#!/bin/bash
. /usr/local/bin/inc_cronfunctions.sh

exec2 rsync -rav /my/source/dir/ /my/target/dir/
cw.exec rsync -rav /my/source/dir/ /my/target/dir/

# ... add command 2 here ...
fetchRc
# cw.fetchRc

# ... add command 3 here ...
fetchRc
# cw.fetchRc

#
quit
cw.quit

```
Loading

0 comments on commit e64beef

Please sign in to comment.