change and simplify logging functions

This commit is contained in:
Jakobus Schürz 2022-09-14 14:43:45 +02:00
parent bc9921bf17
commit 4cf1076197
9 changed files with 81 additions and 216 deletions

View file

@ -93,22 +93,22 @@ EOF
if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \ if [ $(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 ) \
-lt $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ]; then -lt $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ]; then
logwarn "sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) reached" logwarning "sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) reached"
logwarn "check if reachable" logwarning "check if reachable"
if $( timeout --foreground "${MSC_GIT_TIMEOUT-$MSC_GIT_TIMEOUT_DEFAULT}" $SGIT ls-remote >/dev/null 2>&1) ;then if $( timeout --foreground "${MSC_GIT_TIMEOUT-$MSC_GIT_TIMEOUT_DEFAULT}" $SGIT ls-remote >/dev/null 2>&1) ;then
logwarn "reachable" logwarning "reachable"
return 0 return 0
else else
logwarn "not reachable" logwarning "not reachable"
return 3 return 3
fi fi
else else
local seconds=$(( $( date --date="@$(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 )" "+%s") - $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") )) local seconds=$(( $( date --date="@$(stat --printf %Y ${MSC_GIT_UPD_REPO_STATFILE} 2>/dev/null || echo 0 )" "+%s") - $(date --date="${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours ago" "+%s") ))
logwarn "repo sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) not reached yet. $((seconds/86400)) days $(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds") left." logwarning "repo sync-intervall (${MSC_GIT_REPO_UPDATE_INTERVALL-${MSC_GIT_REPO_UPDATE_INTERVALL_DEFAULT}} hours) not reached yet. $((seconds/86400)) days $(date -d "1970-01-01 + $seconds seconds" "+%H hours %M minutes %S seconds") left."
return 2 return 2
fi fi
else else
logwarn "no sync allowed from config" >&2 logwarning "no sync allowed from config" >&2
return 1 return 1
fi fi
@ -136,10 +136,10 @@ EOF
[ -z "$nok" ] && mkdir -pv "${MSC_LOGDIR}" [ -z "$nok" ] && mkdir -pv "${MSC_LOGDIR}"
else else
logwarn "git not installed, no configuration possible, please install git" >&2 logwarning "git not installed, no configuration possible, please install git" >&2
fi fi
else else
logwarn "Not syncing profile in case of configuration" logwarning "Not syncing profile in case of configuration"
fi fi
} }

View file

@ -164,7 +164,7 @@ run_fix () {
MIMEDEST="${mimedestvid}/${subdestvid}" MIMEDEST="${mimedestvid}/${subdestvid}"
;; ;;
*) *)
logwarn "unsupportet mimetype »${mime}« for »${file}«" logwarning "unsupportet mimetype »${mime}« for »${file}«"
;; ;;
esac esac
if [ -n "${MIMEDEST+x}" ] ; then if [ -n "${MIMEDEST+x}" ] ; then
@ -179,7 +179,7 @@ run_fix () {
$setoriginalfilename && exiftool -overwrite_original "-originalfilename<filename" "$file" $setoriginalfilename && exiftool -overwrite_original "-originalfilename<filename" "$file"
;; ;;
*) *)
logwarn "do not set original filname in case of setoriginalfilename=$setoriginalfilename" logwarning "do not set original filname in case of setoriginalfilename=$setoriginalfilename"
;; ;;
esac esac
@ -246,10 +246,10 @@ run_fix () {
echo copy to ${dest}/${MIMEDEST}/%Y/%Y_%m echo copy to ${dest}/${MIMEDEST}/%Y/%Y_%m
if $movecreatedate ; then if $movecreatedate ; then
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})" loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})"
logwarn "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")" logwarning "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
else else
loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})" loginfo "$(exiftool -o . '-Directory<DateTimeOriginal' -d ${dest}/${MIMEDEST}/%Y/%Y_%m -if '($datetimeoriginal)' "$file" 2>&1 |tee -a ${LOGFILE})"
logwarn "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")" logwarning "$(readlink -f "$file"): $(exiftool -filename -if '(not ($datetimeoriginal or $createdate))' "$file")"
fi fi
;; ;;
renameinplace) renameinplace)
@ -279,7 +279,7 @@ run_fix () {
fi fi
;; ;;
*) *)
logwarn "value for move is neither »true« nor »copy«" logwarning "value for move is neither »true« nor »copy«"
esac esac
fi fi

View file

