Compare commits
25 Commits
4c997a6b7d
...
92e69d1b85
| Author | SHA1 | Date | |
|---|---|---|---|
| 92e69d1b85 | |||
| 00ea88780d | |||
| d9aa8973a3 | |||
| ec75cffbf1 | |||
| b8fb744623 | |||
| d0b2c781bf | |||
| c4d28d1f8c | |||
| f085838781 | |||
| 4a6c339a07 | |||
| c94f12e976 | |||
| 5443612381 | |||
| df144a5211 | |||
| 04811805c3 | |||
| aa446c00e6 | |||
| 56a5ad59fe | |||
| ce1ab86d22 | |||
| cae57ccf6b | |||
| 0fcd0d9c13 | |||
| e411a35b93 | |||
| 0e787be456 | |||
| 17c33d237f | |||
| 5b55ac95a3 | |||
| f4dc8f44b1 | |||
| 3792881529 | |||
| c1f54a619c |
@@ -1,135 +1,160 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
getTimeStamp() {
|
getTimeStamp() {
|
||||||
(date +%F" "%T)
|
(date -u +"%Y-%m-%dT%H:%M:%SZ")
|
||||||
|
}
|
||||||
|
|
||||||
|
getHostname() {
|
||||||
|
(hostname -s)
|
||||||
|
}
|
||||||
|
|
||||||
|
lowercase() {
|
||||||
|
echo "$1" | tr '[:upper:]' '[:lower:]'
|
||||||
}
|
}
|
||||||
|
|
||||||
uppercase() {
|
uppercase() {
|
||||||
echo "$1" | tr '[:lower:] [:upper:]'
|
echo "$1" | tr '[:lower:]' '[:upper:]'
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: message, $2: level, $3: status, $4: operation, $5: code
|
standardizeLogLevels() {
|
||||||
writeJsonLog() {
|
LEVEL_TO_STANDARDIZE="$(lowercase "$1")"
|
||||||
PROGRAM_NAME="cfddns"
|
case "$LEVEL_TO_STANDARDIZE" in
|
||||||
DEFAULT_CODE=99
|
"critical" | "crit" | "fatal")
|
||||||
DEFAULT_LEVEL="info"
|
echo "CRIT"
|
||||||
DEFAULT_MESSAGE="An unspecified error has occurred"
|
;;
|
||||||
DEFAULT_OPERATION="unspecified"
|
"error" | "err")
|
||||||
DEFAULT_STATUS="ERR"
|
echo "ERR"
|
||||||
|
;;
|
||||||
|
"warning" | "warn")
|
||||||
|
echo "WARN"
|
||||||
|
;;
|
||||||
|
"information" | "info")
|
||||||
|
echo "INFO"
|
||||||
|
;;
|
||||||
|
"debug" | "verbose")
|
||||||
|
echo "DEBUG"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
echo "INFO"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
if [ -z "$1" ]; then
|
translateLogLevelsToJournalD() {
|
||||||
PROPS="$(
|
LEVEL_TO_TRANSLATE="$(standardizeLogLevels "$1")"
|
||||||
jq --null-input \
|
case "$LEVEL_TO_TRANSLATE" in
|
||||||
--arg level "err" \
|
"CRIT")
|
||||||
--arg pName "$PROGRAM_NAME" \
|
echo 2
|
||||||
--arg operation "$DEFAULT_OPERATION" \
|
;;
|
||||||
--arg status "$DEFAULT_STATUS" \
|
"ERR")
|
||||||
--arg code "$DEFAULT_CODE" \
|
echo 3
|
||||||
--arg message "$DEFAULT_MESSAGE" \
|
;;
|
||||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
"WARN")
|
||||||
)"
|
echo 4
|
||||||
else
|
;;
|
||||||
PROPS="$(
|
"INFO")
|
||||||
jq --null-input \
|
echo 6
|
||||||
--arg level "${2:-$DEFAULT_LEVEL}" \
|
;;
|
||||||
--arg pName "$PROGRAM_NAME" \
|
"DEBUG")
|
||||||
--arg status "$(uppercase "${3:-$DEFAULT_STATUS}")" \
|
echo 7
|
||||||
--arg operation "${4:-$DEFAULT_OPERATION}" \
|
;;
|
||||||
--arg code "${5:-$DEFAULT_CODE}" \
|
*)
|
||||||
--arg message "$1" \
|
echo 6
|
||||||
'{"level":$level, "programName":$pName, "status":$status, "operation":$operation, "code":$code, "message":$message}' \
|
esac
|
||||||
)"
|
}
|
||||||
|
|
||||||
|
# modified syslog fmt: <ISO-8601 timestamp> <hostname> <tag>[pid]: [LEVEL:$2] [STATUS:$3] <MESSAGE:$1> (<OPERATION:$4>:<CODE:$5>)
|
||||||
|
writePlainTextLog() {
|
||||||
|
# not enough information to generate a meaningful log message
|
||||||
|
if [ -z "$1" ] || [ $# -lt 3 ]; then
|
||||||
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
printf "%s %s %s[%s]: [%s] [%s] %s (%s:%s)\n" \
|
||||||
|
"$(getTimeStamp)" "$(getHostname)" "$LOG_PROGRAM_NAME" "$$" \
|
||||||
|
"$(standardizeLogLevels "$2")" "$(uppercase "$3")" "$1" "${4:-UNSPECIFIED}" "${5:-999}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# $1: message, $2: level, $3: status, [$4: operation], [$5: code]
|
||||||
|
writeJsonLog() {
|
||||||
|
# not enough information to generate a meaningful log message
|
||||||
|
if [ -z "$1" ] || [ $# -lt 3 ]; then
|
||||||
|
return
|
||||||
|
fi
|
||||||
|
|
||||||
|
PROPS="$(
|
||||||
|
jq --null-input \
|
||||||
|
--arg timestamp "$(getTimeStamp)" \
|
||||||
|
--arg hostname "$(getHostname)" \
|
||||||
|
--arg pName "$LOG_PROGRAM_NAME" \
|
||||||
|
--arg pid "$$" \
|
||||||
|
--arg level "$(standardizeLogLevels "$2")" \
|
||||||
|
--arg status "$(uppercase "$3")" \
|
||||||
|
--arg message "$1" \
|
||||||
|
--arg operation "${4:-UNSPECIFIED}" \
|
||||||
|
--arg code "${5:-999}" \
|
||||||
|
'{"timestamp":$timestamp, "hostname":$hostname, "programName":$pName, "pid":$pid, "level":$level, "status":$status, "message":$message, "operation":$operation, "code":$code}' \
|
||||||
|
)"
|
||||||
|
|
||||||
echo "$PROPS" | jq "."
|
echo "$PROPS" | jq "."
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: message, $2: level, $3: status, $4: operation, $5: code
|
# $1: message, $2: level, $3: status, $4: operation, $5: code
|
||||||
writeJournalLog() {
|
writeJournalDLog() {
|
||||||
PROGRAM_NAME="cfddns"
|
# not enough information to generate a meaningful log message
|
||||||
DEFAULT_CODE=99
|
if [ -z "$1" ] || [ $# -lt 3 ]; then
|
||||||
DEFAULT_LEVEL="info"
|
return
|
||||||
DEFAULT_MESSAGE="An unspecified error has occurred"
|
|
||||||
DEFAULT_OPERATION="unspecified"
|
|
||||||
DEFAULT_STATUS="ERR"
|
|
||||||
|
|
||||||
# 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
|
fi
|
||||||
|
|
||||||
# log the message
|
J_TIMESTAMP="$(getTimeStamp)"
|
||||||
if [ -z "$1" ]; then
|
J_PID="$$"
|
||||||
|
J_PRIO="$(translateLogLevelsToJournalD "$2")"
|
||||||
|
J_STAT="$(uppercase "$3")"
|
||||||
|
|
||||||
logger --journald <<end
|
logger --journald <<end
|
||||||
MESSAGE_ID="$(systemd-id128 new)"
|
SYSLOG_TIMESTAMP=${J_TIMESTAMP}
|
||||||
SYSLOG_TIMESTAMP="$(getTimeStamp)"
|
|
||||||
SYSLOG_FACILITY=3
|
SYSLOG_FACILITY=3
|
||||||
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
|
SYSLOG_IDENTIFIER=${LOG_PROGRAM_NAME}
|
||||||
PRIORITY=4
|
SYSLOG_PID=${J_PID}
|
||||||
ERRNO="${DEFAULT_CODE}"
|
PRIORITY=${J_PRIO}
|
||||||
OPERATION="${DEFAULT_OPERATION}"
|
STATUS=${J_STAT}
|
||||||
MESSAGE="${DEFAULT_MESSAGE}"
|
MESSAGE=$1
|
||||||
STATUS="${DEFAULT_STATUS}"
|
OPERATION=${4:-UNSPECIFIED}
|
||||||
|
ERRNO=${5:-999}
|
||||||
end
|
end
|
||||||
else
|
|
||||||
logger --journald <<end
|
|
||||||
MESSAGE_ID="$(systemd-id128 new)"
|
|
||||||
SYSLOG_TIMESTAMP="$(getTimeStamp)"
|
|
||||||
SYSLOG_FACILITY=3
|
|
||||||
SYSLOG_IDENTIFIER="${PROGRAM_NAME}"
|
|
||||||
PRIORITY="${LOG_LEVEL}"
|
|
||||||
ERRNO="${5:-$DEFAULT_CODE}"
|
|
||||||
OPERATION="${4:-$DEFAULT_OPERATION}"
|
|
||||||
MESSAGE="$1"
|
|
||||||
STATUS="${3:-$DEFAULT_STATUS}"
|
|
||||||
end
|
|
||||||
fi
|
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if [ "$1" = "json" ]; then
|
LOG_PROGRAM_NAME="cfddns"
|
||||||
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
|
if [ "$1" = "text" ]; then
|
||||||
writeJsonLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
|
writePlainTextLog # this will do nothing
|
||||||
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
|
writePlainTextLog "Testing plain-text logging output" # this will also do nothing
|
||||||
writeJsonLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
|
writePlainTextLog "Informational plain-text log message" "information" "ok" # info message
|
||||||
|
writePlainTextLog "Plain-text message forced to info level" "whoops" "ok" # level translated to default
|
||||||
|
writePlainTextLog "Warning! This is plain-text." "warning" "flagged" # warning level with 'flagged' status
|
||||||
|
writePlainTextLog "Error sample! This is plain-text." "err" "error" # error level message
|
||||||
|
writePlainTextLog "Unable to continue, critical failure. (This is plain-text)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
|
||||||
|
elif [ "$1" = "json" ]; then
|
||||||
|
writeJsonLog # this will do nothing
|
||||||
|
writeJsonLog "This is a test message" # this will also do nothing
|
||||||
|
writeJsonLog "Informational JSON log message" "information" "ok" # info message
|
||||||
|
writeJsonLog "JSON message forced to info level" "whoops" "ok" # level translated to default
|
||||||
|
writeJsonLog "Warning! This is JSON." "warning" "flagged" # warning level with 'flagged' status
|
||||||
|
writeJsonLog "Error sample! This is JSON." "err" "error" # error level message
|
||||||
|
writeJsonLog "Unable to continue, critical failure. (This is JSON)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
|
||||||
elif [ "$1" = "journal" ]; then
|
elif [ "$1" = "journal" ]; then
|
||||||
writeJournalLog # this will generate a generic error message
|
writeJournalDLog # this will do nothing
|
||||||
writeJournalLog "This is a test message" # generate this message at the default level with the default code
|
writeJournalDLog "This is a test message" # this will also do nothing
|
||||||
writeJournalLog "This is a test message" "warning" # generate this message at the 'warning' level with the default code
|
writeJournalDLog "Informational JOURNALD log message" "information" "ok" # info message
|
||||||
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
|
writeJournalDLog "JOURNALD message forced to info level" "whoops" "ok" # level translated to default
|
||||||
writeJournalLog "This is a test message" "alert" "testing" 10 # generate this message at the 'alert' level with all parameters set
|
writeJournalDLog "Warning! This is a JOURNALD message." "warning" "flagged" # warning level with 'flagged' status
|
||||||
|
writeJournalDLog "Error sample! This is a JOURNALD." "err" "error" # error level message
|
||||||
|
writeJournalDLog "Unable to continue, critical failure. (This is JOURNALD message)" "fatal" "exit" "startup" "2" # critical level error with operation and error code
|
||||||
else
|
else
|
||||||
printf "\nPlease specify either 'json' or 'journal' after the script name. Exiting.\n\n"
|
printf "\nPlease specify either 'text', 'json', or 'journal' after the script name. Exiting.\n\n"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user