127 lines
3.6 KiB
Bash
127 lines
3.6 KiB
Bash
#!/bin/sh
|
|
|
|
getTimeStamp() {
|
|
(date +%F" "%T)
|
|
}
|
|
|
|
# $1: message, $2: level, $3: operation, $4: code
|
|
writeJsonLog() {
|
|
PROGRAM_NAME="cfddns"
|
|
DEFAULT_CODE=99
|
|
DEFAULT_LEVEL="info"
|
|
DEFAULT_MESSAGE="An unspecified error has occurred"
|
|
DEFAULT_OPERATION="unspecified"
|
|
|
|
if [ -z "$1" ]; then
|
|
PROPS="$(
|
|
jq --null-input \
|
|
--arg level "err" \
|
|
--arg pName "$PROGRAM_NAME" \
|
|
--arg operation "$DEFAULT_OPERATION" \
|
|
--arg code "$DEFAULT_CODE" \
|
|
--arg message "$DEFAULT_MESSAGE" \
|
|
'{"level":$level, "programName":$pName, "operation":$operation, "code":$code, "message":$message}' \
|
|
)"
|
|
else
|
|
PROPS="$(
|
|
jq --null-input \
|
|
--arg level "${2:-$DEFAULT_LEVEL}" \
|
|
--arg pName "$PROGRAM_NAME" \
|
|
--arg operation "${3:-$DEFAULT_OPERATION}" \
|
|
--arg code "${4:-$DEFAULT_CODE}" \
|
|
--arg message "$1" \
|
|
'{"level":$level, "programName":$pName, "operation":$operation, "code":$code, "message":$message}' \
|
|
)"
|
|
fi
|
|
|
|
echo "$PROPS" | jq "."
|
|
return
|
|
}
|
|
|
|
# $1: message, $2: level, $3: operation, $4: code
|
|
writeJournalLog() {
|
|
PROGRAM_NAME="cfddns"
|
|
DEFAULT_CODE=99
|
|
DEFAULT_LEVEL="info"
|
|
DEFAULT_MESSAGE="An unspecified error has occurred"
|
|
DEFAULT_OPERATION="unspecified"
|
|
|
|
# translate error levels
|
|
if [ -z "$2" ]; then
|
|
LOG_LEVEL=1
|
|
else
|
|
case "$2" in
|
|
"emergency" | "emerg")
|
|
LOG_LEVEL=7
|
|
;;
|
|
"alert")
|
|
LOG_LEVEL=6
|
|
;;
|
|
"critical" | "crit")
|
|
LOG_LEVEL=5
|
|
;;
|
|
"error" | "err")
|
|
LOG_LEVEL=4
|
|
;;
|
|
"warning" | "warn")
|
|
LOG_LEVEL=3
|
|
;;
|
|
"notice")
|
|
LOG_LEVEL=2
|
|
;;
|
|
"info")
|
|
LOG_LEVEL=1
|
|
;;
|
|
"debug")
|
|
LOG_LEVEL=0
|
|
;;
|
|
esac
|
|
fi
|
|
|
|
# log the message
|
|
if [ -z "$1" ]; then
|
|
logger --journald <<end
|
|
MESSAGE_ID=$(systemd-id128 new)
|
|
SYSLOG_TIMESTAMP=$(getTimeStamp)
|
|
SYSLOG_FACILITY=3
|
|
SYSLOG_IDENTIFIER=${PROGRAM_NAME}
|
|
PRIORITY=4
|
|
ERRNO=${DEFAULT_CODE}
|
|
OPERATION=${DEFAULT_OPERATION}
|
|
MESSAGE=${DEFAULT_MESSAGE}
|
|
end
|
|
else
|
|
logger --journald <<end
|
|
MESSAGE_ID=$(systemd-id128 new)
|
|
SYSLOG_TIMESTAMP=$(getTimeStamp)
|
|
SYSLOG_FACILITY=3
|
|
SYSLOG_IDENTIFIER=${PROGRAM_NAME}
|
|
PRIORITY=${LOG_LEVEL}
|
|
ERRNO=${4:-$DEFAULT_CODE}
|
|
OPERATION=${3:-$DEFAULT_OPERATION}
|
|
MESSAGE=$1
|
|
end
|
|
fi
|
|
|
|
return
|
|
}
|
|
|
|
if [ "$1" = "json" ]; then
|
|
writeJsonLog # this will generate a generic error message
|
|
writeJsonLog "This is a test message" # generate this message at the default level with the default code
|
|
writeJsonLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
|
|
writeJsonLog "This is a test message" "error" "testing" # generate this message at the 'error' level, part of a 'test' operation, and with the default code
|
|
writeJsonLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
|
|
elif [ "$1" = "journal" ]; then
|
|
writeJournalLog # this will generate a generic error message
|
|
writeJournalLog "This is a test message" # generate this message at the default level with the default code
|
|
writeJournalLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
|
|
writeJournalLog "This is a test message" "error" "testing" # generate this message at the 'error' level as part of a 'test' operation, and with the default code
|
|
writeJournalLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
|
|
else
|
|
printf "\nPlease specify either 'json' or 'journal' after the script name. Exiting.\n\n"
|
|
exit 1
|
|
fi
|
|
|
|
exit 0
|