From fbe17cae44a9b1236cc18c458e4ae16d6fcd8813 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Thu, 23 Feb 2017 20:04:53 +0100 Subject: [PATCH] Improve logging: stdout, file, syslog options. --- ccollect | 135 ++++++++++++++++++++++++++++++++++++------ doc/changes/2.0 | 16 +++++ doc/changes/next | 17 +----- doc/man/ccollect.text | 34 ++++++++++- 4 files changed, 166 insertions(+), 36 deletions(-) create mode 100644 doc/changes/2.0 diff --git a/ccollect b/ccollect index e1074b0..6369e84 100755 --- a/ccollect +++ b/ccollect @@ -127,6 +127,11 @@ fi PARALLEL="" MAX_JOBS="" USE_ALL="" +LOGFILE="" +SYSLOG="" +# e - only errors, a - all output +LOGLEVEL="a" +LOGONLYERRORS="" # # catch signals @@ -138,18 +143,11 @@ trap "${TRAPFUNC}" 1 2 15 # Functions # -# time displaying echo -_techo() +# check if we are running interactive or non-interactive +# see: http://www.tldp.org/LDP/abs/html/intandnonint.html +_is_interactive() { - echo "$(${DDATE}): $@" -} - -# exit on error -_exit_err() -{ - _techo "Error: $@" - rm -f "${TMP}" - exit 1 + [ -t 0 -o -p /dev/stdin ] } add_name() @@ -192,13 +190,16 @@ ${__myname}: [args] ccollect creates (pseudo) incremental backups - -h, --help: Show this help screen - -a, --all: Backup all sources specified in ${CSOURCES} - -j [max], --jobs [max] Specifies the number of jobs to run simultaneously. - If max is not specified then parallelise all jobs. - -p, --parallel: Parallelise backup processes (deprecated from 2.0) - -v, --verbose: Be very verbose (uses set -x) - -V, --version: Print version information + -h, --help: Show this help screen + -a, --all: Backup all sources specified in ${CSOURCES} + -e, --errors: Log only errors + -j [max], --jobs [max] Specifies the number of jobs to run simultaneously. + If max is not specified then parallelise all jobs. + -l FILE, --logfile FILE Log to specified file + -p, --parallel: Parallelise backup processes (deprecated from 2.0) + -s, --syslog: Log to syslog with tag ccollect + -v, --verbose: Be very verbose (uses set -x) + -V, --version: Print version information This is version ${VERSION} released on ${RELEASE}. @@ -219,6 +220,54 @@ unlock() "${unlockf}" "$@" } +# time displaying echo +# stdout version +_techo_stdout() +{ + echo "$(${DDATE}): $@" +} + +# syslog version +_techo_syslog() +{ + logger -t ccollect "$@" +} + +# specified file version +_techo_file() +{ + _techo_stdout "$@" >> "${LOGFILE}" +} + +# determine _techo version before parsing options +if _is_interactive +then + _techof="_techo_stdout" +else + _techof="_techo_syslog" +fi + +# _techo with determined _techo version +_techo() +{ + if [ "${LOGLEVEL}" = "a" ] + then + "${_techof}" "$@" + fi +} + +_techo_err() +{ + _techo "Error: $@" +} + +_exit_err() +{ + _techo_err "$@" + rm -f "${TMP}" + exit 1 +} + # # Parse options # @@ -247,6 +296,28 @@ while [ "$#" -ge 1 ]; do esac fi ;; + -e|--errors) + LOGONLYERRORS="1" + ;; + -l|--logfile) + if [ "$#" -ge 2 ] + then + case "$2" in + -*) + _exit_err "Missing log file" + ;; + *) + LOGFILE="$2" + shift + ;; + esac + else + _exit_err "Missing log file" + fi + ;; + -s|--syslog) + SYSLOG="1" + ;; -v|--verbose) set -x ;; @@ -268,6 +339,34 @@ while [ "$#" -ge 1 ]; do shift done +# determine _techo version and logging level after parsing options +if [ "${LOGFILE}" ] +then + _techof="_techo_file" + LOGLEVEL="a" +elif _is_interactive +then + if [ "${SYSLOG}" ] + then + _techof="_techo_syslog" + LOGLEVEL="a" + else + _techof="_techo_stdout" + LOGLEVEL="e" + fi +else + _techof="_techo_syslog" + LOGLEVEL="a" +fi + +if [ "${LOGFILE}" -o "${SYSLOG}" ] +then + if [ "${LOGONLYERRORS}" ] + then + LOGLEVEL="e" + fi +fi + # check that MAX_JOBS is natural number > 0 # empty string means run all in parallel echo "${MAX_JOBS}" | grep -q -E '^[1-9][0-9]*$|^$' diff --git a/doc/changes/2.0 b/doc/changes/2.0 new file mode 100644 index 0000000..2fee139 --- /dev/null +++ b/doc/changes/2.0 @@ -0,0 +1,16 @@ + * Introduce -j option for max parallel jobs, deprecate -p (Darko Poljak) + * Add locking (Darko Poljak) + * Fix source-is-up check (Nikita Koshikov) + * Fix some minor command line parsing issues (Nico Schottelius) + * Correct output, if configuration is not in cconfig format (Nico Schottelius) + * Minor code cleanups and optimisations (Nico Schottelius) + * ccollect_analyse_logs.sh traps more errors and warnings (Patrick Drolet) + * Remove -v for mkdir and rm, as they are not POSIX (Patrick Drolet) + * Export destination_* to source specific post_exec (Nico Schottelius) + * Update documentation regarding exported variables (Nico Schottelius) + * Simplify time calculation (Nico Schottelius) + * Documentate pre_exec error handling (Nico Schottelius) + * Added start script (Thorsten Elle) + * Documentate autofs hint (Nico Schottelius) + * Speedup source-is-up check and remove --archive (Nico Schottelius) + * Removed support for remote backup (see doc) (Nico Schottelius) diff --git a/doc/changes/next b/doc/changes/next index 2fee139..ec42d4b 100644 --- a/doc/changes/next +++ b/doc/changes/next @@ -1,16 +1 @@ - * Introduce -j option for max parallel jobs, deprecate -p (Darko Poljak) - * Add locking (Darko Poljak) - * Fix source-is-up check (Nikita Koshikov) - * Fix some minor command line parsing issues (Nico Schottelius) - * Correct output, if configuration is not in cconfig format (Nico Schottelius) - * Minor code cleanups and optimisations (Nico Schottelius) - * ccollect_analyse_logs.sh traps more errors and warnings (Patrick Drolet) - * Remove -v for mkdir and rm, as they are not POSIX (Patrick Drolet) - * Export destination_* to source specific post_exec (Nico Schottelius) - * Update documentation regarding exported variables (Nico Schottelius) - * Simplify time calculation (Nico Schottelius) - * Documentate pre_exec error handling (Nico Schottelius) - * Added start script (Thorsten Elle) - * Documentate autofs hint (Nico Schottelius) - * Speedup source-is-up check and remove --archive (Nico Schottelius) - * Removed support for remote backup (see doc) (Nico Schottelius) +* Add options for stdout, file and syslog logging (Darko Poljak) diff --git a/doc/man/ccollect.text b/doc/man/ccollect.text index 9fc9086..67f8f47 100644 --- a/doc/man/ccollect.text +++ b/doc/man/ccollect.text @@ -29,6 +29,9 @@ OPTIONS -a, --all:: Backup all sources specified in /etc/ccollect/sources +-e, --errors:: + Log only errors + -h, --help:: Show the help screen @@ -36,14 +39,41 @@ OPTIONS Specifies the number of jobs to run simultaneously. If max is not specified then parallelise all jobs. +-l FILE, --logfile FILE:: + Log to specified file + -p, --parallel:: Parallelise backup processes (deprecated from 2.0) +-s, --syslog:: + Log to syslog with tag ccollect + +-V, --version:: + Show version and exit + -v, --verbose:: Be very verbose (uses set -x) --V, --version:: - Show version and exit + +LOGGING MECHANISM +----------------- +ccollect logging depends on running in non-interactive/interactive mode +and on specified optins. The mechanism behaves as the following: + +non-interactive mode:: + + * standard output goes to syslog + * optional: specify logging into file + * log all output by default + * optional: log only errors + +interactive mode:: + + * standard output goes to stdout + * log only errors + * optional: log into syslog or file + - log all output by default + - optional: log only errors SEE ALSO