@ -91,8 +91,8 @@ fi
#echo $SGIT fetch ${MSC_GIT_REMOTE_NAME} -p 1>&2 #echo $SGIT fetch ${MSC_GIT_REMOTE_NAME} -p 1>&2
if $dorepoupdate; then if $dorepoupdate; then
loginfo "fetch repo${dosubmodupdate:+ and submodules}" loginfo "fetch repo${dosubmodupdate:+ and submodules}"
#$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; } #$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarning fetch failed; gitupdateend; exit 1; }
$SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarn fetch failed; gitupdateend; exit 1; } $SGIT fetch ${dosubmodupdate:+--recurse-submodules} ${MSC_GIT_REMOTE_NAME} -p 2>>"${MSC_LOGFILE}"|| { logwarning fetch failed; gitupdateend; exit 1; }
if ${dosubmodupdate:-false}; then if ${dosubmodupdate:-false}; then
lastupdatesubmodules lastupdatesubmodules
fi fi
@ -107,7 +107,7 @@ EOF
$SGIT merge FETCH_HEAD 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 3; } $SGIT merge FETCH_HEAD 1>>"${MSC_LOGFILE}" 2>>"${MSC_LOGFILE}"|| { gitupdateend; exit 3; }
touch $MSC_GIT_UPD_REPO_STATFILE touch $MSC_GIT_UPD_REPO_STATFILE
else else
logwarn -n " Lokale Änderungen festgestellt:" logwarning -n " Lokale Änderungen festgestellt:"
echo " Siehe Logfile ${MSC_LOGFILE}" >&2 echo " Siehe Logfile ${MSC_LOGFILE}" >&2
cat << EOF >> "${MSC_LOGFILE}" cat << EOF >> "${MSC_LOGFILE}"
um die Änderung zurückzusetzen bitte um die Änderung zurückzusetzen bitte
@ -137,7 +137,7 @@ EOF
logdebug "$($SGIT submodule update --remote)" logdebug "$($SGIT submodule update --remote)"
logdebug "$($SGIT commit -a -m 'update submodules')" logdebug "$($SGIT commit -a -m 'update submodules')"
touch $MSC_GIT_UPD_SUBMOD_STATFILE touch $MSC_GIT_UPD_SUBMOD_STATFILE
logwarn "submodules synced" logwarning "submodules synced"
fi fi
fi fi

View file

