Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 1 | #!/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 | # ----------------------------------------------------------------------------- |
| 22 | function usage { |
| 23 | echo "usage: onos-log-query [-n query-name] [-f from-time] [-t to-time] [-x] [node]" >&2 |
| 24 | exit 1 |
| 25 | } |
| 26 | |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 27 | name=query # default name |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 28 | onlyts=0 |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 29 | |
| 30 | while getopts n:f:t:x?h o; do |
| 31 | case "$o" in |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 32 | x) onlyts=1;; |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 33 | n) name="$OPTARG";; |
| 34 | f) from="$OPTARG";; |
| 35 | t) to="$OPTARG";; |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 36 | *) usage;; |
| 37 | esac |
| 38 | done |
| 39 | |
| 40 | let OPC=$OPTIND-1 |
| 41 | shift $OPC |
| 42 | |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 43 | [ "$name" = "karaf" ] && echo "Name cannot be 'karaf'!" && usage |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 44 | |
| 45 | function stitchLogs { |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 46 | awk -v onlyts=${onlyts} -v from="${from}" -v to="${to}" ' |
| 47 | BEGIN { on = !onlyts && from == "" && to == ""; FS="|"; } |
| 48 | /^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}/ { |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 49 | on = (from == "" || $1 >= from) && (to == "" || $1 <= to); |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 50 | if (on) { print $0; } |
| 51 | next; |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 52 | } |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 53 | { if (on && !onlyts) { print $0; }} |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 54 | ' $(ls -r1 karaf.log*) > ${name}.log |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | function stitchNodeLogs { |
| 58 | pushd $1 &>/dev/null |
| 59 | stitchLogs |
| 60 | popd &>/dev/null |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 61 | echo ${1}/${name}.log |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if [ $# -eq 1 -a -d "$1" ]; then |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 65 | stitchNodeLogs $1 |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 66 | elif [ $# -ge 1 ]; then |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 67 | echo "No node directory for '$1'!" >&2 |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 68 | usage |
| 69 | elif ls karaf.log &>/dev/null; then |
Thomas Vachuska | bf5d1fe | 2018-04-25 15:49:00 -0400 | [diff] [blame] | 70 | stitchLogs |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 71 | elif 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 | \ |
Thomas Vachuska | 1b083cd | 2018-04-25 23:34:08 -0400 | [diff] [blame] | 77 | sed "s:/${name}.log::" | \ |
| 78 | egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}' | \ |
| 79 | sort -t\| -k2,2 > ${name}.log |
| 80 | echo ${name}.log |
Thomas Vachuska | fe7a75e | 2018-04-25 01:06:58 -0400 | [diff] [blame] | 81 | fi |