Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [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 collect cluster-wide diagnostics into a single tar stream. |
| 21 | # ----------------------------------------------------------------------------- |
| 22 | function usage() { |
Seyeon Jeong | be3fa0d | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 23 | echo "usage: $(basename $0) [-x] [-j] [-n name] [-P port] [-u user] [-p password] [ip1 ip2...]" |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 24 | echo "" |
| 25 | echo "Environment Variables:" |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 26 | echo " DIAGS_PROFILE Profile to be used to collect diags." |
| 27 | echo " Availables profiles in onos-diagnostics-profile" |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 28 | 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." |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 37 | echo " # Diags profile is drawn from environment variable." |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 38 | 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 Jeong | be3fa0d | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 47 | 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 Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 52 | 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 | |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 61 | # 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 Vachuska | a7be50d | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 67 | ONOS_WEB_USER=${ONOS_WEB_USER:-onos} # ONOS WEB User defaults to 'onos' |
| 68 | ONOS_WEB_PASS=${ONOS_WEB_PASS:-rocks} # ONOS WEB Password defaults to 'rocks' |
| 69 | ONOS_WEB_PORT=${ONOS_WEB_PORT:-8181} # REST API port defaults to '8181' |
| 70 | |
| 71 | . $(dirname $0)/_find-node |
| 72 | |
Thomas Vachuska | a7be50d | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 73 | port=${ONOS_WEB_PORT} |
| 74 | user=${ONOS_WEB_USER} |
| 75 | password=${ONOS_WEB_PASS} |
| 76 | |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 77 | # Scan arguments for user/password or other options... |
Seyeon Jeong | be3fa0d | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 78 | while getopts n:P:u:p:x?j?h o; do |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 79 | case "$o" in |
| 80 | n) name=$OPTARG;; |
Thomas Vachuska | a7be50d | 2018-04-16 14:02:18 -0700 | [diff] [blame] | 81 | P) port=$OPTARG;; |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 82 | u) user=$OPTARG;; |
| 83 | p) password=$OPTARG;; |
| 84 | x) extract=true;; |
Seyeon Jeong | be3fa0d | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 85 | j) json=true;; |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 86 | *) usage;; |
| 87 | esac |
| 88 | done |
Thomas Vachuska | 7f2a356 | 2018-02-28 10:02:16 -0800 | [diff] [blame] | 89 | |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 90 | let OPC=$OPTIND-1 |
| 91 | shift $OPC |
| 92 | |
| 93 | [ $# -lt 1 -a -z "$ONOS_INSTANCES" ] && usage; |
| 94 | |
| 95 | diags=/tmp/${name:-onos}-diags |
| 96 | rm -fr $diags $diags.tar.gz; mkdir -p $diags |
| 97 | |
| 98 | [ -z $1 ] && nodes=$ONOS_INSTANCES || nodes=$* |
| 99 | |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 100 | # -j option will activate T3_OFFLINE_PROFILE |
| 101 | [ ! -z $json ] && DIAGS_PROFILE=T3_OFFLINE_PROFILE |
| 102 | |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 103 | # Collect diagnostics from each cluster node |
| 104 | for 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 | |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 110 | 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 Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 117 | |
| 118 | # Acquire remotely obtained diagnostics via ssh CLI |
pier | 3b3a298 | 2020-06-12 18:48:37 +0200 | [diff] [blame] | 119 | 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 Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 123 | cmdLog="$(echo $cmd | cut -d\ -f1 | sed 's/:/-/g').txt" |
Seyeon Jeong | be3fa0d | 2020-02-28 01:17:34 -0800 | [diff] [blame] | 124 | [ ! -z $json ] && cmdLog="${cmdLog%.txt}.json" |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 125 | printf "$cmdLog " |
| 126 | onos $node $cmd 2>/dev/null >$cmdLog |
| 127 | done |
| 128 | |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 129 | printf " Done.\n" |
Thomas Vachuska | 15370d2 | 2018-01-30 13:57:38 -0800 | [diff] [blame] | 130 | done |
| 131 | |
| 132 | # Tar-up diagnostics from all the nodes |
| 133 | cd $diags |
| 134 | tar zcf $diags.tar.gz * |
| 135 | [ -z $extract ] && rm -fr $diags |