blob: 5f4537531415bbabace73d6a292f73f6dca2e017 [file] [log] [blame]
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -04001#!/bin/bash
2
3#
4# Copyright 2015-present Open Networking Foundation
5#
6# Licensed under the Apache License, Version 2.0 (the "License");
7# you may not use this file except in compliance with the License.
8# You may obtain a copy of the License at
9#
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing, software
13# distributed under the License is distributed on an "AS IS" BASIS,
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15# See the License for the specific language governing permissions and
16# limitations under the License.
17#
18
19# -----------------------------------------------------------------------------
20# Tool to stitch together ONOS logs collected via onos-diagniostics tool.
21# -----------------------------------------------------------------------------
22function usage {
23 echo "usage: onos-log-query [-n query-name] [-f from-time] [-t to-time] [-x] [node]" >&2
24 exit 1
25}
26
27name=query # default name
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040028onlyts=0
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040029
30while getopts n:f:t:x?h o; do
31 case "$o" in
32 n) name="$OPTARG";;
33 f) from="$OPTARG";;
34 t) to="$OPTARG";;
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040035 x) onlyts=1;;
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040036 *) usage;;
37 esac
38done
39
40let OPC=$OPTIND-1
41shift $OPC
42
43[ "$name" = "karaf" ] && echo "Name cannot be karaf" && usage
44
45function stitchLogs {
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040046 awk -v from="${from}" -v to="${to}" -v onlyts=${onlyts} \
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040047 -v TS="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}" '
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040048 BEGIN { on = 0; FS="|"; }
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040049 TS {
50 on = (from == "" || $1 >= from) && (to == "" || $1 <= to);
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040051 if (on) { print $0; }
52 next;
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040053 }
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040054 { if (on && !onlyts) { print $0; } }
55 ' $(ls -r1 karaf.log*) > ${name}.log
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040056}
57
58function stitchNodeLogs {
59 pushd $1 &>/dev/null
60 stitchLogs
61 popd &>/dev/null
62}
63
64if [ $# -eq 1 -a -d "$1" ]; then
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040065 stitchNodeLogs $1
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040066elif [ $# -ge 1 ]; then
67 echo "No node directory for $1" >&2
68 usage
69elif ls karaf.log &>/dev/null; then
Thomas Vachuskabf5d1fe2018-04-25 15:49:00 -040070 stitchLogs
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040071elif ls */karaf.log &>/dev/null; then
72 for node in $(ls */karaf.log | cut -d/ -f1); do
73 stitchNodeLogs $node &
74 done
75 wait
76 awk '{ print FILENAME " | " $0; }' */${name}.log | \
77 sed "s:/${name}.log::" | sort -t\| -k2,2 > ${name}.log
Thomas Vachuskafe7a75e2018-04-25 01:06:58 -040078fi