Blob Blame History Raw
From 6a387a3a9d150ff972e6842e07a96a288eda4471 Mon Sep 17 00:00:00 2001
From: Ken Gaillot <kgaillot@redhat.com>
Date: Wed, 16 Aug 2017 20:37:36 -0500
Subject: [PATCH] Fix: tools: allow crm_report to work with no log files
 specified

582e886d broke crm_report --sos-mode

Specifying no log files is a valid use case: someone might want to collect only
the other information (cluster configuration, policy engine inputs, etc.);
or someone might use only the systemd journal and no log files.
---
 tools/report.collector | 143 ++++++++++++++++++++++++++++++++-----------------
 1 file changed, 93 insertions(+), 50 deletions(-)

diff --git a/tools/report.collector b/tools/report.collector
index 0130a2b..d7c7c51 100644
--- a/tools/report.collector
+++ b/tools/report.collector
@@ -693,6 +693,85 @@ EOF
     esac
 }
 
+# Trim leading and ending whitespace (using only POSIX expressions)
+trim() {
+    TRIM_S="$1"
+
+    TRIM_S="${TRIM_S#"${TRIM_S%%[![:space:]]*}"}"
+    TRIM_S="${TRIM_S%"${TRIM_S##*[![:space:]]}"}"
+    echo -n "$TRIM_S"
+}
+
+collect_logs() {
+    CL_START="$1"
+    shift
+    CL_END="$1"
+    shift
+    CL_LOGFILES="$@"
+
+    which journalctl > /dev/null 2>&1
+    if [ $? -eq 0 ]; then
+        cl_have_journald=1
+    else
+        cl_have_journald=0
+    fi
+
+    cl_lognames="$CL_LOGFILES"
+    if [ $cl_have_journald -eq 1 ]; then
+        cl_lognames="$cl_lognames journalctl"
+    fi
+    cl_lognames=$(trim "$cl_lognames")
+    if [ -z "$cl_lognames" ]; then
+        return
+    fi
+
+    # YYYY-MM-DD HH:MM:SS
+    cl_start_ymd=$(date -d @${CL_START} +"%F %T")
+    cl_end_ymd=$(date -d @${CL_END} +"%F %T")
+
+    debug "Gathering logs from $cl_start_ymd to $cl_end_ymd:"
+    debug "   $cl_lognames"
+
+    # Remove our temporary file if we get interrupted here
+    trap '[ -z "$cl_pattfile" ] || rm -f "$cl_pattfile"' 0
+
+    # Create a temporary file with patterns to grep for
+    cl_pattfile=$(mktemp) || fatal "cannot create temporary files"
+    for cl_pattern in $LOG_PATTERNS; do
+        echo "$cl_pattern"
+    done > $cl_pattfile
+
+    echo "Log pattern matches from $REPORT_TARGET:" > $ANALYSIS_F
+    if [ -n "$CL_LOGFILES" ]; then
+        for cl_logfile in $CL_LOGFILES; do
+            cl_extract="$(basename $cl_logfile).extract.txt"
+
+            if [ ! -f "$cl_logfile" ]; then
+                # Not a file
+                continue
+
+            elif [ -f "$cl_extract" ]; then
+                # We already have it
+                continue
+            fi
+
+            dumplogset "$cl_logfile" $LOG_START $LOG_END > "$cl_extract"
+            sanitize "$cl_extract"
+
+            grep -f "$cl_pattfile" "$cl_extract" >> $ANALYSIS_F
+        done
+    fi
+
+    # Collect systemd logs if present
+    if [ $cl_have_journald -eq 1 ]; then
+        journalctl --since "$cl_start_ymd" --until "$cl_end_ymd" > journal.log
+        grep -f "$cl_pattfile" journal.log >> $ANALYSIS_F
+    fi
+
+    rm -f $cl_pattfile
+    trap "" 0
+}
+
 debug "Initializing $REPORT_TARGET subdir"
 if [ "$REPORT_MASTER" != "$REPORT_TARGET" ]; then
   if [ -e $REPORT_HOME/$REPORT_TARGET ]; then
@@ -718,17 +797,19 @@ if [ -z "$cluster_cf" ] && [ $cluster != "any" ]; then
    warning "Could not determine the location of your cluster configuration"
 fi
 
-if [ $SEARCH_LOGS = 1 ]; then
-    logfiles=`get_logfiles $cluster "$cluster_cf" "$logd_cf" | sort -u`
-    if [ -z "$logfiles" ]; then
-	fatal "Logfile discovery disabled, try specifying --logfile /some/path"
-    fi
-
-elif [ -z "$EXTRA_LOGS" ]; then
-   fatal "Could not determine the location of your cluster logs, try specifying --logfile /some/path"
+if [ "$SEARCH_LOGS" = "1" ]; then
+    logfiles=$(get_logfiles "$cluster" "$cluster_cf" "$logd_cf" | sort -u)
+fi
+logfiles="$(trim "$logfiles $EXTRA_LOGS")"
 
-else
-    logfiles="$EXTRA_LOGS"
+if [ -z "$logfiles" ]; then
+    which journalctl > /dev/null 2>&1
+    if [ $? -eq 0 ]; then
+        info "Systemd journal will be only log collected"
+    else
+        info "No logs will be collected"
+    fi
+    info "No log files found or specified with --logfile /some/path"
 fi
 
 debug "Config: $cluster ($cluster_cf $logd_cf) $logfiles"
@@ -783,45 +864,7 @@ done
 # in it (AFTER sanitizing, so we don't need to sanitize this output)
 get_readable_cib "$REPORT_HOME/$REPORT_TARGET"
 
-# Grab logs
-start=`date -d @${LOG_START} +"%F %T"`
-end=`date -d @${LOG_END} +"%F %T"`
-
-debug "Gathering logs from $start to $end: $logfiles $EXTRA_LOGS"
-trap '[ -z "$pattfile" ] || rm -f "$pattfile"' 0
-pattfile=`mktemp` || fatal "cannot create temporary files"
-for p in $LOG_PATTERNS; do
-    echo "$p"
-done > $pattfile
-
-for l in $logfiles $EXTRA_LOGS; do
-    b="$(basename $l).extract.txt"
-
-    if [ ! -f "$l" ]; then
-	# Not a file
-	continue
-
-    elif [ -f "$b" ]; then
-	# We already have it
-	continue
-    fi
-
-    dumplogset "$l" $LOG_START $LOG_END > "$b"
-    sanitize "$b"
-
-    echo "Log patterns $REPORT_TARGET:"  > $ANALYSIS_F
-    grep -f "$pattfile" "$b" >> $ANALYSIS_F
-done
-
-which journalctl > /dev/null 2>&1
-if [ $? = 0 ]; then
-   log "Including segment [$LOG_START-$LOG_END] from journald"
-   journalctl --since "$start" --until "$end" > journal.log
-   cat journal.log | grep -f $pattfile >> $ANALYSIS_F
-fi
-
-rm -f $pattfile
-trap "" 0
+collect_logs "$LOG_START" "$LOG_END" $logfiles
 
 # Purge files containing no information
 for f in `ls -1`; do
@@ -838,7 +881,7 @@ for f in `ls -1`; do
 done
 
 # Parse for events
-for l in $logfiles $EXTRA_LOGS; do
+for l in $logfiles; do
     b="$(basename $l).extract.txt"
     node_events "$b" > $EVENTS_F
 
-- 
1.8.3.1