D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
usr
/
local
/
psa
/
admin
/
sbin
/
modules
/
log-browser
/
Filename :
journalctl
back
Copy
#!/bin/bash -e # Copyright 1999-2024. WebPros International GmbH. All rights reserved. usage() { cat <<-EOT Usage: journalctl [fetch|first] [OPTIONS] [journalctl-OPTIONS] Print journalctl logs with commands: fetch Print *all* lines matching the criteria first Print first line matching the criteria Print journalctl logs with OPTIONS: --grep PATTERN Search with PATTERN --lines COUNT Limit match count As this is designed as a wrapper on journalctl utility, other journalctl-OPTIONS are transparently supported. -h, --help Display this help and exit EOT exit 2 } is_journalctl_grep_flag_supported() { if { ! journalctl --grep= --lines=0 --no-pager --quiet 2>&1 >&3 3>&- | grep -q '^' >&2; } 3>&1; then echo -n 1 fi } main() { local command="$1" [ -n "$command" ] || usage shift local grep_pattern lines_count local args=() while [[ $# -gt 0 ]]; do case "$1" in --grep) grep_pattern="$2" shift 2 || usage ;; --grep=*) grep_pattern=${1#--grep=} shift ;; --lines) lines_count="$2" shift 2 || usage ;; --lines=*) lines_count=${1#--lines=} shift ;; -h|--help) usage ;; *) args+=("$1") shift ;; esac done [ -z "$grep_pattern" ] || local grep_flag_supported=$(is_journalctl_grep_flag_supported) [ "$PL_JOURNALCTL_GREP_UNSUPPORTED" != "1" ] || local grep_flag_supported= [ "$command" != "first" ] || lines_count= local journalctl_args=("--no-pager" "--quiet") local grep_args [ -z "$grep_pattern" ] || grep_pattern=$(echo -n "$grep_pattern" | sed 's;\(\[\|\]\);\\\1;g') if [ -n "$grep_flag_supported" ]; then [ -z "$grep_pattern" ] || journalctl_args+=("--grep=$grep_pattern") [ -z "$lines_count" ] || journalctl_args+=("--lines=$lines_count") else if [ -n "$grep_pattern" ]; then if [ -n "$lines_count" ]; then case "$lines_count" in \+*) lines_count=${lines_count##+} ;; \-*) lines_count=${lines_count##-} ;; esac fi # PROBLEM: We receive json text from journalctl which makes no sense to grep it literally but only the MESSAGE part # Also log message could have escaped quote characters within. Below is thought to be the fastest solution. # If the message include '"', it should already be '\"'; so grep_pattern should look for '\\"' to get literal '\' grep_pattern=$(echo -n "$grep_pattern" | sed 's;";\\\\";g') grep_pattern=$(echo -n "$grep_pattern" | sed 's;-;\\-;g') # We match first item by '"MESSAGE"' local pre='\s*"MESSAGE"\s*:\s*"' # We match termination by ', "' OR ' }' at the end, it should match due to JSON format local post='"(\s*,\s*"|\s*})' grep_args=("-Ei" "${pre}.*${grep_pattern}.*${post}") [ -z "$lines_count" ] || grep_args+=("-m" "$lines_count") else [ -z "$lines_count" ] || journalctl_args+=("--lines=$lines_count") fi fi [ "$command" != "first" ] || journalctl_args+=("--lines=+1") export SYSTEMD_COLORS=false case "$command" in fetch|first) if [ -n "$grep_flag_supported" ]; then journalctl "${args[@]}" "${journalctl_args[@]}" else if [ -z "$grep_args" ]; then journalctl "${args[@]}" "${journalctl_args[@]}" else journalctl "${args[@]}" "${journalctl_args[@]}" | grep "${grep_args[@]}" fi fi ;; *) echo "Unknown command: ${command}" >&2 usage ;; esac } main "$@"