Skip to content

Commit

Permalink
update help; use cw.emoji; update exitcodes; update cronstatus
Browse files Browse the repository at this point in the history
  • Loading branch information
axelhahn committed Jan 23, 2024
1 parent 76edf45 commit e0aed4e
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 84 deletions.
113 changes: 76 additions & 37 deletions cronlog-sync.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
# 2022-09-23 v1.3 <[email protected]> fix exitcode on no sync and failed sync
# 2022-09-23 v1.4 <[email protected]> option -q is more quiet and -f to set SYNCAFTER
# 2023-07-21 v1.5 <[email protected]> fix typo in header
# 2024-01-23 v1.6 ahahn update help; use cw.emoji; update exitcodes
# ======================================================================

_version=1.5

_version=1.6
LOGDIR=/var/tmp/cronlogs
TARGET=
SSHKEY=
Expand All @@ -34,7 +34,7 @@ function showHead(){
cat <<ENDOFHEAD
____________________________________________________________________________________
SYNC LOCAL LOGS OF $( hostname -f )
AXELS CRONWRAPPER - SYNC LOCAL LOGS OF $( cw.emoji "🖥️" )$( hostname -f )
______________________________________________________________________________/ v$_version
ENDOFHEAD
Expand All @@ -44,34 +44,65 @@ function showHelp(){
showHead
local self=$( basename $0)
cat <<ENDOFHELP
HELP:
This script syncs local cronlogs to a target.
It should be used as cronjob in /etc/cron.d/ and/ or triggered
whem any cronwrapper script was fisnished.
SYNTAX:
$self [OPTIONS]
PRAMETERS:
-f [integer] time in sec when to force symc without new logs
value 0 forces sync
current value: [$SYNCAFTER]
-h show this help
-i [string] path to ssh private key file
current value: [$SSHKEY]
-l [string] local log dir of cronjobs
current value: [$LOGDIR]
-q be more quiet
-s [integer] sleep random time .. maximum is given value in seconds
-t [string] target dir (local or remote like rsync syntax)
current value: [$TARGET]
DEFAULTS:
see also ${CFGFILE}
EXAMPLES:
$self -s 20 -t [TARGET] wait max 20 sec before starting sync
$self -q -f 0 be more quiet and force sync (0 sec)
This script syncs local cronlogs to a target.
It should be used as cronjob in /etc/cron.d/ and/ or triggered
whem any cronwrapper script was fisnished.
..... $( cw.emoji "" )SYNTAX:
$self [OPTIONS]
..... $( cw.emoji "🔧" )OPTIONS:
-f [integer] time in sec when to force symc without new logs
value 0 forces sync
current value: [$SYNCAFTER]
-h show this help
-i [string] path to ssh private key file
current value:
[$SSHKEY]
-l [string] local log dir of cronjobs
current value:[$LOGDIR]
-q be more quiet
-s [integer] sleep random time .. maximum is given value in seconds
-t [string] target dir (local or remote like rsync syntax)
current value:
[$TARGET]
..... $( cw.emoji "🔷" )DEFAULTS:
see ${CFGFILE}
..... $( cw.emoji "🧩" )EXAMPLES:
$self -s 20 -t [TARGET]
Wait max. 20 sec before starting sync to a custom target
$self -q -f 0
be more quiet and force sync (0 sec)
..... $( cw.emoji "" )EXITCODES:
0 OK. Action ended as expected. No sync needed or sync was done.
1 Missing parameter
2 Invalid option
3 No FQDN was found in hostname
4 No target was set in configuration
5 Target is still example.com
6 Logdir with files to sync was not found
7 rsync of local logs to target failed
ENDOFHELP
}
Expand Down Expand Up @@ -115,30 +146,38 @@ do
*)
cw.cecho error "ERROR: $opt is unknown." >&2
showHelp
exit 1
exit 2
esac
done

test $VERBOSE -ne 0 && showHead

if ! hostname -f | grep "\." >/dev/null; then
test "$REQUIREFQDN" != "0" && cw.cecho error "ERROR: hostname [$( hostname -f )] is not a FQDN - there is no domain behind the host."
test "$REQUIREFQDN" != "0" && exit
test "$REQUIREFQDN" != "0" && exit 3
fi

if [ -z "$TARGET" ]; then
cw.cecho error ERROR: no target was set. use -t >&2
echo
showHelp
exit 2
exit 4
fi

