Providing a tool to stitch and filter ONOS log files collected via onos-diagnostics tool.

Change-Id: Ib11a15cdc446662f46c376b51af7e5b0d999b605
diff --git a/tools/package/runtime/bin/onos-log-query b/tools/package/runtime/bin/onos-log-query
new file mode 100755
index 0000000..56ed544
--- /dev/null
+++ b/tools/package/runtime/bin/onos-log-query
@@ -0,0 +1,76 @@
+#!/bin/bash
+
+#
+# Copyright 2015-present Open Networking Foundation
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+# -----------------------------------------------------------------------------
+# Tool to stitch together ONOS logs collected via onos-diagniostics tool.
+# -----------------------------------------------------------------------------
+function usage {
+    echo "usage: onos-log-query [-n query-name] [-f from-time] [-t to-time] [-x] [node]" >&2
+    exit 1
+}
+
+name=query      # default name
+
+while getopts n:f:t:x?h o; do
+    case "$o" in
+        n) name="$OPTARG";;
+        f) from="$OPTARG";;
+        t) to="$OPTARG";;
+        x) nostt=1;;
+        *) usage;;
+    esac
+done
+
+let OPC=$OPTIND-1
+shift $OPC
+
+[ "$name" = "karaf" ] && echo "Name cannot be karaf" && usage
+
+function stitchLogs {
+    awk -v from="${from}" -v to="${to}" -v nostt=${nostt:-0} \
+        -v TS="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}" '
+        TS {
+            on = (from == "" || $1 >= from) && (to == "" || $1 <= to);
+            if (on) { print $0; next; }
+        }
+        { if (on && !nostt) { print $0; } }
+    ' FS='|' $(ls -r1 karaf.log*) > ${name}.log
+}
+
+function stitchNodeLogs {
+    pushd $1 &>/dev/null
+    stitchLogs
+    popd &>/dev/null
+}
+
+if [ $# -eq 1 -a -d "$1" ]; then
+    stitchNodeLogs $1 && cat $1/${name}.log
+elif [ $# -ge 1 ]; then
+    echo "No node directory for $1" >&2
+    usage
+elif ls karaf.log &>/dev/null; then
+    stitchLogs && cat ${name}.log
+elif ls */karaf.log &>/dev/null; then
+    for node in $(ls */karaf.log | cut -d/ -f1); do
+        stitchNodeLogs $node &
+    done
+    wait
+    awk '{ print FILENAME " | " $0; }' */${name}.log | \
+        sed "s:/${name}.log::" | sort -t\| -k2,2 > ${name}.log
+    cat ${name}.log
+fi