@ -93,6 +93,7 @@ EOF
while [ $i -lt ${#BACKENDS[@]} ];do while [ $i -lt ${#BACKENDS[@]} ];do
cat << EOF cat << EOF
====================================================
Action: $ACTION Action: $ACTION
Umgebung: $UMG Umgebung: $UMG
Cluster: $CLUSTER Cluster: $CLUSTER

View file

@ -1,13 +0,0 @@
#!/bin/sh
F="$(for i in $(find /var/log -maxdepth 1 -name "wtmp*"|sort -h);do echo -n "-f $i ";done)"
cat << EOF
-----
Alle Tage mit lokalen Logins
User: ${1:-$USER}
Host: $(hostname)
Files: $F
-----
EOF
last -w -F -a $F |grep -v "pts/\|reboot\|wtmp"|awk '$1 == u {print $4,$5,$7}' u=${1:-$USER}|uniq

View file

@ -3,7 +3,7 @@
FILELOGLEVEL=DEBUG FILELOGLEVEL=DEBUG
# loggerfactory # loggerfactory
LANG=C LANG=C
for f in logerror logwarn loginfo logdebug logtrace ENTRY EXIT SCRIPTENTRY SCRIPTEXIT; do for f in logerror logwarning loginfo logdebug logtrace ENTRY EXIT SCRIPTENTRY SCRIPTEXIT; do
if LANG=C type -t $f 2> /dev/null| grep -q 'function'; then if LANG=C type -t $f 2> /dev/null| grep -q 'function'; then
: :
else else
@ -198,7 +198,7 @@ agent-start-or-restart () {
;; ;;
1) 1)
logdebug "agent is running, but:" logdebug "agent is running, but:"
logwarn "$msg" logwarning "$msg"
;; ;;
2) 2)
logdebug "former agent is not running -> start it" logdebug "former agent is not running -> start it"
@ -222,7 +222,7 @@ $(ssh-runinagent $agentfile ssh-add -l)"
echo $agentfile echo $agentfile
ret=0 ret=0
else else
logwarn "ssh-identity »$ssh_identity« is not configured. Please create $identitydir and add keys" logwarning "ssh-identity »$ssh_identity« is not configured. Please create $identitydir and add keys"
ret=2 ret=2
fi fi
@ -240,7 +240,7 @@ $(ssh-runinagent $agentfile ssh-add -l)"
echo $agentfile echo $agentfile
ret=0 ret=0
else else
logwarn "no identity given -> exit" logwarning "no identity given -> exit"
ret=1 ret=1
fi fi
@ -318,15 +318,15 @@ agent-load-identity-keys () {
pubkeysonly+=($pubkey) pubkeysonly+=($pubkey)
fi fi
if [ -e "${pubkey%.pub}-cert.pub" ]; then if [ -e "${pubkey%.pub}-cert.pub" ]; then
#logwarn "${pubkey%.pub}-cert.pub: $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}'))" #logwarning "${pubkey%.pub}-cert.pub: $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}'))"
#logwarn "now: $(date +%s -d NOW)" #logwarning "now: $(date +%s -d NOW)"
cert_exp_date=$(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}') cert_exp_date=$(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}')
#[ $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}')) -gt $(date +%s -d NOW) ] \ #[ $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}')) -gt $(date +%s -d NOW) ] \
[ $(date +%s -d $cert_exp_date) -gt $(date +%s -d NOW) ] \ [ $(date +%s -d $cert_exp_date) -gt $(date +%s -d NOW) ] \
|| logwarn "CERTIFICATE IS NOT VALID ANYMORE: ${pubkey%.pub}-cert.pub" || logwarning "CERTIFICATE IS NOT VALID ANYMORE: ${pubkey%.pub}-cert.pub"
#[ $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}')) -lt $(date +%s -d "$SSH_CERT_VALIDITY_WARN_SEC") ] \ #[ $(date +%s -d $(ssh-keygen -L -f "${pubkey%.pub}-cert.pub"|awk '$1 == "Valid:"{print $5}')) -lt $(date +%s -d "$SSH_CERT_VALIDITY_WARN_SEC") ] \
[ $(date +%s -d $cert_exp_date) -lt $(date +%s -d "$SSH_CERT_VALIDITY_WARN_SEC") ] \ [ $(date +%s -d $cert_exp_date) -lt $(date +%s -d "$SSH_CERT_VALIDITY_WARN_SEC") ] \
&& logwarn "CERTIFICATE expires in $(echo "scale=0; ( `date -d $cert_exp_date +%s` - `date -d now +%s`) / (24*3600)" | bc -l) days: ${pubkey%.pub}-cert.pub" && logwarning "CERTIFICATE expires in $(echo "scale=0; ( `date -d $cert_exp_date +%s` - `date -d now +%s`) / (24*3600)" | bc -l) days: ${pubkey%.pub}-cert.pub"
fi fi
done done
loginfo "pubkeysonly: ${pubkeysonly[@]} (count: ${#pubkeysonly[*]})" loginfo "pubkeysonly: ${pubkeysonly[@]} (count: ${#pubkeysonly[*]})"
@ -364,7 +364,7 @@ agent-load-identity-keys () {
loginfo "currently loaded keys after action: $(ssh-runinagent $agentfile ssh-add -l|wc -l)" loginfo "currently loaded keys after action: $(ssh-runinagent $agentfile ssh-add -l|wc -l)"
logdebug "$(ssh-runinagent $agentfile ssh-add -l)" logdebug "$(ssh-runinagent $agentfile ssh-add -l)"
else else
logwarn "ssh-identity $ssh_identity is not configured. Please create $identitydir and add keys" logwarning "ssh-identity $ssh_identity is not configured. Please create $identitydir and add keys"
fi fi
fi fi
@ -380,12 +380,12 @@ function check-pubkeysonly () {
ssh-runinagent $agentfile ssh-add -T ${p} 2>&1 || { $remove || readd=true; break; } ssh-runinagent $agentfile ssh-add -T ${p} 2>&1 || { $remove || readd=true; break; }
done done
else else
logwarn "obviously there is no pubkey for the token in ${SSH_IDENTITIES_DIR}/${ssh_identity}/" logwarning "obviously there is no pubkey for the token in ${SSH_IDENTITIES_DIR}/${ssh_identity}/"
logwarn "you can add the pubkey with" logwarning "you can add the pubkey with"
logwarn " ssh-add -L > ${SSH_IDENTITIES_DIR}/${ssh_identity}/id_etoken.pub" logwarning " ssh-add -L > ${SSH_IDENTITIES_DIR}/${ssh_identity}/id_etoken.pub"
logwarn "make sure, only the token is loaded into ssh-agent with" logwarning "make sure, only the token is loaded into ssh-agent with"
logwarn " ssh-add -l" logwarning " ssh-add -l"
logwarn "only one line should be the output" logwarning "only one line should be the output"
fi fi
logdebug "readd: $readd" logdebug "readd: $readd"
} }
@ -405,7 +405,7 @@ ssh-runinagent () {
/bin/sh -c "unset SSH_AUTH_SOCK SSH_AGENT_PID; . $agentfile >/dev/null 2>/dev/null; $sshcommand" /bin/sh -c "unset SSH_AUTH_SOCK SSH_AGENT_PID; . $agentfile >/dev/null 2>/dev/null; $sshcommand"
ret=$? ret=$?
else else
logwarn "agentfile not existent" logwarning "agentfile not existent"
ret=99 ret=99
fi fi

View file

@ -1,7 +1,7 @@
#!/bin/bash #!/bin/bash
LANG=C LANG=C
for f in logerror logwarn loginfo logdebug logtrace ENTRY EXIT SCRIPTENTRY SCRIPTEXIT; do for f in logerror logwarning loginfo logdebug logtrace ENTRY EXIT SCRIPTENTRY SCRIPTEXIT; do
if LANG=C type -t $f 2> /dev/null| grep -q 'function'; then if LANG=C type -t $f 2> /dev/null| grep -q 'function'; then
: :
else else
@ -37,7 +37,7 @@ done
unset f unset f
logerror "ich bin ein error" logerror "ich bin ein error"
logwarn "ich bin eine warnung" logwarning "ich bin eine warnung"
loginfo "ich bin eine info" loginfo "ich bin eine info"
logdebug "ich bin eine debuginfo" logdebug "ich bin eine debuginfo"
logtrace "ich rede zuviel" logtrace "ich rede zuviel"

View file

@ -99,12 +99,12 @@ setproxy () {
local CONFIG local CONFIG
case $# in case $# in
0) 0)
logwarn "too few arguments" logwarning "too few arguments"
return return
;; ;;
*) *)
if [ -z ${SETPROXY_CREDS_DIRS+x} ] ; then if [ -z ${SETPROXY_CREDS_DIRS+x} ] ; then
logwarn "are you sure, SETPROXY_CREDS_DIRS is defined?" logwarning "are you sure, SETPROXY_CREDS_DIRS is defined?"
return 1 return 1
else else
CONFIG=$(find ${SETPROXY_CREDS_DIRS[*]} -mindepth 1 -name "$1.conf" -print -quit 2>/dev/null ) CONFIG=$(find ${SETPROXY_CREDS_DIRS[*]} -mindepth 1 -name "$1.conf" -print -quit 2>/dev/null )
@ -113,7 +113,7 @@ setproxy () {
;; ;;
esac esac
logwarn "CONFIG: ${CONFIG}" logwarning "CONFIG: ${CONFIG}"
if [ -e ${CONFIG} ]; then if [ -e ${CONFIG} ]; then
logdebug -n "${CONFIG} existing: " logdebug -n "${CONFIG} existing: "
@ -165,7 +165,7 @@ unsetproxy () {
mencfs () { mencfs () {
ENTRY ENTRY
[ $# -eq 0 ] && { logwarn "too few arguments" >&2; return 1; } [ $# -eq 0 ] && { logwarning "too few arguments" >&2; return 1; }
local PKEY local PKEY
local ENCDIR local ENCDIR
local DESTDIR local DESTDIR
@ -173,7 +173,7 @@ mencfs () {
local ENCFS=$(which encfs 2>/dev/null || exit 127 ) local ENCFS=$(which encfs 2>/dev/null || exit 127 )
local CONFIG local CONFIG
if [ -z ${ENCFS_CONFIG_DIRS+x} ] ; then if [ -z ${ENCFS_CONFIG_DIRS+x} ] ; then
logwarn "are you sure, ENCFS_CONFIG_DIRS is defined?" logwarning "are you sure, ENCFS_CONFIG_DIRS is defined?"
EXIT EXIT
return 1 return 1
else else
@ -225,7 +225,7 @@ uencfs () {
[ -z ${FUSERMOUNT+x} ] && return 127 [ -z ${FUSERMOUNT+x} ] && return 127
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
if [ ! -d ${1} ];then if [ ! -d ${1} ];then
logwarn "encrypted directory ${1} not found -> exit" >&2 logwarning "encrypted directory ${1} not found -> exit" >&2
EXIT EXIT
return 128 return 128
else else
@ -255,7 +255,7 @@ kinit-custom () {
local KINIT=$(which kinit 2>/dev/null || exit 127 ) local KINIT=$(which kinit 2>/dev/null || exit 127 )
local CONFIG local CONFIG
if [ -z ${KERBEROS_CONFIG_DIRS+x} ] ; then if [ -z ${KERBEROS_CONFIG_DIRS+x} ] ; then
logwarn "are you sure, KERBEROS_CONFIG_DIRS is defined?" logwarning "are you sure, KERBEROS_CONFIG_DIRS is defined?"
EXIT EXIT
return 1 return 1
else else
@ -267,7 +267,7 @@ kinit-custom () {
source "${CONFIG}" source "${CONFIG}"
logdebug "sourced" logdebug "sourced"
else else
logwarn "${CONFIG} not existing" logwarning "${CONFIG} not existing"
EXIT EXIT
return 2 return 2
fi fi
@ -283,7 +283,7 @@ kinit-custom () {
loginfo "Get kerberos-ticket for: $KERBEROS_USER@$REALM" loginfo "Get kerberos-ticket for: $KERBEROS_USER@$REALM"
if [ -z ${KERBEROS_USER:+x} ];then if [ -z ${KERBEROS_USER:+x} ];then
logwarn "no kerberos user found -> exit" logwarning "no kerberos user found -> exit"
EXIT EXIT
return 4 return 4
else else
@ -496,11 +496,11 @@ EOF
# ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash -c \"function bash () { /bin/bash --rcfile ${REMOTETMPCONFIGS[0]} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPCONFIGS[0]}\"" # ssh -t ${SSH_OPTS} $@ "$RCMD; SSHS=true bash -c \"function bash () { /bin/bash --rcfile ${REMOTETMPCONFIGS[0]} -i ; } ; export -f bash; exec bash --rcfile ${REMOTETMPCONFIGS[0]}\""
# rm "${TMPBASHCONFIG}" # rm "${TMPBASHCONFIG}"
# else # else
# logwarn "${TMPBASHCONFIG} does not exist. Using »ssh -t $@«" # logwarning "${TMPBASHCONFIG} does not exist. Using »ssh -t $@«"
# ssh -t "$@" # ssh -t "$@"
# fi # fi
# else # else
# logwarn "too few arguments for sshs" >&2 # logwarning "too few arguments for sshs" >&2
# ssh # ssh
# fi # fi
# #
@ -654,7 +654,7 @@ function tmuxx() {
SESS=($(find ${TMUX_SESSION_DIRS[*]} -mindepth 1 -name "$1.session" 2>/dev/null )) SESS=($(find ${TMUX_SESSION_DIRS[*]} -mindepth 1 -name "$1.session" 2>/dev/null ))
;; ;;
*) *)
logwarn no session specified return logwarning no session specified return
;; ;;
esac esac
TMUX_BIN='/usr/bin/tmux' TMUX_BIN='/usr/bin/tmux'
@ -691,7 +691,7 @@ EOF
which pdsh 1>/dev/null 2>&1 && pdsh -g vpn sudo systemctl status backup.service which pdsh 1>/dev/null 2>&1 && pdsh -g vpn sudo systemctl status backup.service
else else
logwarn "backup.vpn is not reachable -> exit" logwarning "backup.vpn is not reachable -> exit"
return 1 return 1
fi fi
@ -718,7 +718,7 @@ changebeep() {
style=audible style=audible
;; ;;
*) *)
logwarn "usage: changebeep [none|visible|audible]" logwarning "usage: changebeep [none|visible|audible]"
EXIT EXIT
return 1 return 1
;; ;;
@ -847,7 +847,7 @@ reachable () {
loginfo -n "Try to resolve $SERVER: " loginfo -n "Try to resolve $SERVER: "
local IP=$(getent $GETENTHOSTS $SERVER|awk '$0 ~ /STREAM/ {print $1}'|uniq|head -n1) local IP=$(getent $GETENTHOSTS $SERVER|awk '$0 ~ /STREAM/ {print $1}'|uniq|head -n1)
if [ -z ${IP-x} ]; then if [ -z ${IP-x} ]; then
logwarn "not resolvable -> exit" logwarning "not resolvable -> exit"
return 1 return 1
else else
loginfo $IP loginfo $IP

183
logging
View file

@ -6,168 +6,45 @@ mkdir -p "$(dirname ${SCRIPT_LOG})"
touch $SCRIPT_LOG touch $SCRIPT_LOG
function SCRIPTENTRY(){ #for level in emerg alert crit err warning notice info debug; do
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
timeAndDate=`date`
script_name=`basename -- "$0"`
script_name="${script_name%.*}"
echo "[$timeAndDate] [SCRIPTENTRY] > $script_name $FUNCNAME" >> $SCRIPT_LOG
fi
}
function SCRIPTEXIT(){ loglevels=(debug info notice warning err crit alert emerg)
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
script_name=`basename -- "$0"`
script_name="${script_name%.*}"
echo "[$timeAndDate] [SCRIPTEXIT] < $script_name $FUNCNAME" >> $SCRIPT_LOG
fi
}
function ENTRY(){ for level in SCRIPTENTRY SCRIPTEXIT ENTRY EXIT; do
printf -v functext -- '%s() {
local loglevels local loglevels
declare -a loglevels declare -a loglevels
loglevels=("ERROR" "WARN" "INFO") loglevels=(emerg alert crit err warning notice info)
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then timeAndDate=$(date)
local cfn="${FUNCNAME[1]}" script_name="${BASH_SOURCE[0]##*/}"
timeAndDate=`date` local LOGLEVEL=${LOGLEVEL,,}
echo "[$timeAndDate] [ENTRY] > $cfn $FUNCNAME" >> $SCRIPT_LOG if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
fi echo "[%s] »${script_name}« ######################################################" >&2
}
function EXIT(){
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
local cfn="${FUNCNAME[1]}"
timeAndDate=`date`
echo "[$timeAndDate] [EXIT] < $cfn $FUNCNAME" >> $SCRIPT_LOG
fi
}
function logerror(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=()
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[ERROR] (${FUNCNAME[1]}) $msg" >&2
fi fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [ERROR] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG echo "[${timeAndDate}] [%s] ${script_name}" >> "${SCRIPT_LOG}"
fi fi }' "${level}" "${level^^}" "${level^^}"
} eval "$functext"
export -f "${level}"
done
function logwarn(){ for level in trace debug info notice warning err crit alert emerg; do
local function_name="${FUNCNAME[1]}" loglevels=( ${loglevels[@]/$level} )
case $1 in printf -v functext -- 'log%s() {
-n) local msg="$@"
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0 [ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels local loglevels
declare -a loglevels declare -a loglevels
loglevels=("ERROR" ) loglevels=(%s)
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then timeAndDate=$(date)
echo $opts "[WARN] (${FUNCNAME[1]}) $msg" >&2 script_name="${BASH_SOURCE[0]##*/}"
local LOGLEVEL=${LOGLEVEL,,}
if [[ ! ${loglevels[@]} =~ "${LOGLEVEL:-${LOGLEVEL_DEFAULT,,}}" ]];then
printf "[%%s] (%%s) %%s\n" %s "${FUNCNAME[1]}" "${msg}" >&2
fi fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [WARN] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG printf "[%%s] [%%s] (%%s) %%s\n" "$timeAndDate" %s "${FUNCNAME[1]}" "${msg}" >> "${SCRIPT_LOG}"
fi fi }' "$level" "$(echo ${loglevels[@]})" "${level^^}" "${level^^}"
} eval "$functext"
export -f "log${level}"
function loginfo(){ done
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" )
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[INFO] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [INFO] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function logdebug(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[DEBUG] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [DEBUG] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
function logtrace(){
local function_name="${FUNCNAME[1]}"
case $1 in
-n)
local opts="-n"
shift
;;
esac
local msg="$1"
[ -z "${msg:+x}" ] && return 0
timeAndDate=`date`
local loglevels
declare -a loglevels
loglevels=("ERROR" "WARN" "INFO" "DEBUG")
if [[ ! ${loglevels[*]} =~ "${LOGLEVEL:-$LOGLEVEL_DEFAULT}" ]];then
echo $opts "[TRACE] (${FUNCNAME[1]}) $msg" >&2
fi
if [[ ! ${loglevels[*]} =~ "${FILELOGLEVEL:-$FILELOGLEVEL_DEFAULT}" ]];then
echo "[$timeAndDate] [TRACE] (${FUNCNAME[1]}) $msg" >> $SCRIPT_LOG
fi
}
export -f logerror
export -f logwarn
export -f loginfo
export -f logdebug
export -f logtrace
export -f SCRIPTENTRY
export -f ENTRY
export -f SCRIPTEXIT
export -f EXIT