if grep "example.com" <<< "$TARGET"; then
echo
echo "ABORT: target is 'example.com'. You need to modify the configuration"
echo " file ${CFGFILE} and set the target to your own system."
echo
exit 5
fi

if [ $VERBOSE -ne 0 ]; then
echo "----- local data in ${LOGDIR}" && ls -l "${LOGDIR}" || exit 3
echo "----- local data in ${LOGDIR}" && ls -l "${LOGDIR}" || exit 6
echo
echo "----- test for files to sync"
else
ls -l "${LOGDIR}" >/dev/null || exit 3
ls -l "${LOGDIR}" >/dev/null || exit 6
fi


Expand Down Expand Up @@ -173,7 +212,7 @@ then
else
echo "ERROR while syncing files. Next run will try to sync again."
rm -f "${LOGDIR}/$TOUCHFILE" 2>/dev/null
exit 2
exit 7
fi

# ----------------------------------------------------------------------
91 changes: 60 additions & 31 deletions cronstatus.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@
# 2023-07-14 ahahn 1.8 add support for REQUIREFQDN
# 2023-07-14 ahahn 1.9 added check if process still runs
# 2024-01-04 ahahn 1.10 update error messages
# 2024-01-17 ahahn 1.11 update help; use cw.emoji
# ------------------------------------------------------------

_version=1.9
_version=1.11

CW_LABELSTR=
CW_LOGFILE=
LOGDIR=/var/tmp/cronlogs

typeset -i REQUIREFQDN=0
# outfile=/tmp/cronjob_status.$$.tmp
# outfile=/tmp/cronjob_status.tmp
Expand All @@ -42,16 +46,21 @@ sPre=" "

# get a value from logfile (everything behind "=")
# param: label
# global: $logfile
# global: $CW_LOGFILE
function getLogValue(){
grep "^$1=" "$logfile" | cut -f 2- -d "="
grep "^$1=" "$CW_LOGFILE" | cut -f 2- -d "="
}

