Merge pull request #7 from darko-poljak/improve-logging

Improve logging: stdout, file, syslog options.
This commit is contained in:
Darko Poljak 2017-03-22 08:13:15 +01:00 committed by GitHub
commit 12b6b2cf28
4 changed files with 166 additions and 36 deletions

121
ccollect
View file

@ -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()
@ -194,9 +192,12 @@ ${__myname}: [args] <interval name> <sources to backup>
-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
@ -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]*$|^$'

16
doc/changes/2.0 Normal file
View file

@ -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)

View file

@ -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)

View file

@ -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