Thomas Vachuska | d3ba0c3 | 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 | |
| 27 | name=query # default name |
| 28 | |
| 29 | while getopts n:f:t:x?h o; do |
| 30 | case "$o" in |
| 31 | n) name="$OPTARG";; |
| 32 | f) from="$OPTARG";; |
| 33 | t) to="$OPTARG";; |
| 34 | x) nostt=1;; |
| 35 | *) usage;; |
| 36 | esac |
| 37 | done |
| 38 | |
| 39 | let OPC=$OPTIND-1 |
| 40 | shift $OPC |
| 41 | |
| 42 | [ "$name" = "karaf" ] && echo "Name cannot be karaf" && usage |
| 43 | |
| 44 | function stitchLogs { |
| 45 | awk -v from="${from}" -v to="${to}" -v nostt=${nostt:-0} \ |
| 46 | -v TS="[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3}" ' |
| 47 | TS { |
| 48 | on = (from == "" || $1 >= from) && (to == "" || $1 <= to); |
| 49 | if (on) { print $0; next; } |
| 50 | } |
| 51 | { if (on && !nostt) { print $0; } } |
| 52 | ' FS='|' $(ls -r1 karaf.log*) > ${name}.log |
| 53 | } |
| 54 | |
| 55 | function stitchNodeLogs { |
| 56 | pushd $1 &>/dev/null |
| 57 | stitchLogs |
| 58 | popd &>/dev/null |
| 59 | } |
| 60 | |
| 61 | if [ $# -eq 1 -a -d "$1" ]; then |
| 62 | stitchNodeLogs $1 && cat $1/${name}.log |
| 63 | elif [ $# -ge 1 ]; then |
| 64 | echo "No node directory for $1" >&2 |
| 65 | usage |
| 66 | elif ls karaf.log &>/dev/null; then |
| 67 | stitchLogs && cat ${name}.log |
| 68 | elif ls */karaf.log &>/dev/null; then |
| 69 | for node in $(ls */karaf.log | cut -d/ -f1); do |
| 70 | stitchNodeLogs $node & |
| 71 | done |
| 72 | wait |
| 73 | awk '{ print FILENAME " | " $0; }' */${name}.log | \ |
| 74 | sed "s:/${name}.log::" | sort -t\| -k2,2 > ${name}.log |
| 75 | cat ${name}.log |
| 76 | fi |