# get logfiles of all cronwrapper cronjobs
function getLogfiles(){
ls -1t "$LOGDIR"/*log | grep -Fv "/__"
}

# get logfiles of all cronwrapper cronjobs
function _getLabel(){
echo "$1" | rev | cut -f 1- -d '/' | rev | cut -f 2- -d '_' | sed "s,\.log,,"
}

# get logfiles of all cronwrapper cronjobs
function getRunningfiles(){
ls -1t "$LOGDIR"/*log.running* 2>/dev/null
Expand All @@ -62,35 +71,53 @@ function getRunningfiles(){
function showhelp(){
local _self="$( basename $0 )"
echo "
SYNTAX: $_self [OPTIONS|LOGFILE]
OPTIONS:
-h show this help and exit.
Show the status of all local cronjobs that use the cronwrapper or a single job
by giving its logfile as parameter.
..... $( cw.emoji "" )SYNTAX:
$_self [OPTIONS|LOGFILE]
..... $( cw.emoji "🔧" )OPTIONS:
-h show this help and exit.
PARAMETERS:
LOGFILE filename to show details of a single logfile
Default: without any logfile you get a total overview of all
cronjobs.
EXAMPLES:
$_self
show total overview over all jobs
$_self $LOGDIR/myjobfile.log
show output of a single job
..... $( cw.emoji "🏷️" )PARAMETERS:
LOGFILE filename to show details of a single logfile
Default: without any logfile you get a total overview of all
cronjobs.
..... $( cw.emoji "🧩" )EXAMPLES:
$_self
show total overview over all jobs
$_self $LOGDIR/myjobfile.log
show output of a single job
"
}

# show status of a single sob
# param string filename of cronwrapper logfile
# param string filename of cronwrapper logfile OR label of cronjob
# param bool flag: show logfile content; default: empty (=do not sohow log)
function showStatus(){
logfile="$1"
local _showlog="$2"
local _label="$1"
test -f "$_label" && CW_LOGFILE="$_label"
test -f "$_label" || CW_LOGFILE="$CW_LOGDIR/$( hostname -f )_${_label}.log"

CW_LABELSTR=$( _getLabel "$CW_LOGFILE")
local _showlog="$2"
echo
cw.cecho "head" "--- $logfile"
cw.cecho "head" "..... $( cw.emoji "📔" )$CW_LABELSTR"
echo
echo " Logfile : $CW_LOGFILE"
if [ -n "$_showlog" ]; then
cat "$logfile" \
cat "$CW_LOGFILE" \
| sed -e "s/^REM.*/$( printf "\033[0;36m&\033[0m" )/g" \
-e "s/^[A-Z]*/$( printf "\033[0;35m&\033[0m" )/g" \
-e "s/=/$( printf "\033[0;32m&\033[0m" )/g" \
Expand All @@ -100,8 +127,8 @@ function showStatus(){
fi
typeset -i iErr=0

# server=$(basename "$logfile" | cut -f 1 -d "_")
# jobname=$(basename "$logfile" | cut -f 2 -d "_" | sed "s#\.log##")
# server=$(basename "$CW_LOGFILE" | cut -f 1 -d "_")
# jobname=$(basename "$CW_LOGFILE" | cut -f 2 -d "_" | sed "s#\.log##")

sCmd=$(getLogValue SCRIPTNAME)
sLastStart=$(getLogValue SCRIPTSTARTTIME)
Expand All @@ -117,7 +144,7 @@ function showStatus(){
sTTL=$(getLogValue 'SCRIPTTTL')

# ----- check return code
sServer=$(basename "$logfile" | cut -f 1 -d "_")
sServer=$(basename "$CW_LOGFILE" | cut -f 1 -d "_")
sFqdnCheck=
sServerCheck=

Expand All @@ -126,7 +153,7 @@ function showStatus(){
iErr+=1
fi
if test "${sCurrentServer}" != "$sServer"; then
sServerCheck="WARNING : hostname -f returns [${sCurrentServer}] ... and differs to [$sServer] from logfile."
sServerCheck="WARNING : hostname -f returns [${sCurrentServer}] ... and differs to [$sServer] from CW_LOGFILE."
iErr+=1
fi

Expand Down Expand Up @@ -193,11 +220,12 @@ function showStatus(){


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

# show running jobs
Expand All @@ -212,7 +240,7 @@ function showRunningJobs(){
echo
echo "CURRENTLY RUNNING JOBS:"

for logfile in $( getRunningfiles )
for CW_LOGFILE in $( getRunningfiles )
do
sCmd=$(getLogValue SCRIPTNAME)
sLastStart=$(getLogValue SCRIPTSTARTTIME)
Expand All @@ -224,14 +252,14 @@ function showRunningJobs(){
fi

echo
cw.cecho "head" "${sPre}--- for $iSince min - $logfile"
cw.cecho "head" "${sPre}..... for $iSince min - $( cw.emoji "📔" )$CW_LOGFILE"
typeset -i iPid; iPid=$(getLogValue SCRIPTPROCESS)
if [ $iPid -gt 0 ]; then
# detect process id and check if it is still running
if ps $iPid >/dev/null 2>&1; then
cw.cecho "ok" "${sPre}${sPre}OK - still running"
else
cw.cecho "error" "${sPre}${sPre}ERROR : The process $iPid does not exist anymore."
cw.cecho "error" "${sPre}${sPre}ERROR : The process $iPid does not exist anymore. The job was aborted."
cw.cecho "error" "${sPre}${sPre} Check the log file and delete it."
iErr+=1
fi
Expand All @@ -240,6 +268,7 @@ function showRunningJobs(){
echo "${sPre}${sPre}command : ${sCmd}"
echo "${sPre}${sPre}last start: ${sLastStart}"
echo "${sPre}${sPre}ttl : ${iTTL} min"
echo
done
else
echo
Expand Down Expand Up @@ -269,8 +298,8 @@ sCurrentServer=$(hostname -f)
cat <<ENDOFHEAD
____________________________________________________________________________________
CRONJOBS on [$( hostname -f )]
______________________________________________________________________________/ v$_version
AXELS CRONWRAPPER - Jobstatus of cronjobs on $( cw.emoji "🖥️" )$( hostname -f )
_____________________________________________________________________________/ v$_version
ENDOFHEAD

if [ "$1" = "-h" ]; then
Expand Down
2 changes: 1 addition & 1 deletion cronwrapper.cfg.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# -----------------------------------------------------------------------------

# ----- shared values:
LOGDIR=/var/tmp/cronlogs
CW_LOGDIR=/var/tmp/cronlogs


# ----- for cronwrapper
Expand Down
Loading

0 comments on commit e0aed4e

Please sign in to comment.