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