blob: ec80974d3f14b404df214c32e5780d4113a7e97c [file] [log] [blame]
Thomas Vachuska15370d22018-01-30 13:57:38 -08001#!/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 collect cluster-wide diagnostics into a single tar stream.
21# -----------------------------------------------------------------------------
22function usage() {
Seyeon Jeong357bcec2020-02-28 01:17:34 -080023 echo "usage: $(basename $0) [-x] [-j] [-n name] [-P port] [-u user] [-p password] [ip1 ip2...]"
Thomas Vachuska15370d22018-01-30 13:57:38 -080024 echo ""
25 echo "Environment Variables:"
pier1fece882020-06-12 18:48:37 +020026 echo " DIAGS_PROFILE Profile to be used to collect diags."
27 echo " Availables profiles in onos-diagnostics-profile"
Thomas Vachuska15370d22018-01-30 13:57:38 -080028 echo " ONOS_INSTANCES IPs or hostnames of ONOS cluster machines"
29 echo " ONOS_WEB_USER username for REST API"
30 echo " ONOS_WEB_PASS password for REST API"
31 echo ""
32 echo "Example Usages:"
33 echo " # Collect compressed diagnostics for the cluster."
34 echo " # REST API user and password are drawn from environment variables."
35 echo " # Collection archive will be named /tmp/onos-diags.tar.gz"
36 echo " # The cluster node IPs will be drawn from ONOS_INSTANCES variable."
pier1fece882020-06-12 18:48:37 +020037 echo " # Diags profile is drawn from environment variable."
Thomas Vachuska15370d22018-01-30 13:57:38 -080038 echo " > $(basename $0) "
39 echo ""
40 echo " # Collect diagnostics for the cluster and leave them extracted. "
41 echo " # Collection directory will be named /tmp/prague-diags/"
42 echo " # Collection archive will be named /tmp/prague-diags.tar.gz."
43 echo " # REST API user name is 'onos' and password is 'rules'."
44 echo " # The cluster node IPs will be drawn from ONOS_INSTANCES variable."
45 echo " > $(basename $0) -x -n prague -u onos -p rules"
46 echo ""
Seyeon Jeong357bcec2020-02-28 01:17:34 -080047 echo " # Collect diagnostics for the cluster and store them in JSON files."
48 echo " # JSON_CLI_COMMANDS below lists JSON-supported diagnostics commands."
49 echo " # Collection archive will be named /tmp/onos-diags.tar.gz"
50 echo " > $(basename $0) -j"
51 echo ""
Thomas Vachuska15370d22018-01-30 13:57:38 -080052 echo " # Collect compressed diagnostics for a cluster."
53 echo " # REST API user name is 'onos' and password is 'rules'."
54 echo " # Collection archive will be named /tmp/onos-diags.tar.gz"
55 echo " # The cluster node IPs are listed explicitly."
56 echo " > $(basename $0) -u onos -p rules 172.17.0.11 172.17.0.12 172.17.0.13"
57
58 exit 1
59}
60
pier1fece882020-06-12 18:48:37 +020061# Let's source the different profiles
62. onos-diagnostics-profile
63
64# By default the ONOS base profile will be used
65[ -z "$DIAGS_PROFILE" ] && DIAGS_PROFILE=ONOS_PROFILE;
66
Thomas Vachuskaa7be50d2018-04-16 14:02:18 -070067ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos'
68ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks'
69ONOS_WEB_PORT=${ONOS_WEB_PORT:-8181} # REST API port defaults to '8181'
70
71. $(dirname $0)/_find-node
72
Thomas Vachuskaa7be50d2018-04-16 14:02:18 -070073port=${ONOS_WEB_PORT}
74user=${ONOS_WEB_USER}
75password=${ONOS_WEB_PASS}
76
Thomas Vachuska15370d22018-01-30 13:57:38 -080077# Scan arguments for user/password or other options...
Seyeon Jeong357bcec2020-02-28 01:17:34 -080078while getopts n:P:u:p:x?j?h o; do
Thomas Vachuska15370d22018-01-30 13:57:38 -080079 case "$o" in
80 n) name=$OPTARG;;
Thomas Vachuskaa7be50d2018-04-16 14:02:18 -070081 P) port=$OPTARG;;
Thomas Vachuska15370d22018-01-30 13:57:38 -080082 u) user=$OPTARG;;
83 p) password=$OPTARG;;
84 x) extract=true;;
Seyeon Jeong357bcec2020-02-28 01:17:34 -080085 j) json=true;;
Thomas Vachuska15370d22018-01-30 13:57:38 -080086 *) usage;;
87 esac
88done
Thomas Vachuska7f2a3562018-02-28 10:02:16 -080089
Thomas Vachuska15370d22018-01-30 13:57:38 -080090let OPC=$OPTIND-1
91shift $OPC
92
93[ $# -lt 1 -a -z "$ONOS_INSTANCES" ] && usage;
94
95diags=/tmp/${name:-onos}-diags
96rm -fr $diags $diags.tar.gz; mkdir -p $diags
97
98[ -z $1 ] && nodes=$ONOS_INSTANCES || nodes=$*
99
pier1fece882020-06-12 18:48:37 +0200100# -j option will activate T3_OFFLINE_PROFILE
101[ ! -z $json ] && DIAGS_PROFILE=T3_OFFLINE_PROFILE
102
Thomas Vachuska15370d22018-01-30 13:57:38 -0800103# Collect diagnostics from each cluster node
104for node in $nodes; do
105 printf "Collecting diagnostics on $node..."
106
107 # Prepare a clean place for collecting the node diagnostic data
108 cd $diags; rm -fr $node; mkdir -p $node; cd $node;
109
pier1fece882020-06-12 18:48:37 +0200110 if [ -z $json ]; then
111 # Acquire locally obtained diagnostics via REST API and extract them
112 printf "logs "
113 curl -sS --fail --user $user:$password \
114 http://$node:$port/onos/v1/diagnostics > ../$node.tar.gz
115 tar zxf ../$node.tar.gz && rm ../$node.tar.gz
116 fi
Thomas Vachuska15370d22018-01-30 13:57:38 -0800117
118 # Acquire remotely obtained diagnostics via ssh CLI
pier1fece882020-06-12 18:48:37 +0200119 eval CLI_COMMANDS=\${${DIAGS_PROFILE}[@]}
120 for cmd in $CLI_COMMANDS; do
121 # @ is used as workaround for the array expansion
122 cmd="$(echo $cmd | sed 's/@/ /g')"
Thomas Vachuska15370d22018-01-30 13:57:38 -0800123 cmdLog="$(echo $cmd | cut -d\ -f1 | sed 's/:/-/g').txt"
Seyeon Jeong357bcec2020-02-28 01:17:34 -0800124 [ ! -z $json ] && cmdLog="${cmdLog%.txt}.json"
Thomas Vachuska15370d22018-01-30 13:57:38 -0800125 printf "$cmdLog "
126 onos $node $cmd 2>/dev/null >$cmdLog
127 done
128
Thomas Vachuska15370d22018-01-30 13:57:38 -0800129 printf " Done.\n"
Thomas Vachuska15370d22018-01-30 13:57:38 -0800130done
131
132# Tar-up diagnostics from all the nodes
133cd $diags
134tar zcf $diags.tar.gz *
135[ -z $extract ] && rm -fr $diags